将mongodb安装为windows服务

时光怂恿深爱的人放手 提交于 2020-02-29 05:34:33

首先  下载mongodb:

http://www.mongodb.org/downloads

解压到自己喜欢的一个目录

D:\nosql\mongodb

配置MONGODB_HOME环境变量

MONGODB_HOME=D:\nosql\mongodb\mongodb-win32-i386-2.0.6

修改path变量值:在最后加上%MONGODB_HOME%\bin;

编写安装批处理语句:

mongod --logpath D:\nosql\mongodb\logs\mongodb.log  --logappend --dbpath D:\nosql\mongodb\data\db\ --directoryperdb --serviceName MongoDB --install

语句中不存在的目录和文件需要自己建立

操作打开cmd,进入到

D:\nosql\mongodb\mongodb-win32-i386-2.0.6\bin目录,执行以上批处理:

失败!!!!!

查看日志:D:\nosql\mongodb\logs\mongodb.log 


Error connecting to the Service Control Manager: 拒绝访问。 (5)
Tue Jan 15 10:45:31 dbexit: 
Tue Jan 15 10:45:31 shutdown: going to close listening sockets...
Tue Jan 15 10:45:31 shutdown: going to flush diaglog...
Tue Jan 15 10:45:31 shutdown: going to close sockets...
Tue Jan 15 10:45:31 shutdown: waiting for fs preallocator...
Tue Jan 15 10:45:31 shutdown: closing all files...
Tue Jan 15 10:45:31 closeAllFiles() finished
Tue Jan 15 10:45:31 dbexit: really exiting now


这是操作系统权限问题。cmd必须通过系统管理员运行才行!!!

从开始菜单定位到cmd 右键 以管理员身份运行。

重新定位到D:\nosql\mongodb\mongodb-win32-i386-2.0.6\bin目录

执行上面的批处理,然后查看日志:D:\nosql\mongodb\logs\mongodb.log 

多出一部分:

Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.
Tue Jan 15 10:50:33 dbexit: 
Tue Jan 15 10:50:33 shutdown: going to close listening sockets...
Tue Jan 15 10:50:33 shutdown: going to flush diaglog...
Tue Jan 15 10:50:33 shutdown: going to close sockets...
Tue Jan 15 10:50:33 shutdown: waiting for fs preallocator...
Tue Jan 15 10:50:33 shutdown: closing all files...
Tue Jan 15 10:50:33 closeAllFiles() finished
Tue Jan 15 10:50:33 dbexit: really exiting now

补充一下三个命令:(必须以管理员身份运行cmd,然后执行下面的命令)

启动MongoDB:net start MongoDB
停止MongoDB:net stop MongoDB
删除MongoDB:sc delete MongoDB


此时通过services.msc进入服务管理控制台就可以看到有一个Mongo DB的服务,这只是显示名字,单击查看属性可以看到服务名是:MongoDB

此时可以尝试启动mongodb服务,擦。。这次是1053错误。。。,(服务没有及时响应启动或控制请求)才吃查看日志:

Tue Jan 15 11:49:55 
Tue Jan 15 11:49:55 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Tue Jan 15 11:49:55 
Tue Jan 15 11:49:55 [initandlisten] MongoDB starting : pid=12540 port=27017 dbpath=D:\nosql\mongodb\data\db"  --directoryperdb  --service   32-bit host=WJH-PC
Tue Jan 15 11:49:55 [initandlisten] 
Tue Jan 15 11:49:55 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Tue Jan 15 11:49:55 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Tue Jan 15 11:49:55 [initandlisten] **       with --journal, the limit is lower
Tue Jan 15 11:49:55 [initandlisten] 
Tue Jan 15 11:49:55 [initandlisten] db version v2.0.6, pdfile version 4.5
Tue Jan 15 11:49:55 [initandlisten] git version: e1c0cbc25863f6356aa4e31375add7bb49fb05bc
Tue Jan 15 11:49:55 [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_42
Tue Jan 15 11:49:55 [initandlisten] options: { dbpath: "D:\nosql\mongodb\data\db"  --directoryperdb  --service  ", logappend: true, logpath: "D:\nosql\mongodb\logs\mongodb.log" }
Tue Jan 15 11:49:55 [initandlisten] exception in initAndListen: 10296 dbpath (D:\nosql\mongodb\data\db"  --directoryperdb  --service  ) does not exist, terminating
Tue Jan 15 11:49:55 dbexit: 
Tue Jan 15 11:49:55 [initandlisten] shutdown: going to close listening sockets...
Tue Jan 15 11:49:55 [initandlisten] shutdown: going to flush diaglog...
Tue Jan 15 11:49:55 [initandlisten] shutdown: going to close sockets...
Tue Jan 15 11:49:55 [initandlisten] shutdown: waiting for fs preallocator...
Tue Jan 15 11:49:55 [initandlisten] shutdown: closing all files...
Tue Jan 15 11:49:55 [initandlisten] closeAllFiles() finished
Tue Jan 15 11:49:55 dbexit: really exiting now


查询得知是路径解析错误,得修改注册表!!!

 其实得修改两个地方,一个是服务的名字一个是路径。

通过regedit进入注册表编辑器:

HKEY_LOCAL_MACHINE/SYSTEM/service/MongoDB




双击ImagePath

将值修改为:

"D:\nosql\mongodb\mongodb-win32-i386-2.0.6\bin\mongod" --logpath  "D:\nosql\mongodb\logs\mongodb.log"  --logappend  --dbpath  "D:\nosql\mongodb\data\db\\"  --directoryperdb --serviceName MongoDB --service

注意标红部分是要修改的。

好了,现在可以启动服务了。

service_pack='Service Pack 2') BOOST_LIB_VERSION=1_42
Tue Jan 15 13:04:48 [initandlisten] options: { dbpath: "D:\nosql\mongodb\data\db\", directoryperdb: true, logappend: true, logpath: "D:\nosql\mongodb\logs\mongodb.log", service: true, serviceName: "MongoDB" }
Tue Jan 15 13:04:48 [initandlisten] waiting for connections on port 27017
Tue Jan 15 13:04:48 [websvr] admin web console waiting for connections on port 28017








易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!