MongoDB安裝配置(Windows)

要在Windows上安裝 MongoDB,首先打開: http://www.mongodb.org/downloads 下載最新版本的MongoDB。確保根據您的Windows版本獲得正確版本的MongoDB。要獲取 Windows 版本,請打開命令提示符並執行以下命令。

C:\Users\Administrator> wmic os get osarchitecture
OSArchitecture
64 位

如上所示,這裏 Windows 版本是 64位的操作系統,所以下載對應的 MongoDB-for-Windows 64位版本,如下圖所示 -

MongoDB安裝配置(Windows)

前提條件

MongoDB社區版需要Windows Server 2008 R2,Windows Vista或更高版本(注:本教程將演示在Windows10 46位系統上安裝MongoDB)。 .msi安裝程序包括所有其他軟件相關性,並將自動升級任何使用.msi文件安裝的舊版本的 MongoDB。

MongoDB安裝

在Windows資源管理器中,找到下載的MongoDB.msi文件,通常位於默認的「下載」文件夾中。 雙擊mongodb-win32-x86_64-2008plus-ssl-3.4.5-signed.msi文件。 將出現一組屏幕,指導您完成安裝過程。

MongoDB安裝配置(Windows)

下一步(Next>),在彈出的界面中,打上勾以表示「同意安裝協議」 -

MongoDB安裝配置(Windows)

下一步(Next>),選擇自定義(Custom)安裝,如下圖所示 -

MongoDB安裝配置(Windows)

下一步(Next>),選擇安裝目錄爲:D:\Program Files\MongoDB\Server\3.4\ ,如下圖所示 -

MongoDB安裝配置(Windows)

下一步(Next>),開始安裝 -

MongoDB安裝配置(Windows)

安裝過程中,如下圖所示 -

MongoDB安裝配置(Windows)

安裝完成,點擊:Finish,如下圖所示 -

MongoDB安裝配置(Windows)

運行MongoDB

注意:不要使 mongod.exe 在公共網絡上可見,而不使用「安全模式」進行認證設置。 MongoDB旨在在受信任的環境中運行,默認情況下數據庫不啓用「安全模式」。

MongoDB需要一個數據目錄來存儲所有數據。MongoDB的默認數據目錄路徑是從中啓動 MongoDB 的驅動器上的絕對路徑D:\Program Files\MongoDB\Server\3.4\data\db。通過在命令提示符中運行以下命令來創建此文件夾:

D:\Program Files\MongoDB\Server\3.4> "md D:\Program Files\MongoDB\Server\3.4\data"

可以使用--dbpath選項爲mongod.exe指定數據文件的備用路徑,例如:

「D:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --dbpath "D:\Program Files\MongoDB\Server\3.4\data\db"

啓動MongoDB

要啓動 MongoDB,請運行 mongod.exe 。 例如,從命令提示符:

「D:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"

注意:如果不使用 --dbpath 指定數據存儲的目錄,那麼 MongoDB 默認使用的是 「C:\data\db「 目錄,所以在啓動 MongoDB 數據庫之前要確保 「C:\data\db「 目錄已經創建了。

在執行上面命令啓動後,應該會看到類似下面的輸出結果 -

C:\Users\Administrator>"D:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"
2017-06-29T02:37:46.688+0800 I CONTROL  [initandlisten] MongoDB starting : pid=10652 port=27017 dbpath=C:\data\db\ 64-bit host=MY-PC
2017-06-29T02:37:46.691+0800 I CONTROL  [initandlisten] targetMinOS: Windows
... ...
... ...
2017-06-29T02:37:46.733+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1491M,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),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-06-29T02:37:47.738+0800 I CONTROL  [initandlisten]
2017-06-29T02:37:47.742+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-06-29T02:37:47.751+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-06-29T02:37:47.761+0800 I CONTROL  [initandlisten]
2017-06-29T02:37:48.315+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/data/db/diagnostic.data'
2017-06-29T02:37:48.659+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-06-29T02:37:48.669+0800 I INDEX    [initandlisten]          building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-06-29T02:37:48.710+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2017-06-29T02:37:48.718+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2017-06-29T02:37:48.732+0800 I NETWORK  [thread1] waiting for connections on port 27017
2017-06-29T02:40:54.820+0800 I NETWORK  [thread1] connection accepted from 127.0.0.1:60285 #1 (1 connection now open)
2017-06-29T02:40:54.822+0800 I NETWORK  [conn1] received client metadata from 127.0.0.1:60285 conn1: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.4.5" }, os: { type: "Windows", name: "Microsoft Windows 8", architecture: "x86_64", version: "6.2 (build 9200)" } }
`

打開 MongoDB 客戶端測試使用

D:\Program Files\MongoDB\Server\3.4\bin> mongo.exe
MongoDB shell version v3.4.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.5
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:
2017-06-29T02:37:47.738+0800 I CONTROL  [initandlisten]
2017-06-29T02:37:47.742+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-06-29T02:37:47.751+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-06-29T02:37:47.761+0800 I CONTROL  [initandlisten]
>
> db
test
> show dbs 
admin  0.000GB
local  0.000GB
>

MongoDB幫助

要獲取命令列表,請在MongoDB客戶端中鍵入:db.help()。 這將列出一個命令列表,如下面的屏幕截圖所示 -

> db.help()
DB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
        db.auth(username, password)
        db.cloneDatabase(fromhost)
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost)
        db.createCollection(name, { size : ..., capped : ..., max : ... } )
        db.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } )
        db.createUser(userDocument)
        db.currentOp() displays currently executing operations in the db
        db.dropDatabase()
        db.eval() - deprecated
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections
        db.getCollectionNames()
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getLogComponents()
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getName()
        db.getPrevError()
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
        db.hostInfo() get details about the server's host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.logout()
        db.printCollectionStats()
        db.printReplicationInfo()
        db.printShardingStatus()
        db.printSlaveReplicationInfo()
        db.dropUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
        db.serverStatus()
        db.setLogLevel(level,<component>)
        db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
        db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db
        db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db
        db.setVerboseShell(flag) display extra information in shell output
        db.shutdownServer()
        db.stats()
        db.version() current version of the server
>

MongoDB統計

要獲取有關MongoDB服務器的統計信息,請在 MongoDB 客戶端中鍵入命令:db.stats()。 這個命令將顯示數據庫名稱,數據庫中的集合和文檔數量。命令的輸出如下面的截圖所示 -

> db.stats()
{
        "db" : "test",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "ok" : 1
}
>