MongoDB权限设置

添加管理员账号
 
    
> use admin
switched to db admin
> db.createUser({user:'admin',pwd:'admin',roles:[{role:'dbAdminAnyDatabase',db:'admin'}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "dbAdminAnyDatabase",
"db" : "admin"
}
]
}

开启用户权限认证
 
    
[root@tbtravel local]# mongod --auth

管理员登录
 
    
[root@tbtravel ~]# mongo 127.0.0.1:27017/admin -uadmin -padmin
MongoDB shell version: 3.0.9
connecting to: 127.0.0.1:27017/admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "EY/F2DffzBtDQxEkZ8owOQ==", "storedKey" : "ED8eeiKZZcaFjUW/WhpGdnpeGjY=", "serverKey" : "L2L5FeoSPGVMCWxEPFgKwRcDARc=" } }, "roles" : [ { "role" : "dbAdminAnyDatabase", "db" : "admin" } ] }

添加普通用户
 
    
> use test
switched to db test
> db.createUser({user:'test',pwd:'test',roles:[{role:'readWrite',db:'test'}]})
Successfully added user: {
"user" : "test",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}
> db.auth('test','test')
1
db.auth('test','test'),输出一个结果值为1,说明这个用户匹配上了,如果用户名、密码不对,会输出0

普通用户登录
 
     
[root@tbtravel ~]# mongo 127.0.0.1:27017/test -utest -ptest
MongoDB shell version: 3.0.9
connecting to: 127.0.0.1:27017/test
> db
test
> show collections

 

猜你喜欢

转载自blog.csdn.net/u022812849/article/details/51303921