如何在Windows上安装MongoDB,启动,卸载

时间:2020-02-23 14:40:49  来源:igfitidea点击:

今天,我们将学习如何在Windows上安装MongoDB。
我们大多数人都将Windows用于个人用途。
因此,我编写本教程来帮助您在Windows上安装MongoDB。
我们还将学习如何在Windows上启动和卸载MongoDB。

Windows版MongoDB

从MongoDB 2.2版开始,MongoDB不支持Windows XP。
因此,如果您使用的是Windows XP,请升级它或者使用较低版本的MongoDB。
本教程旨在帮助您在Windows最新版本(即Windows 7和Windows 8、8.1等)上安装MongoDB。

在Windows上安装MongoDB

请按照以下步骤将MongoDB安装到Windows系统中。

  • 考虑实际需要的特定MongoDB构建类型可以使用Windows的MongoDB的三种构建类型。
    Windows Server 2008 R2系列的MongoDB:此构建类型仅在2008R2,Windows 7 64位,Windows 8和最新版本的Windows上运行。

  • 适用于Windows 64位的MongoDB:这种MongoDB生成类型可在Windows XP以外的任何64位版本的Windows上运行,涉及Windows Server 2008 R2和Windows 7 64位。

  • 适用于Windows 32位版的MongoDB:MongoDB可以在Windows XP以外的任何32位版本的Windows上运行,MongoDB的32位版本仅设计用于较旧的系统,并用于测试和开发系统。
    Windows 32位版本的MongoDB仅支持小于2GB的数据库。

如果您想知道系统上正在运行哪个特定版本的Windows,则只需在命令提示符下使用以下命令。

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\hyman>wmic os get osarchitecture
OSArchitecture
64-bit

C:\Users\hyman>
  • 下载适用于Windows的MongoDB从MongoDB下载页面下载最新版本。
    确保为Windows下载了最新版本的MongoDB。
    请注意,MongoDB的64位版本不能在32位Windows上运行。

  • MongoDB安装选项在Windows资源管理器中,只需找到下载的MongoDB msi文件,然后双击该msi文件。
    一系列屏幕将指导您完成安装过程。
    有多个选项,例如"典型","自定义"和"完整"安装。
    MongoDB默认安装进入"程序文件",因此,如果要更改它,则进行"自定义"安装。

  • MongoDB安装文件夹移动到另一个位置(可选)有时我们想将安装移动到另一个目录,为此可以使用move命令。
    该命令需要以管理员身份运行。
    例如,要将文件夹移动到C:\MongoDB,请按照下图所示的步骤进行操作.MongoDB不依赖于任何其他系统,因为它是自包含的。
    您可以从您选择的文件夹中执行MongoDB,并将其安装在任何文件夹中(例如D:\test \ mongodb)

运行MongoDB服务器

我们需要创建一个目录,MongoDB服务器将其中存储所有数据。
MongoDB的默认数据目录路径为\ data \ db。
使用命令提示符中的以下命令创建此文件夹。
请注意,如果该目录不存在,MongoDB将尝试创建它。
但是事先创建它是很好的。

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\hyman>cd \

C:\>md \data\db

C:\>

要启动MongoDB服务器,我们需要运行mongod.exe。
例如,从命令提示符;

C:\>C:\MongoDB\bin\mongod.exe

此MongoDB启动主数据库过程。
控制台结果中的等待连接消息确定mongod.exe进程已完成。

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\hyman>cd \

C:\>MongoDB\bin\mongod.exe
MongoDB\bin\mongod.exe --help for help and startup options
2014-11-11T13:36:19.016+0530 [initandlisten] MongoDB starting : pid=5292 port=27
017 dbpath=\data\db\ 64-bit host=Meghna
2014-11-11T13:36:19.017+0530 [initandlisten] targetMinOS: Windows 7/Windows Serv
er 2008 R2
2014-11-11T13:36:19.017+0530 [initandlisten] db version v2.6.5
2014-11-11T13:36:19.017+0530 [initandlisten] git version: e99d4fcb4279c0279796f2
37aa92fe3b64560bf6
2014-11-11T13:36:19.017+0530 [initandlisten] build info: windows sys.getwindowsv
ersion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1')
BOOST_LIB_VERSION=1_49
2014-11-11T13:36:19.017+0530 [initandlisten] allocator: system
2014-11-11T13:36:19.017+0530 [initandlisten] options: {}
2014-11-11T13:36:19.092+0530 [initandlisten] journal dir=\data\db\journal
2014-11-11T13:36:19.094+0530 [initandlisten] recover : no journal files present,
 no recovery needed
