MongoDB的介紹我就不說了。直接開始環境的搭建和連接。在這個之前,向大家介紹幾個關於MongoDB的網站。
1. https://www.mongodb.com/
MongoDB的官網。
2. https://www.mongoing.com
MongoDB的中文網站
3. http://docs.mongoing.com/manual-zh/
中文文檔
4. http://github.com/mongodb
github上mongodb的托管地址,各個版本都有。
5. jira.mongodb.org/browser
MongoDB bug反饋的地方
自己本來是打算在linux系統上搭建的,但是環境的搭建總是這么不順利。最開始是在github下載了源碼,然后去編譯,但是不知道為什么源碼總是編譯失敗。然后放棄源碼編譯,去官網下載了二進制壓縮包。下載完成后啟動Mongodb服務器端是還是出現了錯誤,多次嘗試解決未果,就只能先暫時放下然后轉戰windows系統進行學習實踐。不過在后面有時間的話一定會在linux環境中成功搭建。所以下面的實戰均是在windows系統下的練習。
1.下載mongodb。
①進入官網https://www.mongodb.com/ ,點擊右上角DownLoad下載按鈕。

②選擇你要下載的系統和版本,之后點擊DownLoad。

③之后會跳轉到注冊頁面,可以忽略,因為過一會就會開始下載。
可忽略注冊步驟

可看到注冊頁面上方的提示信息。

開始下載

2.下載完成后雙擊安裝。
①默認安裝在C盤以MongoDB為文件名的目錄中。

②目錄下的文件

其中bin就是最重要的一個文件。bin下面的文件如下(沒有全部介紹,現在開始學習只會用到mongo.exe和mongod.exe):

3.創建目錄。
①在E盤創建如下MongoDB-FirstTest文件夾。

②在MongoDB-FirstTest文件夾下創建相應的目錄結構。

③找到之前安裝在C盤的MongoDB文件目錄,拷貝bin目錄下的mongo.exe和mongod.exe到E:\MongoDB-FirstTest\bin


④在E:\MongoDB-FirstTest\conf文件夾下創建配置文件


寫入mongod.conf的文件內容如下:
③data文件夾和log文件夾只創建即可 ,啟動服務器后會自動生成一系列數據。
4. 啟動mongodb服務器。
①打開window命令行工具進入E:\MongoDB-FirstTest\bin

②啟動mongodb服務器,啟動時使用-f指定我們剛才寫的配置文件目錄。

如果我們不配置日志文件,則啟動之后的信息就會在輸入啟動命令之后輸出在控制台上。因為我們配置了日志文件,所以啟動信息會記錄在日志文件中。
5.查看啟動服務器后生成的日志文件和數據文件。
(1)日志文件
①在第四步啟動服務器之后,E:\MongoDB-FirstTest\conf目錄下會生成mongod.log文件

②文件內容如下(可以看看紅色加粗部分的內容):
2016-08-12T13:30:12.103+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] MongoDB starting : pid=2452 port=12345 dbpath=E:\MongoDB-FirstTest\data 64-bit host=hjj-PC 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] db version v3.2.8 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p 9 Jul 2015 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] allocator: tcmalloc 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] modules: none 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] build environment: 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distarch: x86_64 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] target_arch: x86_64 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] options: { config: "E:\MongoDB-FirstTest\conf\mongod.conf", net: { port: 12345 }, storage: { dbPath: "E:\MongoDB-FirstTest\data" }, systemLog: { destination: "file", path: "E:\MongoDB-FirstTest\log\mongod.log" } } 2016-08-12T13:30:12.113+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2016-08-12T13:30:15.061+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker 2016-08-12T13:30:15.062+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'E:/MongoDB-FirstTest/data/diagnostic.data' 2016-08-12T13:30:15.795+0800 I NETWORK [initandlisten] waiting for connections on port 12345
(2)數據文件
在第四步啟動服務器之后,E:\MongoDB-FirstTest\data目錄下會生成如下文件


7.連接mongodb服務器。
①打開window命令行工具進入E:\MongoDB-FirstTest\bin

②連接服務器 mongo 127.0.0.1:12345
紅色部分為連接命令
白色部分為連接成功后的提示

