MongoDB在windows上的安装

下载安装包并安装

下载地址
https://www.mongodb.com/download-center#community
这里写图片描述

双击下载的安装包,一路next,可以修改路径,主要这个地方不要选

这里写图片描述

mongodb-win32-x86_64-2008plus-ssl-3.6.5-signed.msi这一版本选了会报错,安装不成功

参考文献

https://stackoverflow.com/questions/49721421/mongodb-installation-error-mongodb-setup-wizard-ended-prematurely

我把mongodb安装在了D:\mongodb下

安装后的目录结构

这里写图片描述

运行Mongodb

设置mongodb运行环境

MongoDB requires a data directory to store all data. MongoDB’s default data directory path is the absolute path \data\db on the drive from which you start MongoDB.

mongodb需要一个data目录来存放所有的数据,mongodb的默认数据目录的路径是系统中的绝对路径\data\db

我们在mongodb的安装目录下创建相应的目录D:\mongodb\data\db和D:\mongodb\data\logs

这里写图片描述

启动mongodb

启动命令,注意安装mongodb的路径和data目录的路径

"D:\mongodb\bin\mongod.exe" --dbpath D:\mongodb\data\db\

这里写图片描述

连接到mongodb

重新开一个命令行窗口,进入mongodb的安装目录/bin下面

输入

mongo.exe

连接到mongodb数据库

这里写图片描述

创建mongodb服务

注意下面的命令要在管理员权限下

win10是按下win然后搜索cmd.exe,按下Ctrl + Shift + Enter

这样每次启动mongodb都很麻烦

首先要为mongodb创建一个配置文件,这样我们就不用把启动命令写的很长,只要启动的时候读取配置即可。

配置文件内容mongod.cfg

配置了日志和数据的路径

systemLog:
    destination: file
    path: d:\mongodb\data\log\mongod.log
storage:
    dbPath: d:\mongodb\data\db

我们可以通过下面的命令来把mongodb添加到windows服务中

sc.exe create MongoDB binPath= "\"d:\mongodb\bin\mongod.exe\" --service --config=\"d:\mongodb\mongod.cfg"" DisplayName= "MongoDB" start= "auto"

这里写图片描述

这样我们就可以通过如下命令启动或停止mongodb的服务了

net start MongoDB
net start MongoDB

还可以通过

sc.exe delete MongoDB

删除MongoDB服务

这里写图片描述

参考文献

  1. https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/
  2. https://www.cnblogs.com/iocool/p/6677899.html
  3. https://stackoverflow.com/questions/49721421/mongodb-installation-error-mongodb-setup-wizard-ended-prematurely
  4. https://www.cnblogs.com/inuex/p/4299690.html

猜你喜欢

转载自blog.csdn.net/frankcheng5143/article/details/80504140