To configure a username and password in MongoDB, follow these steps:
1. Start MongoDB service and connect
First make sure the MongoDB service is running. Then, use the MongoDB client to connect to the MongoDB server. Can be usedmongo
Command line tools or MongoDB Compass to connect.
Connect using the mongo command line tool:
Open a command prompt (Windows) or terminal (Linux/macOS) and enter the following command:
mongo
This will connect to the default MongoDB server (usually locallocalhost:27017
)。
2. Create an administrator user (optional)
If you don't have an administrator user, you first need to create an administrator user that manages access and permissions to the MongoDB database. Here is an example of creating an administrator user:
use admin ({ user: "adminUser", pwd: "adminPassword", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
-
adminUser
It is the administrator username.adminPassword
It is the password of the administrator user. -
roles
The administrator user has been specifiedadmin
Permissions on the database, here isuserAdminAnyDatabase
, allowing users in any database to be managed.
3. Create a database user
Next, for your specific database (such as one calledmydatabase
database) create user. First switch to the database where you want to create the user (which will be created if it does not exist):
use mydatabase
Then, create a user and grant the appropriate role:
({ user: "dbUser", pwd: "dbPassword", roles: [{ role: "readWrite", db: "mydatabase" }] })
-
dbUser
It is the database username,dbPassword
It is the password of the database user. -
roles
The user specifiedmydatabase
Role permissions on the database, here isreadWrite
, allows reading and writing of data in this database.
4. Exit and reconnect
After the user creation is completed, exit the current MongoDB connection and reconnect to the MongoDB server using the newly created user:
exit
Then, reconnect with the newly created user:
mongo -u dbUser -p dbPassword --authenticationDatabase mydatabase
-
-u dbUser
and-p dbPassword
The user name and password are specified respectively. -
--authenticationDatabase mydatabase
Specifies the database for authentication, here ismydatabase
, because the user we created is inmydatabase
Under the database.
5. Configure MongoDB to allow authentication
If the MongoDB server does not enable authentication by default, you need to modify the MongoDB configuration fileAnd restart the MongoDB service.
existAdd or modify the following lines to the file:
security: authorization: enabled
Save and close the file. Then, restart the MongoDB service for the changes to take effect.
Things to note
- Security: Make sure your password is strong enough and don't hardcode your password in the application.
- Permission Management: Assign appropriate roles and permissions to users according to actual needs.
- Production environment: The above steps are suitable for development and testing environments. In production environments, more detailed configuration and management are recommended based on security best practices.
Summarize
This is the end of this article about MongoDB configuring username and password. For more related content on MongoDB configuring username and password, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!