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 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檔案。 將出現一組螢幕,指導您完成安裝過程。

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

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

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

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

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

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

執行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
}
>