首先天然得先去下載安裝包,官網地址: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。git
接着準備啓動。啓動前咱們先建立好數據目錄,由於剛纔我沒有勾選配置服務,因此沒有建立這兩個目錄,那就自力更生吧。在E盤建立了E:\MongoDB\data\db和E:\MongoDB\data\log\mongo.log後,咱們打開命令行,進入MongoDB的bin目錄,執行啓動命令mongod:web
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已存在,它本身新建立了一個文件了,實際上這兩個文件都會記錄日誌,打開日誌看同樣:mongodb
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
默認MongoDB監聽的端口是27017。接下來咱們經過客戶端來鏈接一下,再新開一個命令行窗口 -> 敲命令mongo -> 回車,或者直接雙擊MongoDB的bin目錄下的mongo.exe。我這裏直接雙擊mongo.exe,它會自動鏈接上咱們的mongo服務端,並打印一些信息出來,看到>這個符號就表明它在等待咱們的請求了:shell
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。windows