In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is to share with you about how to achieve user management in MongoDB, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
● condition 1: turn on authorization authentication when the server starts.
● condition 2: you need to configure a user name and password.
To be clear, however, if you want to configure a user name and password, it must be for a database. For example, if you are creating a stone database user, you must switch to the stone database.
Example: perform the creation of a user
Any user must have their own operational roles: 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"
}
]
}
So now it means that the stone user has been successfully created. Then if you want the secondary user name to work, you must open the MongoDB service in an authorized manner. Modify the parameter file of MongoDB.
Example: modify the parameter file to add authorization startup
[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
Restart the database:
> 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"
}
At this time, an authentication startup mode is added, and it is found that you can still log in without entering a user name and password, and you can also switch the database directly. However, an error occurred when using the database collection.
Example: use a user name and password when logging in to the database
[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
Or:
[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")
one
At this point, it means that the login operation of the user has been successfully implemented.
Example: change password
[root@D2-LZY245] # mongo-u root-p 'root'-- authenticationDatabase' admin'
> db.changeUserPassword ("stone", "stone")
Example: view users and roles
> 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": "Y7zaryoqOieKsVs61SrAMAK8c =", "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": "69NLL2oWTg0OehrBwytbswung =", "storedKey": "LRmPZjHZorxhfWA+RS6SgzLbev4=", "serverKey": "59xkCl7qKKroomHrKrlK5QMdGx94U8 ="}}, "roles": [{"role": "readWrite" "db": "hr"}]}
The above is how to achieve user management in MongoDB. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.