本机系统:win10/64位
mongodb版本:mongodb-win32-x86_64-2008plus-ssl-3.6.4-signed.msi
安装步骤:
一、下载
1、移步官网下载
2、在下载目录找到安装文件
3、双击安装文件,弹出交互式安装界面
4、点击“Next”后,同意安装协议,再次点击“Next”


5、通过点击 "Custom(自定义)" 按钮来设置你的安装目录,为了方便建议文件夹命名尽量简短如(d:\mongodb)


6、点击“Install”,等待安装完成,最后点击“Finish”,完成安装。


7、我的安装目录是d:\mongodb

二、环境配置
我们都知道,作为一个 developer,每次启动都要去对应的目录,所以第一步是把
D:\mongodb\bin
,添加到环境变量中,这样就可以在任意目录下访问其中的东西了。步骤:电脑->属性->高级系统设置->环境变量->系统变量->path
三、安装MongoDB服务
1、接着,自定义安装的需要建立一个data文件夹,存放MongoDB相关的数据和日志文件。在data目录下面建立db和log文件夹,通过文件名都知道,一个是放log,一个是放数据库相关的文件。并且在log文件夹下创建一个日志文件MongoDB.log。

2、打开cmd(windows键+r输入cmd)命令行,进入D:\mongodb\bin目录(先输入d:进入d盘然后输入cd d:\mongodb\bin)下输入:
D:/mongodb/bin>mongod --dbpath D:\mongodb\data\db
看到与下图类似信息则说明启动成功


四、MongoDB安装为Windows服务
当mongod.exe被关闭时,mongo.exe 就无法连接到数据库了,因此每次想使用mongodb数据库都要开启mongod.exe程序,所以比较麻烦,此时我们可以将MongoDB设置为windows服务。
1、在d:\mongodb新建文件mongo.config在d:\mongodb新建文件mongo.config,这是MongoDB的配置文件,内容如下:
systemLog:
destination: file
# log文件夹路径
path: D:\MongoDB\data\log\MongoDB.log
storage:
# db文件夹路径
dbPath: D:\MongoDB\data\db
你可以在gitbash上打开mongo.config文件,也可以直接在记事本里面打开它,写入上述内容。(此处我用的是gitbash)

2、用管理员身份打开cmd命令行,进入D:\mongodb\bin目录,安装MongoDB服务,输入以下命令:
mongodb.exe --config D:\mongodb\mongod.config --install
此时logs文件中应生成一个新文件。

3、在管理员权限下输入命令:net start MongoDB,将MongoDB服务加入到WINDOWS,注意服务名一定不能写错.不确定服务名的话,打开任务管理器,如下图查看:

4、关闭服务器

踩坑点:
1、开一个cmd窗口,进入mongodb的bin目录,输入mongo或者mongo.exe,测试失败如图

原因:启动mongodb服务,需要先配置一下你要存储数据的地址,打开cmd命令窗口,进入到mongodb\bin目录下输入:
D:/mongodb/bin>mongod --dbpath D:\mongodb\data\db
如果出现了端口27017等待连接则成功。如果没有这行命令则会导致出现上图这种情况
2、MongoDB安装为Windows服务报错,如图:


参考资料:
https://blog.csdn.net/dengdengda/article/details/51729938
https://jingyan.baidu.com/article/d5c4b52bef7268da560dc5f8.html
https://www.jianshu.com/p/d6c7adfe45cf
https://jingyan.baidu.com/article/ed15cb1b52b8661be2698162.html
http://www.runoob.com/mongodb/mongodb-window-install.html
https://blog.csdn.net/qq_33210798/article/details/74332549
网友评论