MongoDB的安装

来源:互联网 发布:惠东网络问政平台 编辑:程序博客网 时间:2024/04/30 01:17

1.下载对应的版本:http://www.mongodb.org/downloads
  我下载的是mongodb-win32-i386-2.2.1-rc1.zip
2.安装
  2.1 解压到任意目录。我这里是D:\mySoft\mongodb此时该目录下有bin文件夹。
  2.2 mongodb需要db目录(存放数据),log目录(存放日志)的。手动创建data,db,log文件        夹, 在mongodb目录下建立data文件夹,在data文件夹建立db文件夹,在data文件夹下建立log文件夹,并在log文件夹下建立mongodb.log文件
2.3 准备工作到位,现在开始安装
   打开doc窗口到D:\mySoft\mongodb\bin目录下,输入命令D:\mySoft\mongodb\bin>mongod -dbpath "D:\mySoft\mongodb\data\db",出现下面的打印,并且会持续很久
Tue Mar 05 11:49:37 [initandlisten] waiting for connections on port 27017 
到浏览器中输入地址“localhost:27017”,回车!
浏览器会打印:
You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number。
此时回到doc窗口。可以看到了“admin web console waiting for connections on port 28017 ”。
再回到浏览器。输入“localhost:28017”,回车。
看到如下信息,说明MongoDB已经启动成功了。
db version v2.2.1-rc1, pdfile version 4.5
git hash: 9495696aa169fb34a85eafcb58b64d2ca5e74f25
sys info: windows sys.getwindowsversion(major=6, minor=0, build=6002, platform=2, service_pack='Service Pack 2') BOOST_LIB_VERSION=1_49
uptime: 758 seconds

测试与使用:
新打开一个DOS窗口,win+R, cmd,回车。
进入到D:\mySoft\mongodb\bin目录,执行命令“D:\mySoft\mongodb\bin>mongo”,

test是默认数据库,foo是默认表。简单的插入,查询。
[html]
> db.foo.insert({a:123,b:345}) 
> db.foo.find() 
{ "_id" : ObjectId("51357b2003004382ccd87dcd"), "a" : 123, "b" : 345 } 



3、安装Windows服务

每次运行mongod --dbpath D:\mySoft\mongodb\data命令行来启动MongoDB实在是不方便,就像我免安装的MySQL一样,我想把它作为Windows服务,这样就方便多了。

D:\mySoft\mongodb\bin>mongod --logpath D:\mySoft\mongodb\logs\MongoDB.log --logappend --dbpath D:\mySoft\mongodb\data --directoryperdb --serviceName MongoDB --install
all output going to: D:\mySoft\mongodb\logs\MongoDB.log
Creating service MongoDB.
Service creation successful.
Service can be started from the command line via 'net start "MongoDB"'.

注意:这条命令要到MongoDB的bin目录下运行,刚开始的时候,我就直接在D:\下运行,结果服务的可执行目录为【"D:\mySoft\mongodb" --logpath  "D:\mySoft\mongodb\logs\MongoDB.log"  --logappend  --dbpath  "D:\mySoft\mongodb\data"  --directoryperdb  --service 】,肯定是不对的。

该命令行指定了日志文件:D:\MongoDB\logs\MongoDB.log,日志是以追加的方式输出的;

数据文件目录:D:\mySoft\mongodb\data,并且参数--directoryperdb说明每个DB都会新建一个目录;

Windows服务的名称:MongoDB;

以上的三个参数都是可以根据自己的情况而定的

最后是安装参数:--install,与之相对的是--remove

启动MongoDB:net start MongoDB

停止MongoDB:net stop MongoDB

 

0 0