MongoDB is a popular NoSQL database that is frequently used in development and production environments. When working with MongoDB in a Dockerized setup using Docker Compose, you may encounter the “Could Not Find User” error, which can be frustrating to deal with. In this article, we will learn how to fix the Could Not Find User Error in MongoDB with Docker Compose.
Table of Contents
Error Description
The “Could Not Find User” error in MongoDB typically occurs when attempting to authenticate against a specific user, and MongoDB is unable to locate that user in the specified context.
You will encounter an error resembling the one depicted in the screenshot when attempting to establish a connection to your MongoDB database.
How to Fix Could Not Find User Error in MongoDB with Docker Compose
Let’s discuss the steps to address the “Could Not Find User” error in MongoDB. Follow the step-by-step steps to resolve the error.
Create Mongo User by Script
Compose a JavaScript script to generate a MongoDB database user during the Docker build process. This script will initiate the creation of a user with the given password, associating it with the specified database name upon startup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // mongo-init.js db.createUser( { user: "stackblogger", pwd: "sb1234", roles: [ { role: "readWrite", db: "sbdb" } ] } ); |
The above script will create a new user with specified credentials and roles. Here’s a brief explanation:
user: "stackblogger"
: Sets the username for the new MongoDB user as “stackblogger”. You can change it with your username.pwd: "sb1234"
: Specifies the password for the new user as “sb1234”. You can change it to your password.roles
: Defines an array of roles assigned to the user. In this case, there’s one role specified.{ role: "readWrite", db: "sbdb" }
: Assigns the “readWrite” role to the user for the “sbdb” (you can change the database name here) database. The “readWrite” role grants the user the ability to both read and write data in the specified database.
So, in summary, this script creates a MongoDB user named “stackblogger” with the password “sb1234” and grants them read and write permissions in the “sbdb” database.
Update Docker Compose Script
Modify the Docker Compose script to execute the MongoDB user creation script on startup, ensuring that the user is created in the MongoDB database once the Docker build process is finished.
Include the following line in the volumes section of your docker-compose script.
1 2 3 | - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro |
Full example of the docker-compose script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # // docker-compose.yml version: '3.3' services: db: image: mongo:latest environment: MONGO_INITDB_ROOT_USERNAME: stackblogger MONGO_INITDB_ROOT_PASSWORD: sb1234 MONGO_INITDB_DATABASE: sbdb ports: - 27017:27017 volumes: - ./mongo-data:/data/db - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro # include the line here restart: unless-stopped volumes: mongo-data: |
Note: Please ensure that the Docker Compose script includes the identical username, password, and database specified in the MongoDB user creation script.
This Docker Compose script defines a MongoDB service with the latest version, specifying initial database setup parameters. It includes a volume for persistent data storage and executes a MongoDB initialization script (mongo-init.js
) during startup, creating a root user with the username “stackblogger,” password “sb1234,” and the database “sbdb.” The MongoDB service is accessible on port 27017.
Rebuild the Docker
Remove the MongoDB persistent data storage from the directory and rebuild the container using the following commands in sequence.
Remove the MongoDB Persistent Data
1 2 3 | rm -rf mongo-data/ |
Rebuild the container
1 2 3 | docker compose up --build -d db |
The MongoDB container has been initiated successfully. Now, proceed to connect to the database using the credentials.
Connect to MongoDB
Connect to the MongoDB database on port 27017 using the credentials specified in the MongoDB initialization script.
Excellent! The connection to MongoDB has been successfully established!
Frequently Asked Questions
This error occurs when MongoDB is unable to locate a specified user during an authentication or authorization process.
Check if the user is created, verify credentials, ensure proper database context, and confirm the user’s roles and privileges.
Ensure that the user has the appropriate roles, such as “readWrite” or other relevant roles, to perform the required operations in the specified database.
Conclusion
Create a script to create a Mongo user on startup, delete any existing Mongo persistent data, rebuild the container, and just establish the connection. That should resolve the issue.
Checkout the importance of indexing in Mongo database.