本文简单介绍在Windows Server 2008 R2 SP1安装MongoDB
1、 版本选择和下载安装源文件,由于是在64位操作系统且为2008 R2上安装的,故选择版本mongodb-win32-x86_64-2008plus-2.2.2.zip,读者根据自己的实际应用环境进行选择
http://downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.2.2.zip
2、解压到 d:\,再重命名为mongodb,路径为d:\mongodb
3、设置数据库所在目录
创建目录 d:\data\db
4、启动MongoDB服务,可以先通过—help进行帮助查看
- C:\>d:\mongodb\bin\mongod.exe --help
- Allowed options:
- General options:
- -h [ --help ] show this usage information
- --version show version information
- -f [ --config ] arg configuration file specifying additional options
- -v [ --verbose ] be more verbose (include multiple times for more
- verbosity e.g. -vvvvv)
- --quiet quieter output
- --port arg specify port number - 27017 by default
- --bind_ip arg comma separated list of ip addresses to listen on
- - all local ips by default
- --maxConns arg max number of simultaneous connections - 20000 by
- default
- --objcheck inspect client data for validity on receipt
- --logpath arg log file to send write to instead of stdout - has
- to be a file, not directory
- --logappend append to logpath instead of over-writing
- --pidfilepath arg full path to pidfile (if not set, no pidfile is
- created)
- --keyFile arg private key for cluster authentication
- --auth run with security
- --cpu periodically show cpu and iowait utilization
- --dbpath arg directory for datafiles - defaults to \data\db\
- --diaglog arg 0=off 1=W 2=R 3=both 7=W+some reads
- --directoryperdb each database will be stored in a separate
- directory
- --ipv6 enable IPv6 support (disabled by default)
- --journal enable journaling
- --journalCommitInterval arg how often to group/batch commit (ms)
- --journalOptions arg journal diagnostic options
- --jsonp allow JSONP access via http (has security
- implications)
- --noauth run without security
- --nohttpinterface disable http interface
- --nojournal disable journaling (journaling is on by default
- for 64 bit)
- --noprealloc disable data file preallocation - will often hurt
- performance
- --noscripting disable scripting engine
- --notablescan do not allow table scans
- --nssize arg (=16) .ns file size (in MB) for new databases
- --profile arg 0=off 1=slow, 2=all
- --quota limits each database to a certain number of files
- (8 default)
- --quotaFiles arg number of files allowed per db, requires --quota
- --repair run repair on all dbs
- --repairpath arg root directory for repair files - defaults to
- dbpath
- --rest turn on simple rest api
- --slowms arg (=100) value of slow for profile and console log
- --smallfiles use a smaller default file size
- --syncdelay arg (=60) seconds between disk syncs (0=never, but not
- recommended)
- --sysinfo print some diagnostic system information
- --upgrade upgrade db if needed
- Windows Service Control Manager options:
- --install install Windows service
- --remove remove Windows service
- --reinstall reinstall Windows service (equivalent to --remove
- followed by --install)
- --serviceName arg Windows service name
- --serviceDisplayName arg Windows service display name
- --serviceDescription arg Windows service description
- --serviceUser arg account for service execution
- --servicePassword arg password used to authenticate serviceUser
- Replication options:
- --oplogSize arg size to use (in MB) for replication op log. default is
- 5% of disk space (i.e. large is good)
- Master/slave options:
- --master master mode
- --slave slave mode
- --source arg when slave: specify master as <server:port>
- --only arg when slave: specify a single database to replicate
- --slavedelay arg specify delay (in seconds) to be used when applying
- master ops to slave
- --autoresync automatically resync if slave data is stale
- Replica set options:
- --replSet arg arg is <setname>[/<optionalseedhostlist>]
- --replIndexPrefetch arg specify index prefetching if secondary)
- [none|_id_only|all]
- Sharding options:
- --configsvr declare this is a config db of a cluster; default port
- 27019; default dir /data/configdb
- --shardsvr declare this is a shard db of a cluster; default port
- 27018
- --noMoveParanoia turn off paranoid saving of data for moveChunk. this
- is on by default for now, but default will switch
以上是帮助详细内容
- C:\ >d:\mongodb\bin\mongod.exe --dbpath=d:\data\db
- Sun Dec 30 20:22:19 [initandlisten] MongoDB starting : pid=2512 port=27017 ddbpath=d:\data\db 64-bit host=WIN-6HB65GUTBNC
- Sun Dec 30 20:22:19 [initandlisten] db version v2.2.2, pdfile version 4.5
- Sun Dec 30 20:22:19 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
- Sun Dec 30 20:22:19 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2,
- service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
- Sun Dec 30 20:22:19 [initandlisten] options: { dbpath: "d:\data\db" }
- Sun Dec 30 20:22:19 [initandlisten] journal ddir=d:/data/db/journal
- Sun Dec 30 20:22:19 [initandlisten] recover : no journal files present, no recovery needed
- Sun Dec 30 20:22:19 [initandlisten] preallocating a journal file d:/data/db/journal/prealloc.1
- Sun Dec 30 20:22:29 [initandlisten] preallocating a journal file d:/data/db/journal/prealloc.2
- Sun Dec 30 20:22:39 [initandlisten] waiting for connections on port 27017
- Sun Dec 30 20:22:39 [websvr] admin web console waiting for connections on port 28017
通过输出内容可以查看到MongoDB的进程号、监听端口号、版本号、web访问端口等信息
5、可以通过浏览器查看当前各项状态统计信息,例如:http://127.0.0.1:28017/
6、停止 MongoDB服务, Ctrl-C
- C:\Users\Administrator>d:\mongodb\bin\mongod.exe --dbpath=d:\data\db
- Sun Dec 30 20:22:19 [initandlisten] MongoDB starting : pid=2512 port=27017 ddbpath=d:\data\db 64-bit host=WIN-6HB65GUTBNC
- Sun Dec 30 20:22:19 [initandlisten] db version v2.2.2, pdfile version 4.5
- Sun Dec 30 20:22:19 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
- Sun Dec 30 20:22:19 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2,
- service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
- Sun Dec 30 20:22:19 [initandlisten] options: { dbpath: "d:\data\db" }
- Sun Dec 30 20:22:19 [initandlisten] journal ddir=d:/data/db/journal
- Sun Dec 30 20:22:19 [initandlisten] recover : no journal files present, no recovery needed
- Sun Dec 30 20:22:19 [initandlisten] preallocating a journal file d:/data/db/journal/prealloc.1
- Sun Dec 30 20:22:29 [initandlisten] preallocating a journal file d:/data/db/journal/prealloc.2
- Sun Dec 30 20:22:39 [initandlisten] waiting for connections on port 27017
- Sun Dec 30 20:22:39 [websvr] admin web console waiting for connections on port 28017
- Sun Dec 30 20:32:19 Ctrl-C signal
- Sun Dec 30 20:32:19 [consoleTerminate] got CTRL_C_EVENT, will terminate after current cmd ends
- Sun Dec 30 20:32:19 [consoleTerminate] now exiting
- Sun Dec 30 20:32:19 dbexit:
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: going to close listening sockets...
- Sun Dec 30 20:32:19 [consoleTerminate] closing listening socket: 368
- Sun Dec 30 20:32:19 [consoleTerminate] closing listening socket: 376
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: going to flush diaglog...
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: going to close sockets...
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: waiting for fs preallocator...
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: lock for final commit...
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: final commit...
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: closing all files...
- Sun Dec 30 20:32:19 [consoleTerminate] closeAllFiles() finished
- Sun Dec 30 20:32:19 [consoleTerminate] journalCleanup...
- Sun Dec 30 20:32:19 [consoleTerminate] removeJournalFiles
- Sun Dec 30 20:32:19 [consoleTerminate] shutdown: removing fs lock...
- Sun Dec 30 20:32:19 dbexit: really exiting now
7、将MongoDB加入到Windows服务中,
创建文件D:\mongodb\logs\mongodb.log,再执行如下命令:d:\mongodb\bin\mongod.exe --dbpath=d:\data\db --logpath=d:\mongodb\logs\mongodb.log –install
- C:\ >d:\mongodb\bin\mongod.exe --dbpath=d:\data\db --logpath=d:\mongodb\logs\mongodb.log --install
- all output going to: d:\mongodb\logs\mongodb.log
- log file [d:\mongodb\logs\mongodb.log] exists; copied to temporary file [d:\mongodb\logs\mongodb.log.2012-12-30T12-30-11]
这时可以通过net start "Mongo DB"来启动服务:
C:\>net start "Mongo DB"
Mongo DB 服务已经启动成功。
通过net stop"Mongo DB"来关闭服务:
C:\>net stop "Mongo DB"
发生系统错误 109。
管道已结束。
关闭的时候会有个错误提示,估计是个bug,可以忽略,虽然不影响,但总是感觉不好,希望MongoDB团队尽快解决这个小bug。
8、访问MongoDB,双击D:\mongodb\bin\mongo.exe,或者在cmd命令提示符下输入
” D:\mongodb\bin\mongo.exe”回车即可访问。
- C:\>d:\mongodb\bin\mongo.exe --help
- MongoDB shell version: 2.2.2
- usage: d:\mongodb\bin\mongo.exe [options] [db address] [file names (ending in .js)]
- db address can be:
- foo foo database on local machine
- 192.169.0.5/foo foo database on 192.168.0.5 machine
- 192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
- options:
- --shell run the shell after executing files
- --nodb don't connect to mongod on startup - no 'db address'
- arg expected
- --norc will not run the ".mongorc.js" file on start up
- --quiet be less chatty
- --port arg port to connect to
- --host arg server to connect to
- --eval arg evaluate javascript
- -u [ --username ] arg username for authentication
- -p [ --password ] arg password for authentication
- -h [ --help ] show this usage information
- --version show version information
- --verbose increase verbosity
- --ipv6 enable IPv6 support (disabled by default)
- file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified
以下是通过服务的方式启动的时候的日志内容:
- Sun Dec 30 21:05:49 Trying to start Windows service 'MongoDB'
- Sun Dec 30 21:05:49 Service running
- Sun Dec 30 21:05:49 [initandlisten] MongoDB starting : pid=3884 port=27017 ddbpath=d:\data\db 64-bit host=WIN-6HB65GUTBNC
- Sun Dec 30 21:05:49 [initandlisten] db version v2.2.2, pdfile version 4.5
- Sun Dec 30 21:05:49 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
- Sun Dec 30 21:05:49 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
- Sun Dec 30 21:05:49 [initandlisten] options: { dbpath: "d:\data\db", logpath: "d:\mongodb\logs\mongodb.log", service: true }
- Sun Dec 30 21:05:50 [initandlisten] journal ddir=d:/data/db/journal
- Sun Dec 30 21:05:50 [initandlisten] recover : no journal files present, no recovery needed
- Sun Dec 30 21:05:50 [initandlisten] waiting for connections on port 27017
- Sun Dec 30 21:05:50 [websvr] admin web console waiting for connections on port 28017
- Sun Dec 30 21:10:06 [initandlisten] connection accepted from 127.0.0.1:16061 #5 (1 connection now open)
- Sun Dec 30 21:11:34 [initandlisten] connection accepted from 124.193.159.8:27974 #6 (2 connections now open)
- Sun Dec 30 21:12:52 [serviceShutdown] got SERVICE_CONTROL_STOP request from Windows Service Control Manager, will terminate after current cmd ends
- Sun Dec 30 21:12:52 [serviceShutdown] now exiting
- Sun Dec 30 21:12:52 dbexit:
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: going to close listening sockets...
- Sun Dec 30 21:12:52 [serviceShutdown] closing listening socket: 412
- Sun Dec 30 21:12:52 [serviceShutdown] closing listening socket: 420
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: going to flush diaglog...
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: going to close sockets...
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: waiting for fs preallocator...
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: lock for final commit...
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: final commit...
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: closing all files...
- Sun Dec 30 21:12:52 [serviceShutdown] closeAllFiles() finished
- Sun Dec 30 21:12:52 [serviceShutdown] journalCleanup...
- Sun Dec 30 21:12:52 [conn6] end connection 124.193.159.8:27974 (1 connection now open)
- Sun Dec 30 21:12:52 [conn5] end connection 127.0.0.1:16061 (1 connection now open)
- Sun Dec 30 21:12:52 [serviceShutdown] removeJournalFiles
- Sun Dec 30 21:12:52 [serviceShutdown] shutdown: removing fs lock...
- Sun Dec 30 21:12:52 [initandlisten] now exiting
- Sun Dec 30 21:12:52 dbexit: ; exiting immediately