2012年11月27日发布的V2.2版本不支持WinXP。为了避免2G文件大小限制,本次安装使用64位的Win7操作系统。
- 下载网址:
- http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2.2.2.zip
顺便的,各操作系统下载地址汇总:http://www.mongodb.org/downloads
下载文件解压后就可以用了。本次安装解压到I:\mongodb\db,并创建I:\mongodb\data目录,使用下列指令来启动数据库,其中--dbpath指定了数据文件路径(默认路径c:\data\db也需要手工创建):
- i:\mongodb\db\bin\mongod.exe --dbpath "i:\mongodb\data"
启动的过程中弹出防火墙提示框,允许。cmd窗口最后一行看到“waiting for connections”表示mongod.exe已正常工作。这个窗口中还可以看到操作系统是“64-bit”,看到mongodb的版本信息。
数据库启动后I:\mongodb\data下自动创建了journal目录,并创建了mogod.lock文件。journal目录下已经有了一个文件“j._0”了。在cmd窗口中能看到“journal dir=i:/mongodb/data/journal”。这个journal相当于redo,据说对系统性能影响大约5%。为了安全牺牲这5%是值得的,因此,2.0以后的MongoDB版本默认安装都是开启的。我认为这一细节可视为MongoDB奔向成熟的标志。
打开CMD窗口,输入下列指令连接数据库:
- i:\mongodb\db\bin\mongo.exe
At the mongo.exe prompt, issue the following two commands to insert a record in the test collection of the default test database and then retrieve that record:
- > db.test.save({a:1})
- > db.test.find()
{ "_id" : ObjectId("50c6b336ba95d7738d1042e3"), "a" : 1 }
下面记录ctrl+c关闭mongod.exe全过程:
- Tue Dec 11 13:37:07 Ctrl-C signal
- Tue Dec 11 13:37:07 [consoleTerminate] got CTRL_C_EVENT, will terminate after cu
- rrent cmd ends
- Tue Dec 11 13:37:07 [consoleTerminate] now exiting
- Tue Dec 11 13:37:07 dbexit:
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: going to close listening socket
- s...
- Tue Dec 11 13:37:07 [consoleTerminate] closing listening socket: 540
- Tue Dec 11 13:37:07 [consoleTerminate] closing listening socket: 548
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: going to flush diaglog...
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: going to close sockets...
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: waiting for fs preallocator...
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: lock for final commit...
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: final commit...
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: closing all files...
- Tue Dec 11 13:37:07 [consoleTerminate] closeAllFiles() finished
- Tue Dec 11 13:37:07 [consoleTerminate] journalCleanup...
- Tue Dec 11 13:37:07 [consoleTerminate] removeJournalFiles
- Tue Dec 11 13:37:07 [consoleTerminate] shutdown: removing fs lock...
- Tue Dec 11 13:37:07 dbexit: really exiting now
MongoDB as a Windows Service
New in version 2.0.
将MongoDB配置为Windows Service, 实现开机自启动。mongod.exe从2.0开始支持,mongos.exe 2.1.1开开始支持。
-
创建log文件目录Create a specific directory for MongoDB log files:
md i:\mongodb\log
-
创建配置文件:
echo logpath=i:\mongodb\log\mongo.log > i:\mongodb\db\mongod.cfg
使用配置文件指定log文件的位置是件好事。
注意事项
考虑是否设置logappend选项,如果不设置,则每次启动时会清空日志历史信息。从V2.2开始,logpath和logappend的默认设置发生了变化。
下列指令需要管理员权限(“Administrative Privileges”):
-
安装MongoDB服务:
i:\mongodb\db\bin\mongod.exe --config i:\mongodb\db\mongod.cfg --install
all output going to: i:\mongodb\log\mongo.log. Modify the path to the mongod.cfg file as needed. For the--install option to succeed, you must specify a logpath setting or the --logpath run-time option. (配置logpath是必须的)
-
运行MongoDB service:
net start MongoDB
注意事项
If you wish to use an alternate path for your dbpath specify it in the config file (e.g. i:\mongodb\db\mongod.cfg) on that you specified in the --install operation. You may also specify --dbpathon the command line; however, always prefer the configuration file.
优先使用mongod.cfg中的配置。
If the dbpath directory does not exist, mongod.exe will not be able to start. The default value for dbpath is \data\db.
需手工创建dbpath目录。
Stop or Remove the MongoDB Service
-
To stop the MongoDB service:
net stop MongoDB
-
To remove the MongoDB service:
i:\mongodb\db\bin\mongod.exe --remove