MongoDB User Management
User management in MongoDB involves operations such as user creation, role assignment, authentication, and login.
The following is a detailed explanation, including how to use MongoDB Shell (mongo) or MongoDB Compass to manage users.
Managing users with MongoDB Shell (mongo)
The following is a detailed explanation of user management using MongoDB Shell (mongosh), including specific steps for creating users, assigning roles, authentication, and login.
1. Connect to MongoDB
First, open your terminal and use the mongosh command to connect to the MongoDB server:
mongosh --host --port
Explanation:
mongosh: starts the MongoDB Shell command-line tool.--host: specifies the hostname or IP address of the MongoDB server.: the hostname of the MongoDB server (such aslocalhost) or IP address (such as127.0.0.1)。
--port: specifies the port number of the MongoDB server.: the port number on which the MongoDB server listens, the default port is27017。
** 2. Switch to the target database**
In MongoDB, users are created for a specific database. Use the use command to switch to the database where you want to create the user:
use
- database_name - is the database to switch to.
** 3. Create a user**
Use the db.createUser command to create a user and assign roles.
For example, create a user named testuser with password password123, and assign the readWrite and dbAdmin roles:
db.createUser({
user: "testuser",
pwd: "password123",
roles: [
{ role: "readWrite", db: "" },
{ role: "dbAdmin", db: "" }
]
})
4. Verify the user
After creating a user, you can use the db.auth command to verify the user's identity:
db.auth("testuser", "password123")
5. Enable authentication
To ensure that only authenticated users can access MongoDB, you need to enable authentication.
Edit the MongoDB configuration file mongod.conf and add the following content:
security:
authorization: "enabled"
Then restart the MongoDB service to apply the changes.
** 6. Log in as a user**
After enabling authentication, you need to connect to MongoDB using the created user identity:
mongosh --host --port -u "testuser" -p "password123" --authenticationDatabase ""
7. Delete a user
Use the db.dropUser command to delete a specified user.
For example, delete the user named testuser:
db.dropUser("testuser")
Example operations
The following is a complete example operation flow:
Start MongoDB Shell and connect to the server:
mongosh --host localhost --port 27017
Switch to the testdb database:
use testdb
Create the testuser user:
db.createUser({
user: "testuser",
pwd: "password123",
roles: [{ role: "readWrite", db: "testdb" }]
})
Enable authentication and restart the MongoDB instance
Edit the mongod.conf file and add the following content:
security:
authorization: "enabled"
Restart the MongoDB service:
sudo systemctl restart mongod
Connect with authentication using the testuser user:
mongosh --host localhost --port 27017 -u "testuser" -p "password123" --authenticationDatabase "testdb"
Delete the testuser user:
db.dropUser("testuser")