MongoDBMongodb实践程序员

安装MongoDB

2017-07-08  本文已影响174人  CoderMiner

Windows中安装MongoDB社区版

概述

平台支持:
从2.2版本开始,MongoDB不再支持Windows XP。请使用最新的Windows版本安装MongoDB

注意:
如果你正在使用Windows Server 2008 R2或Windows 7,请安装 a hotfix to resolve an issue with memory mapped files on Windows

系统要求

MongoDB 社区版,要求的平台Windows Server 2008 R2,Windows Vista,或者更高的Windows版本,可以直接使用 .msi安装包安装

下载

请在下载页面选择合适自己平台的版本 下载页面

MongoDB for Windows 64-bit 只能安装在 Windows Server 2008 R2,Windows 7 64-bit,或更高的Windows版本上,这个
安装包,包含了针对Windows平台的一些优化增强,不能安装在老得windows版本中

MongoDB for Windows 64-bit Legacy 能安装在WIndows Vista,Windows Server 2008中,这个版本并不包含,最近的优化增强

可以通过下面的命令查看最近的系统版本

wmic os get caption
wmic os get osarchitecture

安装 MongoDB 社区版

界面安装

下载对应的MongoDB的安装包 .msi 文件,下载之后,双击安装包,然后一路点击 Next,在安装的过程中,可以自己选择要安装的目录

明令行安装

在Windows中可以使用明令 msiexec.exe 明令来安装MongoDB

  1. 打开命令行
  2. 执行命令

msiexec.exe /q /i mongodb-win32-x86_64-2008plus-ssl-3.4.6-signed.msi
INSTALLLOCATION="C:\Program Files\MongoDB\Server\3.4.6"
ADDLOCAL="all"

可以通过改变 INSTALLLOCATION的值,来改变安装的路径,默认情况下上面的命令会安装所有的MongoDB的组件,
也可以指定安装某些特定的组件,通过指定 ADDLOCAL的参数(用,隔开不同的组件),MongoDB的组件包含以下内容:

组件名 二进制文件
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

如安装特定的组件的命令:

msiexec.exe /q /i mongodb-win32-x86_64-2008plus-ssl-3.4.6-signed.msi
INSTALLLOCATION="C:\Program Files\MongoDB\Server\3.4.6"
ADDLOCAL="MonitoringTools,ImportExportTools,MiscellaneousTools"

运行MongoDB

在非安全模式下,不要把 mongod.exe 文件暴露在公网环境中,默认情况下MongoDB是没有打开安全模式的,

  1. 设置MongoDB环境

MongoDB需要一个文件目录来存储数据,MongoDB默认情况下的目录是在你运行MongoDB的根目录,可以通过 --dbpath
来指定数据的目录

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --dbpath d:\test\mongodb\data

如果路径中包含空格,请使用双引号包含路径

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --dbpath "d:\test\mongo db data"

也可以通过配置文件来指定 dbpath

  1. 启动MongoDB

使用命令 mongod.exe 来启动 MongoDB

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe"
  1. 连接 MondoDB

请使用 mongo.exe 命令,连接到MogoDB

"C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe
  1. 使用MongoDB

接下来就可以使用MongoDB来创建数据,具体的请参考 入门指引

配置MongoDB的服务

  1. 使用 管理员 权限,打开命令行(cmd)
  2. 创建数据目录
mkdir d:\data\db
mkdir d:\data\log
  1. 创建配置文件

配置文件必须设置 systemLog.path,具体的配置选项,请参考配置选项

例:创建一个 d:\mongod.cfg 文件,指定了 systemLog.pathstorage.dbPath

systemLog:
    destination: file
    path: d:\data\log\mongod.log
storage:
    dbPath: d:\data\db
  1. 安装 MongoDB 服务

执行下面的命令一定要使用 管理员权限

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --config "d:\mongod.cfg" --install
  1. 启动 MongoDB 服务
net start MongoDB
  1. 停止或删除 MongoDB 服务

停止服务

net stop MongoDB

删除服务

"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe" --remove

创建一个随着开机可以自启动的服务

具体的步骤和上面的类似 只不过第四步有点不同 ,配置好配置文件之后,在第四步执行

sc.exe create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.4\bin\mongod.exe\" --service --config=\"d:\mongod.cfg\"" DisplayName= "MongoDB" start= "auto"

sc.exe 要求在可执行文件的 = 后面必须要有空格("binPath= "),双引号也要进行转义为""

具体的MongoDB的例子,请参考 MongoDB 服务配置和权限

上一篇下一篇

猜你喜欢

热点阅读