MongoDB is a powerful NoSQL database, but managing users, authentication, and backups can sometimes be tricky. This article is a ,y quick cheat sheet for developers, providing essential MongoDB commands tested on a self-managed MongoDB instance running on localhost.
First things first – connecting to MongoDB
Logging into MongoDB from the terminal:
mongo -u 'username' -p 'your-secure-password' --authenticationDatabase admin
If your database user is tied to a specific database (e.g., custom_db
), use:
mongo -u admin -p 'your-secure-password' --authenticationDatabase custom_db
Creating a New User
To create a user with full permissions on a specific database:
use ams;
db.createUser({
user: "user_dev",
pwd: "your-secure-password",
roles: [{ role: "dbOwner", db: "my_db_instance_name" }]
});
If you need a read-only user:
db.createUser({
user: "user_readonly",
pwd: "readonly-password",
roles: [{ role: "read", db: "my_db_instance_name" }]
});
Changing a User’s Password
For security, changing passwords periodically is a good practice. Here’s how to update a user’s password:
use admin;
db.changeUserPassword("admin", "new-secure-password");
For a user in another database:
use different_db;
db.changeUserPassword("user_dev", "new-secure-password");
Listing Users in a Database
To see all users in the my_db_instance_name
database:
use my_db_instance_name;
db.getUsers();
Backing Up a MongoDB Database
To create a compressed backup of your database:
mongodump --host mongodb --port 27017 -u "$DB_USER" -p "$DB_PWD" \
--authenticationDatabase "my_db_instance_name" --db "my_db_instance_name" --archive=backup-$(date +%F).gz --gzip
This command dumps the my_db_instance_name
database into a .gz
file with a timestamp.
Restoring a MongoDB Backup
Restoring a backup is just as easy:
mongorestore --host mongodb --port 27017 -u "$DB_USER" -p "$DB_PWD" \
--authenticationDatabase "my_db_instance_name" --db "my_db_instance_name" --gzip --archive=backup-YYYY-MM-DD.gz
Make sure to replace YYYY-MM-DD
with the correct backup file date.
Running MongoDB in Docker
If you’re using Docker, running MongoDB with authentication enabled requires the --auth
flag. Below is an example of how to run MongoDB in a Docker container with authentication, persistent storage, and proper environment variables:
version: '3.8'
services:
mongodb:
image: mongo:latest
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: your-secure-password
volumes:
- mongo-data:/data/db
command: ["mongod", "--auth"]
ports:
- "27017:27017"
volumes:
mongo-data:
This setup ensures that MongoDB retains data even if the container is restarted or removed. Start MongoDB with docker compose:
docker-compose up -d
You can then connect to it using:
mongo -u admin -p 'your-secure-password' --authenticationDatabase admin
Additional Useful MongoDB Commands
Listing All Databases
To see all available databases:
show dbs
Listing All Collections in a Database
To check available collections:
use my_db_instance_name;
show collections
Checking Storage Statistics for a Collection
To analyze how much space a collection is using:
db.my_collection.stats()
Leave a Reply
You must be logged in to post a comment.