2014-11-11T13:36:19.186+0530 [initandlisten] allocating new ns file \data\db\loc
al.ns, filling with zeroes...
2014-11-11T13:36:20.002+0530 [FileAllocator] allocating new datafile \data\db\lo
cal.0, filling with zeroes...
2014-11-11T13:36:20.003+0530 [FileAllocator] creating directory \data\db_tmp
2014-11-11T13:36:20.031+0530 [FileAllocator] done allocating datafile \data\db\l
ocal.0, size: 64MB,  took 0.018 secs
2014-11-11T13:36:20.035+0530 [initandlisten] build index on: local.startup_log p
roperties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2014-11-11T13:36:20.036+0530 [initandlisten]     added index to empty collection

2014-11-11T13:36:20.037+0530 [initandlisten] command local.$cmd command: create
{ create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0
 numYields:0  reslen:37 851ms
2014-11-11T13:36:20.038+0530 [initandlisten] waiting for connections on port 270
17

您可以使用mongod.exe的–dbpath选项为数据文件使用其他路径,例如:

C:\>C:\MongoDB\bin\mongod.exe --dbpath D:\test\mongodb\data

如果您的路径包含空格,则需要使用双引号,如下所示。

C:\>C:\MongoDB\bin\mongod.exe --dbpath "D:\test\mongo db data"

安全级别取决于您的系统配置,Windows提供了一个弹出的"安全警报"对话框,用于阻止" C:\Folder Name \ MongoDB \ bin \ mongod.exe"的某些功能阻止网络通信。

所有客户端都应选择专用网络。
例如,我的家庭或者工作网络,然后单击"允许访问"。
有关安全性和MongoDB的更多信息,请查看安全性文档。

通过命令提示符连接到MongoDB服务器

我们可以通过mongo.exe客户端应用程序连接到MongoDB服务器,只需打开另一个命令提示符并执行它即可。

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\hyman>C:\MongoDB\bin\mongo.exe
MongoDB shell version: 2.6.5
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
      https://docs.mongodb.org/
Questions? Try the support group
      https://groups.google.com/group/mongodb-user
>

就是这样,一旦连接,我们就可以发出任何MongoDB Shell命令。
要关闭客户端连接,只需触发"退出"命令或者简单地关闭命令提示符即可。

MongoDB作为Windows服务

上面安装的问题是我们需要手动启动MongoDB服务器,然后保持命令提示符处于打开状态。
解决方案是将其配置为Windows服务,以便它可以在系统启动并运行后立即启动。
由于它将在后台运行,因此无需保持命令提示符打开。

在将MongoDB安装配置为Windows服务之前,让我们为MongoDB日志创建一个配置文件。

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\hyman>cd \

C:\>md MongoDB-logs

现在,使用echo命令,我们可以创建一个配置文件。
如果遇到任何权限问题,请以管理员身份运行命令提示符。

C:\WINDOWS\system32>echo logpath=C:\MongoDB-logs\mongo.log > C:\MongoDB\mongod.conf

现在以管理员身份在命令下方运行,以将MongoDB设置为服务。

C:\>C:\MongoDB\bin\mongod.exe --config C:\MongoDB\mongod.conf --install

C:\>

如果要更改MongoDB服务器端口或者任何其他配置,则可以在上述配置文件中提供它们。

如果要为mongod.exe的多个示例安装服务,请使用唯一的–serviceName和–serviceDisplayName安装每个服务。
仅当存在足够的系统资源并且您的设计需要它时,才使用多个示例。

卸载MongoDB Windows

如果要删除MongoDB服务,请以管理员身份在命令下方运行。

C:\>C:\MongoDB\bin\mongod.exe --config C:\MongoDB\mongod.conf --remove

C:\>