③連接成功后在去查看日志文件會發現最后一行內容
2016-08-12T13:33:54.647+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58238 #1 (1 connection now open)
8.關閉客戶端連接
ctrl+c 即可關閉客戶端連接。
再去查看日志文件(紅色部分)
2016-08-12T13:30:12.103+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] MongoDB starting : pid=2452 port=12345 dbpath=E:\MongoDB-FirstTest\data 64-bit host=hjj-PC
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] db version v3.2.8
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p 9 Jul 2015
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] allocator: tcmalloc
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] modules: none
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] build environment:
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distarch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] options: { config: "E:\MongoDB-FirstTest\conf\mongod.conf", net: { port: 12345 }, storage: { dbPath: "E:\MongoDB-FirstTest\data" }, systemLog: { destination: "file", path: "E:\MongoDB-FirstTest\log\mongod.log" } }
2016-08-12T13:30:12.113+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-08-12T13:30:15.061+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-08-12T13:30:15.062+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'E:/MongoDB-FirstTest/data/diagnostic.data'
2016-08-12T13:30:15.795+0800 I NETWORK [initandlisten] waiting for connections on port 12345
2016-08-12T13:33:54.647+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58238 #1 (1 connection now open)
2016-08-12T14:27:48.123+0800 I NETWORK [conn1] end connection 127.0.0.1:58238 (0 connections now open)
9.關閉服務器
ctrl+c 即可關閉服務器。
再去查看日志文件(紅色部分)
2016-08-12T13:30:12.103+0800 I CONTROL [main] Hotfix KB2731284 or later update is not installed, will zero-out data files 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] MongoDB starting : pid=2452 port=12345 dbpath=E:\MongoDB-FirstTest\data 64-bit host=hjj-PC 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] db version v3.2.8 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p 9 Jul 2015 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] allocator: tcmalloc 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] modules: none 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] build environment: 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] distarch: x86_64 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] target_arch: x86_64 2016-08-12T13:30:12.113+0800 I CONTROL [initandlisten] options: { config: "E:\MongoDB-FirstTest\conf\mongod.conf", net: { port: 12345 }, storage: { dbPath: "E:\MongoDB-FirstTest\data" }, systemLog: { destination: "file", path: "E:\MongoDB-FirstTest\log\mongod.log" } } 2016-08-12T13:30:12.113+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 2016-08-12T13:30:15.061+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker 2016-08-12T13:30:15.062+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'E:/MongoDB-FirstTest/data/diagnostic.data' 2016-08-12T13:30:15.795+0800 I NETWORK [initandlisten] waiting for connections on port 12345 2016-08-12T13:33:54.647+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:58238 #1 (1 connection now open) 2016-08-12T14:27:48.123+0800 I NETWORK [conn1] end connection 127.0.0.1:58238 (0 connections now open) 2016-08-12T14:30:03.952+0800 I CONTROL [thread1] Ctrl-C signal 2016-08-12T14:30:03.953+0800 I CONTROL [consoleTerminate] got CTRL_C_EVENT, will terminate after current cmd ends 2016-08-12T14:30:03.953+0800 I FTDC [consoleTerminate] Shutting down full-time diagnostic data capture 2016-08-12T14:30:04.087+0800 I CONTROL [consoleTerminate] now exiting 2016-08-12T14:30:04.095+0800 I NETWORK [consoleTerminate] shutdown: going to close listening sockets... 2016-08-12T14:30:04.095+0800 I NETWORK [consoleTerminate] closing listening socket: 388 2016-08-12T14:30:04.096+0800 I NETWORK [consoleTerminate] shutdown: going to flush diaglog... 2016-08-12T14:30:04.096+0800 I NETWORK [consoleTerminate] shutdown: going to close sockets... 2016-08-12T14:30:04.107+0800 I STORAGE [consoleTerminate] WiredTigerKVEngine shutting down 2016-08-12T14:30:06.037+0800 I STORAGE [consoleTerminate] shutdown: removing fs lock... 2016-08-12T14:30:06.038+0800 I CONTROL [consoleTerminate] dbexit: rc: 12
