时间:2021-07-01 10:21:17 帮助过:42人阅读
格式
> db.createUser(
{
user:"<name>",
pwd:"<password>",
customData:{<any information>,roles:[{role:"<role>",db:"<database>"}]}
)
read、readWrite;
dbAdmin、dbOwner、userAdmin;
clusterAdmin、clusterManager、clusterMonitor、hostManager;
backup、restore;
readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
这里还有几个角色间接或直接提供了系统超级用户的访问
(dbOwner 、userAdmin、userAdminAnyDatabase)
# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
> use admin
switched to db admin
> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "3.4" }
{ "_id" : "authSchema", "currentVersion" : 5 }
解决方法:
> db.system.version.update({"_id":"authSchema"},{$set:{"currentVersion":3}})
# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
> use admin
switched to db admin
> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "3.4" }
> db.system.version.insert({"_id":"authSchema","currentVersion":3})
WriteResult({ "nInserted" : 1 })
> db.system.version.find()
{ "_id" : "featureCompatibilityVersion", "version" : "3.4" }
{ "_id" : "authSchema", "currentVersion" : 3 }
> db.createUser({user:"admin",pwd:"123456",roles:[{"role":"userAdminAnyDatabase","db":"admin"},{"role":"readWrite","db":"test"}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "readWrite",
"db" : "test"
}
]
}
# bindIp: 127.0.0.1 那一行可以注释掉,也可以修改为如下所示:
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
# 远程连接数据库验证,旧版本为auth = true
security:
authorization: enabled
systemctl restart mongod
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT
D:\Dev\Cmder
λ mongo 192.168.56.102:27017/admin -u whoami -p
MongoDB shell version v3.6.2
Enter password:
connecting to: mongodb://192.168.56.102:27017/admin
MongoDB server version: 3.4.10
>
MongoDB Users
标签:mat image int 分享 state 图片 blog stat monitor