赞
踩
1)3.x 步骤
注:4.x 与3.x基本相同,但是无需指定存储路径,在安装的时候,要求配置(举例为windows环境下)
2 )4.x 步骤
第一步创建超级管理用户
use admin
db.createUser({
user:'admin', pwd:'123456', roles:[{role:'root',db:'admin'}]
})
注意这里一定要是 use admin
, 而且db也是admin
第二步修改 Mongodb 数据库配置文件
C:\Program Files\MongoDB\Server\4.0\bin\mongod.cfg
这里是默认安装路径windows下的配置:
# 开启权限认证
security:
authorization: enabled
mac下:路径按照文档上的来
systemLog:
destination: file
path: "/usr/local/mongodb/log/mongod.log"
logAppend: true
storage:
dbPath: "/Users/johnny/data/db"
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27017
security:
authorization: enable
第三步重启 mongodb 服务
mongod --config /usr/local/mongodb/etc/mongod.conf
mongod -f /usr/local/mongodb/etc/mongod.conf
第四步用超级管理员账户连接数据库
mongo eggcms -u eggadmin -p 123456
mongo localhost:27017/eggcms -u eggadmin -p 123456
第五步给 eggcms 数据库创建一个专属用户
只能访问 eggcms 不能访问其他数据库
use eggcms
db.createUser({
user: "eggadmin", pwd: "123456", roles: [ { role: "dbOwner", db: "eggcms" } ]
})
show users;
查看当前库下的用户db.dropUser("eggadmin")
删除用户db.updateUser( "admin", {pwd:"password"});
修改用户密码db.auth("admin","password");
权限认证,如果没有权限可以用权限的用户的权限进行操作,参数是:角色,密码mongo eggcms -u eggadmin -p 123456
使用当前用户eggadmin连接指定数据库eggcmsmongodb://username:pwd@localhost:27017/dbname
const url = 'mongodb://admin:123456@localhost:27017/eggcms';
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。