SoFunction
Updated on 2025-03-04

Steps to configure username and password in MongoDB

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 usedmongoCommand 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" }]
})
  • adminUserIt is the administrator username.adminPasswordIt is the password of the administrator user.
  • rolesThe administrator user has been specifiedadminPermissions 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 calledmydatabasedatabase) 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" }]
})
  • dbUserIt is the database username,dbPasswordIt is the password of the database user.
  • rolesThe user specifiedmydatabaseRole 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 dbUserand-p dbPasswordThe user name and password are specified respectively.
  • --authenticationDatabase mydatabaseSpecifies the database for authentication, here ismydatabase, because the user we created is inmydatabaseUnder 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!