Enable Authentication

To enable the authentication, perform the following steps:

  1. Go to the server where MongoDB service has been installed.
  2. Open the Command Prompt as Administrator and execute the command below:
    mongosh --host x.x.xx.x1 --port 27017
  3. Execute the command below:
    MongoDBMongoDB Enterprise> use admin
  4. Make the following changes in the values before execution:
    • user:<username >
    • pwd :<password>
    Note:
    (Choose any password for the corresponding user for MongoDB authentication.)
    MongoDBMongoDB Enterprise> db.createUser(
    {
    user: "<username>",
    pwd: "<password>",
    roles: [ {role: "userAdminAnyDatabase", db: "admin”}, { role: "root", db: "admin" }, "readWriteAnyDatabase" ]
    }
    )
    On successful execution, you will get {ok: 1} message.
  5. Make the following changes in mongod.cfg and refer to the screenshot below.
    Figure 1. MongoDB Installation – Start MongoDB Service (non-HA) (Cont.)
  6. Restart the MongoDB service on the server.
  7. Re-open the Command Prompt as Administrator and open the MongoDB terminal using the following command:
    mongosh --host <IP> --port <Port> -u <username> -p <password> --authenticationDatabase "admin"
    For e.g. - mongosh --host x.x.xx.x --port 27017 -u <username> -p <password> --authenticationDatabase "admin"

    Connection with MongoDB should be successfully established.