用户及权限管理
用户及权限管理
注意
验证库: 建立用户时use到的库,在使用用户时,要加上验证库才能登陆。
远程登录,必须要写认证库名,否则不能连接
ip port user password auth_dbname
简单理解:建用户时,要先选择一个认证库,记住!然后链接的时候,加上这个认证库。否则连不上
对于管理员用户,必须在admin下创建.
1. 建用户时,use到的库,就是此用户的验证库
2. 登录时,必须明确指定验证库才能登录
3. 通常,管理员用的验证库是admin,普通用户的验证库一般是所管理的库设置为验证库
比如: user:simon 管理---> db:simon_db
那么建立simon用户时,就把simon_db设置为simon的认证库。生产环境,不要使用默认test库作为认证库。
4. 如果直接登录到数据库,不进行use,默认的验证库是test,不是我们生产建议的.
5. 从3.6 版本开始,不添加bindIp参数,默认不让远程登录,只能本地管理员登录。
总结:认证库,bind IP
用户创建语法
use admin
db.createUser # 建用户同时授权
{
user: "<name>",
pwd: "<cleartext password>",
roles: [
{ role: "<role>",
db: "<database>" } | "<role>", #MongoDB权限级别是库
...
]
}
基本语法说明:
user:用户名
pwd:密码
roles:
role:角色名
db:作用对象
role:root, readWrite,read
验证数据库:
mongo -u oldboy -p 123 10.0.0.53/oldboy
用户管理例子
创建超级管理员:管理所有数据库(必须use admin再去创建)
$ mongo
use admin --》 选择认证库
use admin
db.createUser(
{
user: "root",
pwd: "root123",
roles: [ { role: "root", db: "admin" } ]
}
)
> use admin
switched to db admin
>
> db.createUser(
... {
... user:"root",
... pwd:"w",
... roles: [ { role:"root", db: "admin" } ]
... })
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
>
验证用户
db.auth('root','w') # 其实就是即时生效
> db.auth("root","w")
1
>
配置文件中,加入以下配置
security:
authorization: enabled
重启mongodb
mongod -f /mongodb/conf/mongo.conf --shutdown
mongod -f /mongodb/conf/mongo.conf
登录验证
mongo -uroot -proot123 admin
mongo -uroot -proot123 10.0.0.53/admin
# 本地登录,不用加认证库
[mongod@mysql-node01 ~]$ mongo -uroot -p
MongoDB shell version v3.6.20
Enter password:
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("29a7788e-6831-4ce2-a0d7-ca34b09444f9") }
MongoDB server version: 3.6.20
>
>
# 远程登录 不加认证库,无法登录
[mongod@mysql-node01 ~]$ mongo -uroot -pw 10.0.50.61
MongoDB shell version v3.6.20
connecting to: mongodb://10.0.50.61:27017/test?gssapiServiceName=mongodb
2020-10-24T11:17:13.019+0800 E QUERY [thread1] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:275:13
@(connect):1:6
exception: connect failed
[mongod@mysql-node01 ~]$
# 必须加认证库,才能链接,登录后就在admin库下
[mongod@mysql-node01 ~]$ mongo -uroot -pw 10.0.50.61/admin
MongoDB shell version v3.6.20
connecting to: mongodb://10.0.50.61:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b24508d4-eb5b-4b58-889b-00297a69688b") }
MongoDB server version: 3.6.20
>
> db
admin
>
或者
mongo
use admin
db.auth('root','root123')
查看用户
use admin
db.system.users.find().pretty()
>
> use admin
switched to db admin
>
> show tables;
system.users
system.version
>
> db.system.users.find().pretty()
{
"_id" : "admin.root",
"userId" : UUID("e8d60f33-d830-40a9-a0bd-d00faa0d6ca3"),
"user" : "root",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "8U5njsQGK/2Iqj63b8A8eQ==",
"storedKey" : "N8tFdsh8pSannkCs0DyFR5nYHc8=",
"serverKey" : "ZK4OdobETkcCeZtPO8dqNP6rjXQ="
}
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
>
>
创建应用用户
use simon_db
db.createUser(
{
user: "simon",
pwd: "simon",
roles: [ { role: "readWrite" , db: "simon_db" } ]
}
)
> use simon_db
switched to db simon_db
>
>
> db.createUser(
... {
... user:"simon",
... pwd:"simon",
... roles: [ { role:"readWrite",db:"simon_db"}]
... })
Successfully added user: {
"user" : "simon",
"roles" : [
{
"role" : "readWrite",
"db" : "simon_db"
}
]
}
>
mongo -uapp01 -papp01 app
# 本地登录
[mongod@mysql-node01 ~]$ mongo -usimon simon_db
MongoDB shell version v3.6.20
Enter password:
connecting to: mongodb://127.0.0.1:27017/simon_db?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("475eca63-9dab-4033-80b2-592d408e3902") }
MongoDB server version: 3.6.20
>
>
[mongod@mysql-node01 ~]$ mongo -u simon -p --authenticationDatabase simon_db
MongoDB shell version v3.6.20
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=simon_db&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("99c66c4a-e715-4691-abda-22209e04aa72") }
MongoDB server version: 3.6.20
>
# 远程登录
[mongod@mysql-node01 ~]$ mongo -usimon 10.0.50.61:27017/simon_db
MongoDB shell version v3.6.20
Enter password:
connecting to: mongodb://10.0.50.61:27017/simon_db?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("547aefc8-9ef4-4ff0-ac0e-0f0363aaf159") }
MongoDB server version: 3.6.20
>
>
[mongod@mysql-node01 ~]$ mongo -u simon -p --host 10.0.50.61:27017 --authenticationDatabase simon_db
MongoDB shell version v3.6.20
Enter password:
connecting to: mongodb://10.0.50.61:27017/?authSource=simon_db&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d464f703-2c19-4650-8b7f-f02e448cb974") }
MongoDB server version: 3.6.20
>
>
修改密码
use admin
db.changeUserPassword("username", "xxx")
db.updateUser("username",{pwd:"changepass1"});
# 修改密码和用户信息
db.runCommand(
{
updateUser:"username",
pwd:"xxx",
customData:{title:"xxx"}
}
修改权限
# 注:updateuser 它是完全替换之前的值
db.updateUser("username",{roles:[ {role:"read",db:"testDB"} ]})
# 如果要新增或添加roles,则使用
db.grantRolesToUser()
db.revokeRolesFromUser()
# 添加权限
db.grantRolesToUser("username",[
{role:"readWrite", db:"testDB"},
{role:"read", db:"testDB"}
]
)
# 回收权限
db.revokeRolesFromUser("username",[
{role:"read", db:"testDB"}
]
)
查询mongodb中的用户信息
mongo -uroot -proot123 10.0.0.53/admin
db.system.users.find().pretty()
> use admin
> db.system.users.find({user:"simon"}).pretty()
{
"_id" : "simon_db.simon",
"userId" : UUID("da7fb905-13ca-415f-9759-b1fbc4675e62"),
"user" : "simon",
"db" : "simon_db",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "mpqF1ip6lg/aczK4Z6MLXg==",
"storedKey" : "4anUFCBtgwuiwFJVnnwSJAlxQQE=",
"serverKey" : "748WFqoSbNCX/yCY448K2V2nWtQ="
}
},
"roles" : [
{
"role" : "readWrite",
"db" : "simon_db"
}
]
}
>
删除用户(root身份登录,use到验证库)
删除用户
# 注意,删用户前,先查询用户所属那个认证库,切换到对应认证库,然后再删除。
mongo -uroot -proot123 10.0.0.53/admin
use oldboy1
db.dropUser("app02")
> use simon_db
switched to db simon_db
> db.dropUser("simon")
true
>
> use admin
switched to db admin
> db.system.users.find(user:"simon")
2020-10-24T11:42:18.614+0800 E QUERY [thread1] SyntaxError: missing ) after argument list @(shell):1:25
>
db.system.users.remove({user:"hejian"})
用户管理注意事项
1. 建用户要有验证库,管理员admin,普通用户是要管理的库
2. 登录时,注意验证库
mongo -uapp01 -papp01 10.0.0.51:27017/oldboy
3. 重点参数
net:
port: 27017
bindIp: 10.0.0.51,127.0.0.1
security:
authorization: enabled