Introduction
When setting up a database for production use, security is paramount. MongoDB provides robust features to ensure that your data remains secure through authentication and authorization mechanisms. This tutorial will guide you through the process of enabling username and password authentication for a MongoDB instance.
Prerequisites
- Basic understanding of MongoDB.
- Access to a server where MongoDB is installed.
- Administrative privileges on the server to modify configurations and restart services.
Step-by-step Guide to Secure MongoDB with Authentication
1. Start MongoDB without Authentication
First, ensure that your MongoDB server is running without authentication. This allows you to create user accounts in the database.
mongod --dbpath /data/db
Replace /data/db
with your actual data directory path if it’s different.
2. Connect to the MongoDB Shell
Open a new terminal window and connect to your MongoDB instance:
mongo
3. Create an Administrative User
In the MongoDB shell, switch to the admin
database and create a user with administrative privileges. This user will be used for managing other users and databases.
use admin
db.createUser(
{
user: "myAdminUser",
pwd: "securePassword123", // Use a strong password in practice
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
4. Stop MongoDB and Restart with Authentication Enabled
Exit the MongoDB shell by typing exit
or pressing Ctrl+C
. Now, stop the server and restart it with authentication enabled.
mongod --auth --dbpath /data/db
The --auth
flag ensures that all users must authenticate before accessing any data in the database.
5. Authenticate as an Administrative User
Connect to MongoDB again:
mongo
Authenticate using the administrative user credentials you created earlier:
use admin
db.auth("myAdminUser", "securePassword123")
You should see a 1
if authentication is successful, indicating that your authentication setup works.
6. Create Additional Users
Now you can create other users with specific roles for different databases or access levels.
For example, to create a user who can read and write to a database called myDatabase
:
use myDatabase
db.createUser(
{
user: "myRegularUser",
pwd: "anotherSecurePassword", // Use a strong password in practice
roles: [ { role: "readWrite", db: "myDatabase" } ]
}
)
Configuration File (Optional)
Instead of using the --auth
flag every time you start MongoDB, you can enable authentication directly from the configuration file.
-
Open your MongoDB configuration file (
/etc/mongod.conf
by default). -
Locate the
security
section and uncomment or add the following line:security: authorization: enabled
-
Save the changes and restart MongoDB:
sudo service mongod restart
Best Practices
- Use strong, complex passwords for all user accounts.
- Regularly review and update user roles to adhere to the principle of least privilege.
- Consider using environment variables or a secure vault service to manage database credentials securely.
Conclusion
By following these steps, you have successfully secured your MongoDB instance with username and password authentication. This setup helps protect sensitive data by ensuring that only authenticated users can access your databases.