首先自然得先去下載安裝包,官網地址:https://www.mongodb.com/download-center#community,默認會提供最新的64位安裝包,我看到的是4.0.9。下載后雙擊該.msi文件 -> 彈框后點Next -> 打√ -> 點Custom -> 點Browse,選擇自己想安裝的目錄 -> 接下來會讓你選擇是將MongoDB作為windows的服務,默認給你的是windows內置賬戶(Run service as Network Service user),若勾選下面那個是本地或域用戶,需要你自己指定賬戶和密碼。這里我選擇不勾選,我想自己來啟動MongoDB,所以不勾選上面的Install MongoD as a Service -> 點擊Next,這里左下角有個框框默認勾上了,安裝圖形化界面,我這里也不需要,去掉√ -> 繼續Next,最后Install。
接着准備啟動。啟動前我們先創建好數據目錄,因為剛才我沒有勾選配置服務,所以沒有創建這兩個目錄,那就自力更生吧。在E盤創建了E:\MongoDB\data\db和E:\MongoDB\data\log\mongo.log后,我們打開命令行,進入MongoDB的bin目錄,執行啟動命令mongod:
D:\>cd Dev\MongDB\bin D:\Dev\MongDB\bin>mongod -dbpath "e:\MongoDB\data\db" -logpath "e:\MongoDB\data\log\mongo.log"
2019-05-24T14:19:14.893+0800 I CONTROL [main] log file "e:\MongoDB\data\log\mongo.log" exists; moved to "e:\MongoDB\data\log\mongo.log.2019-05-24T06-19-14".
它提示說mongo.log已存在,它自己新創建了一個文件了,實際上這兩個文件都會記錄日志,打開日志看一樣:
2019-05-24T14:19:15.310+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' 2019-05-24T14:19:15.313+0800 I CONTROL [initandlisten] MongoDB starting : pid=31840 port=27017 dbpath=e:\MongoDB\data\db 64-bit host=wulf00 2019-05-24T14:19:15.313+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2 2019-05-24T14:19:15.314+0800 I CONTROL [initandlisten] db version v4.0.9 2019-05-24T14:19:15.314+0800 I CONTROL [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765 2019-05-24T14:19:15.314+0800 I CONTROL [initandlisten] allocator: tcmalloc 2019-05-24T14:19:15.314+0800 I CONTROL [initandlisten] modules: none 2019-05-24T14:19:15.314+0800 I CONTROL [initandlisten] build environment: 2019-05-24T14:19:15.321+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl 2019-05-24T14:19:15.321+0800 I CONTROL [initandlisten] distarch: x86_64 2019-05-24T14:19:15.321+0800 I CONTROL [initandlisten] target_arch: x86_64 2019-05-24T14:19:15.321+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "e:\MongoDB\data\db" }, systemLog: { destination: "file", path: "e:\MongoDB\data\log\mongo.log" } } 2019-05-24T14:19:15.331+0800 I STORAGE [initandlisten] Detected data files in e:\MongoDB\data\db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. 2019-05-24T14:19:15.331+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=3525M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress), 2019-05-24T14:19:15.525+0800 I STORAGE [initandlisten] WiredTiger message [1558678755:524591][31840:140708351462480], txn-recover: Main recovery loop: starting at 4/5888 to 5/256 2019-05-24T14:19:15.671+0800 I STORAGE [initandlisten] WiredTiger message [1558678755:671179][31840:140708351462480], txn-recover: Recovering log 4 through 5 2019-05-24T14:19:15.770+0800 I STORAGE [initandlisten] WiredTiger message [1558678755:770031][31840:140708351462480], txn-recover: Recovering log 5 through 5 2019-05-24T14:19:15.841+0800 I STORAGE [initandlisten] WiredTiger message [1558678755:840877][31840:140708351462480], txn-recover: Set global recovery timestamp: 0 2019-05-24T14:19:15.868+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0) 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] 2019-05-24T14:19:16.358+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'e:/MongoDB/data/db/diagnostic.data' 2019-05-24T14:19:16.360+0800 I NETWORK [initandlisten] waiting for connections on port 27017
上面標黃處用於指定數據文件和日志文件路徑,如果不指定,默認是在D盤,加入D盤沒有提前創建好這兩個目錄,會報錯:
D:\Dev\MongDB\bin>mongod 2019-11-10T18:28:54.863-0700 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' 2019-11-10T18:28:54.886-0700 I CONTROL [initandlisten] MongoDB starting : pid=28268 port=27017 dbpath=D:\data\db\ 64-bit host=wulf00 2019-11-10T18:28:54.886-0700 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2 2019-11-10T18:28:54.888-0700 I CONTROL [initandlisten] db version v4.0.9 2019-11-10T18:28:54.891-0700 I CONTROL [initandlisten] git version: fc525e2d9b0e4bceff5c2201457e564362909765 2019-11-10T18:28:54.891-0700 I CONTROL [initandlisten] allocator: tcmalloc 2019-11-10T18:28:54.892-0700 I CONTROL [initandlisten] modules: none 2019-11-10T18:28:54.893-0700 I CONTROL [initandlisten] build environment: 2019-11-10T18:28:54.893-0700 I CONTROL [initandlisten] distmod: 2008plus-ssl 2019-11-10T18:28:54.894-0700 I CONTROL [initandlisten] distarch: x86_64 2019-11-10T18:28:54.894-0700 I CONTROL [initandlisten] target_arch: x86_64 2019-11-10T18:28:54.895-0700 I CONTROL [initandlisten] options: {} 2019-11-10T18:28:54.912-0700 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory D:\data\db\ not found., terminating 2019-11-10T18:28:54.915-0700 I NETWORK [initandlisten] shutdown: going to close listening sockets... 2019-11-10T18:28:54.916-0700 I CONTROL [initandlisten] now exiting 2019-11-10T18:28:54.916-0700 I CONTROL [initandlisten] shutting down with code:100
默認MongoDB監聽的端口是27017。接下來我們通過客戶端來連接一下,再新開一個命令行窗口 -> 敲命令mongo -> 回車,或者直接雙擊MongoDB的bin目錄下的mongo.exe。我這里直接雙擊mongo.exe,它會自動連接上我們的mongo服務端,並打印一些信息出來,看到>這個符號就代表它在等待我們的請求了:
MongoDB shell version v4.0.9 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("cc101713-a114-40a8-815a-5e21f54cb82d") } MongoDB server version: 4.0.9 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server. 2019-05-24T14:19:15.878+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning. 2019-05-24T14:19:15.879+0800 I CONTROL [initandlisten] --- Enable MongoDB's free cloud-based monitoring service, which will then receive and display metrics about your deployment (disk utilization, CPU, operation statistics, etc). The monitoring data will be available on a MongoDB website with a unique URL accessible to you and anyone you share the URL with. MongoDB may use this information to make product improvements and to suggest MongoDB products and deployment options to you. To enable free monitoring, run the following command: db.enableFreeMonitoring() To permanently disable this reminder, run the following command: db.disableFreeMonitoring() --- > show dbs admin 0.000GB config 0.000GB local 0.000GB > use test switched to db test > show collections > db.wlf.save({"name":"wu linfeng"}) WriteResult({ "nInserted" : 1 }) > show collections wlf > db.wlf.findOne() { "_id" : ObjectId("5ce78e956abf4e49803d5029"), "name" : "wu linfeng" } >
上面先看了下都有哪些庫,然后指定了test(沒有會自動創建),再看了下有哪些集合(剛創建的庫自然是空的),接着用save創建了wlf這個集合並插入了一個屬性name。