本篇文章给大家分享的是有关MongoDB中怎么实现用户管理,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
● 条件一:服务器启动的时候打开授权认证。
● 条件二:需要配置用户名和密码。
但是需要明确的是,如果要想配置用户名和密码,一定是针对一个数据库的,例如现有要创建的是stone数据库用户,那么就必须切换到stone数据库上。
范例:执行用户的创建
任何用户都必须具备一个自己的操作角色:read、readWrite
> use admin
switched to db admin
> db.createUser({
... "user":"root",
... "pwd":"root",
... "roles":[{"role":"root","db":"admin"}]
... })
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> use hr
switched to db hr
> use hr
switched to db hr
> db.createUser({
... "user":"stone",
... "pwd":"stone",
... "roles":[{"role":"readWrite","db":"hr"}]
... })
Successfully added user: {
"user" : "stone",
"roles" : [
{
"role" : "readWrite",
"db" : "hr"
}
]
}
那么现在就表示成功的创建了stone用户。那么如果要想让次用户名起作用,则必须以授权的方式打开MongoDB服务。修改MongoDB的参数文件。
范例:修改参数文件,增加授权启动
[root@D2-LZY245 ~]# vim /usr/local/mongodb/mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
auth=true
port=27017
fork=true
重启数据库:
> use admin
switched to db admin
> db.shutdownServer();
[root@D2-LZY245 ~]# mongod -f /usr/local/mongodb/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 25987
child process started successfully, parent exiting
[root@D2-LZY245 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
> use stone
switched to db stone
> db.emp.find();
Error: error: {
"ok" : 0,
"errmsg" : "not authorized on stone to execute command { find: \"emp\", filter: {} }",
"code" : 13,
"codeName" : "Unauthorized"
}
此时增加了一个验证的启动模式,发现依然可以在不输入用户名和密码的前提下进行登录,并且也可以直接进行数据库的切换操作。但是在使用数据库集合的时候出现了错误提示。
范例:登录数据库的时候使用用户名和密码
[root@D2-LZY245 ~]# mongo -u "stone" -p "stone" --authenticationDatabase "hr"
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
或者:
[root@D2-LZY245 ~]# mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
> use hr
switched to db hr
> db.auth("stone","stone")
1
此时就表示成功的实现了用户的登录操作。
范例:修改密码
[root@D2-LZY245 ~]# mongo -u root -p 'root' --authenticationDatabase 'admin'
> db.changeUserPassword("stone","stone")
范例:查看用户及角色
> use admin
switched to db admin
> db.system.users.find();
{ "_id" : "test.stone", "user" : "stone", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "boSLJ2acqh503AW0MZO7yA==", "storedKey" : "Y7zaryoqOiedeKsVs61SrAMAK8c=", "serverKey" : "+wa0fO2hwNrLRIuVmtJMvR1AJv0=" } }, "roles" : [ { "role" : "readWrite", "db" : "hr" } ] }
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "NmiMDfzuCnkO1CRKrVUV9A==", "storedKey" : "rD6NaqsfON8h0YMNv6idPKUpac0=", "serverKey" : "9MXDynhFSNmHEkMIMfxnSKdQPio=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "hr.stone", "user" : "stone", "db" : "hr", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "69NLL2oWTg0OehrBwytbsw==", "storedKey" : "LRmPZjHZorxhfWA+RS6SgzLbev4=", "serverKey" : "59xkCl7qKK+HrKrlK5QMdGx94U8=" } }, "roles" : [ { "role" : "readWrite", "db" : "hr" } ] }
以上就是MongoDB中怎么实现用户管理,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。