MongoDB 数据库 用户管理
- 前言
- 一、创建用户
- 二、登录数据库
前言
本环境是基于 Centos 7.8 系统构建mongodb-enterprise-4.2.8学习环境
具体构建,请参考 mongodb-enterprise-4.2.8 环境构建
一、创建用户
MongoDB Enterprise myRS:PRIMARY> db.createUser(
... {
... user: 'root',
... pwd: 'admin',
... roles: [{role: 'root',db: 'admin'}]
... }
... )
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
# 查看用户
MongoDB Enterprise myRS:PRIMARY> db.system.users.find()
{ "_id" : "admin.root", "userId" : UUID("7f2f01f7-1faa-4152-8053-9f9c6c85d562"), "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "nNWoXtGNClw3xIki0KvKeA==", "storedKey" : "4VHG+4x4PlYNUTSXFuRF11VzkBA=", "serverKey" : "wCow20yodjdq0w69SWVu1AppiD8=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "uf0S2xTQ1OGPZ7qDG0mNM2tw9Cx91rLUCpBEXw==", "storedKey" : "ZYYEYUOyuuhCUTKO6WvfwQNd7xSjccR8CWN3ubEU3tQ=", "serverKey" : "4GQmYdKEuWKVGeNXtCNL7cp+iZkY/9OzrSFrwIY58iU=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
MongoDB Enterprise myRS:PRIMARY> show users
{
"_id" : "admin.root",
"userId" : UUID("7f2f01f7-1faa-4152-8053-9f9c6c85d562"),
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
# 验证用户是否可用
MongoDB Enterprise myRS:PRIMARY> db.auth('root','admin')
1
#启用用户认证
[root@node01 ~]# vim /etc/mongod.conf
security:
authorization: enabled
[root@node01 ~]# systemctl restart mongod
二、登录数据库
方法一:
方法二