windows下的MongoDB安装与安全认证设置

MongoDB安装完后有两种启动方式,我们要以服务方式启动,并设置访问权限

安装完后进入安装目录,新建配置文件 config.cfg

systemLog:
    destination: file
    path: E:\MongoDB\mongod.log
storage:
    dbPath: E:\MongoDB\data
net:
    bindIp: 0.0.0.0
    port: 27017

然后启动一个命令行启动mongod服务:

cd C:\Program Files\MongoDB\Server\3.6\bin
mongod --config="C:\Program Files\MongoDB\Server\3.6\bin\config.cfg"

再另启一个命令行进入mongodb设置用户密码

mongo.exe
use admin db.createUser({user:'admin',pwd:'password',roles:[{role:'root',db:'admin'}]})

# 设置其他数据库
use admin
db.auth('admin','password');
db.createUser({user:'admin',pwd:'password',roles:[{role:'dbAdmin',db:'xxx'},{role:'readWrite',db:'数据库名称'}]});

#角色包括: 1. 数据库用户角色:read、readWrite; 2. 数据库管理角色:dbAdmin、dbOwner、userAdmin; 3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager; 4. 备份恢复角色:backup、restore; 5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase 6. 超级用户角色:root

关闭两个命令行,再启动一个命令行添加windows服务

sc create MongoDB binPath= "\"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe\" --service --auth --config=\"C:\Program Files\MongoDB\Server\3.6\bin\config.cfg\"" DisplayName= "MongoDB Server" start= "auto"
# 划线部分替换为安装目录

# 启动服务
sc start MongoDB

再次访问时需要输入:db.auth(‘admin’,’password’)  完成认证

显示更多

1,297 thoughts on “windows下的MongoDB安装与安全认证设置

  1. My wife and i were now cheerful when Louis managed to round up his studies with the precious recommendations he made through your web site. It’s not at all simplistic to just always be giving away things which usually some others might have been making money from. And we also consider we need the writer to appreciate because of that. The main explanations you made, the straightforward blog navigation, the friendships your site make it easier to foster – it’s everything extraordinary, and it’s really aiding our son and our family reckon that that idea is cool, and that’s incredibly mandatory. Thank you for all the pieces! reputable canadian online pharmacy