6MongoDB安装配置(Windows)


要在Windows上安装 MongoDB,首先打开: http://www.mongodb.org/downloads 下载最新版本的MongoDB。确保根据您的Windows版本获得正确版本的MongoDB。要获取 Windows 版本,请打开命令提示符并执行以下命令。

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

如上所示,这里 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"
Shell

可以使用--dbpath选项为mongod.exe指定数据文件的备用路径,例如:

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

启动MongoDB

要启动 MongoDB,请运行 mongod.exe 。 例如,从命令提示符:

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

注意:如果不使用 --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)" } }
`
Shell

打开 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
>
Shell

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
>
Shell

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
}
>
Shell

 关注右侧公众号,随时随地查看教程 MongoDB教程目录 
转载自:https://www.yiibai.com/mongodb/mongodb_environment.html

You may also like...