MongoDB以其操作简单、完全免费、源码公开、随时下载等特点,被广泛应用于各种大型门户网站和专业网站,大大降低了运营成本。本文描述了在Widows平台下的安装步骤及其过程,供大家参考。
一、主要步骤
1、查看当前使用的Windows版本及架构
wmic os get caption
wmic os get osarchitecture
2、下载对应的版本
http://www.mongodb.org/downloads
3、配置mongdb
创建存放数据文件的路径
md \data\db
You can specify an alternate path for data files using the –dbpath option to mongod.exe, for example:
C:\mongodb\bin\mongod.exe –dbpath d:\test\mongodb\data
If your path includes spaces, enclose the entire path in double quotes, for example:
C:\mongodb\bin\mongod.exe –dbpath “d:\test\mongo db data”
4、启动mongodb
To start MongoDB, run mongod.exe. For example, from the Command Prompt:
C:\mongodb\bin\mongod.exe
This starts the main MongoDB database process. The waiting for connections message in the console
output indicates that the mongod.exe process is running successfully.
5、连接到mongodb
To connect to MongoDB through the mongo.exe shell, open another Command Prompt.
C:\mongodb\bin\mongo.exe
6、终止mongodb
Later, to stop MongoDB, press Control+C in the terminal where the mongod instance is running
二、Mongodb的主要组件
Component Set Binaries
--------------- --------------------
Server mongod.exe
Router mongos.exe
Client mongo.exe
MonitoringTools mongostat.exe, mongotop.exe
ImportExportTools mongodump.exe, mongorestore.exe, mongoexport.exe,mongoimport.exe
MiscellaneousTools bsondump.exe, mongofiles.exe, mongooplog.exe, mongoperf.exe
三、配置Mongodb作为Windows服务
Step 1: Open an Administrator command prompt ###打开一个命令行窗口
Step 2: Create directories. ###创建数据库数据及日志目录
mkdir c:\data\db
mkdir c:\data\log
Step 3: Create a configuration file ###创建配置文件
Create a configuration file. The file must set systemLog.path. Include additional configuration options as appropriate.
For example, create a file at C:\mongodb\mongod.cfg that specifies both systemLog.path and storage.dbPath:
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
Step 4: Install the MongoDB service.
Install the MongoDB service by starting mongod.exe with the --install option and the -config option to
specify the previously created configuration file.
"C:\mongodb\bin\mongod.exe" --config "C:\mongodb\mongod.cfg" --install
Step 5: Start the MongoDB service.
net start MongoDB
Step 6: Stop or remove the MongoDB service as needed.
To stop the MongoDB service use the following command:
net stop MongoDB
To remove the MongoDB service use the following command:
"C:\mongodb\bin\mongod.exe" --remove
四、安装演示
1、安装
C:\Users\1636>wmic os get caption
Caption
Microsoft Windows 7 Ultimate
C:\Users\1636>wmic os get osarchitecture
OSArchitecture
64-bit
使用下载的msi文件开始安装到指定文件夹,如本例中的D:\MongoDB\Server\3.0\
2、配置环境变量
//如本例中将D:\MongoDB\Server\3.0\bin添加到系统环境变量PATH
;D:\MongoDB\Server\3.0\bin
D:\>mkdir d:\MongoDB\data
D:\>d:\MongoDB\Server\3.0\bin\mongod.exe --dbpath d:\MongoDB\data
2015-10-29T09:26:11.498+0800 I CONTROL Hotfix KB2731284 or later update is not installed, will zero-out data files
2015-10-29T09:26:11.508+0800 I JOURNAL [initandlisten] journal dir=d:\MongoDB\data\journal
2015-10-29T09:26:11.509+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-10-29T09:26:11.611+0800 I JOURNAL [durability] Durability thread started
2015-10-29T09:26:11.613+0800 I JOURNAL [journal writer] Journal writer thread started
2015-10-29T09:26:11.718+0800 I CONTROL [initandlisten] MongoDB starting : pid=40540 port=27017
dbpath=d:\MongoDB\data 64-bit host=hq1636
2015-10-29T09:26:11.719+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2015-10-29T09:26:11.722+0800 I CONTROL [initandlisten] db version v3.0.6
2015-10-29T09:26:11.725+0800 I CONTROL [initandlisten] git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2
2015-10-29T09:26:11.728+0800 I CONTROL [initandlisten] build info: windows sys.getwindowsversion(major=6,
minor=1, build=7601, platform=2, service_pa
ck='Service Pack 1') BOOST_LIB_VERSION=1_49
2015-10-29T09:26:11.731+0800 I CONTROL [initandlisten] allocator: tcmalloc
2015-10-29T09:26:11.737+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "d:\MongoDB\data" } }
2015-10-29T09:26:11.906+0800 I NETWORK [initandlisten] waiting for connections on port 27017
//此处已表明mongodb已启动等待连接
在打开一个单独的cmd窗口,使用mongo客户端连接到mongod
C:\Users\1636>d:\MongoDB\Server\3.0\bin\mongo.exe
2015-10-29T09:28:12.427+0800 I CONTROL Hotfix KB2731284 or later update is not installed, will zero-out data
files
MongoDB shell version: 3.0.6
connecting to: test //此时已连接成功
> db.version()
3.0.6
> show dbs
local 0.078GB
//如果此时需要停止mongod,则直接在原来的窗口使用ctrl+c即可
3、将Mongodb添加为Windows服务
D:\>mkdir d:\MongoDB\data
D:\>mkdir d:\MongoDB\log
D:\> -- Author : Leshami
D:\> -- Blog : http://blog.csdn.net/leshami
D:\>echo logpath=d:\MongoDB\log\mongd.log> "d:\MongoDB\mongod.cfg"
D:\>echo dbpath=d:\MongoDB\data>> "d:\MongoDB\mongod.cfg"
D:\>type d:\MongoDB\mongod.cfg
logpath=d:\MongoDB\log\mongd.log
dbpath=d:\MongoDB\data
D:\>sc.exe create MongoDB binPath= "D:\MongoDB\Server\3.0\bin\mongod.exe --service --config=D:\MongoDB\mongod.cfg"
[SC] CreateService SUCCESS
D:\>net start MongoDB
The MongoDB service is starting.
The MongoDB service was started successfully.
D:\>net stop MongoDB
The MongoDB service is stopping.
The MongoDB service was stopped successfully.
4、修复Bug:Hotfix KB2731284
https://support.microsoft.com/zh-cn/kb/2731284
Linux下快速安装MongoDB