MongoDB Database
Table of contents
- MongoDB Configuration
MongoDB Configuration
This directory contains the MongoDB configuration and seed data for the MEAN Stack Contacts application.
Initialization Script
The main initialization script (init-mongo.sh
) performs two primary tasks:
- Creates a database user with appropriate permissions
- Seeds the database with initial data (users and contacts)
User Creation
mongosh $MONGO_DB -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval "db.createUser({ user: '$MONGO_DB_USERNAME', pwd: '$MONGO_DB_PASSWORD', roles: [{ role: 'readWrite', db: '$MONGO_DB'}]})" --authenticationDatabase admin
Data Seeding
The script seeds the database with initial data:
- One user account with login credentials
- Three sample contacts
db.users.insertMany([
{
"_id": ObjectId("6759daeb097c7d69426b7649"),
"create_date": new Date("2024-12-11T19:29:19.895Z"),
"username": "nitin27may@gmail.com",
"email": "nitin27may@gmail.com",
"password": "$2a$10$nc0yO2eeCDOLg6sObsAHfuXQY8NnCrhHz5GbkmPYsAGLsQoSZa.qm",
"firstName": "NItin",
"lastName": "Singh"
}
]);
db.contacts.insertMany([
{
"_id": ObjectId("6759daeb097c7d69426b7641"),
"firstName": "Nitin",
"lastName": "Singh",
"mobile": "9876543243",
"email": "nitin27may@gmail.com",
"city": "Mumbai",
"postalCode": "421201",
"create_date": new Date()
},
// Additional contacts...
]);
Default User Credentials
The seed data creates a default user with the following credentials:
Username: nitin27may@gmail.com
Password: P@ssword#321
Environment Variables
The MongoDB container uses the following environment variables:
MONGO_INITDB_ROOT_USERNAME
: Root username for MongoDBMONGO_INITDB_ROOT_PASSWORD
: Root password for MongoDBMONGO_DB_USERNAME
: Username for the application databaseMONGO_DB_PASSWORD
: Password for the application databaseMONGO_DB
: Database name
These variables should be defined in the root .env
file of the project.
Docker Configuration
The MongoDB container uses the official MongoDB image with custom initialization:
FROM mongo
COPY init-db.d /docker-entrypoint-initdb.d
Volumes
The Docker Compose configuration maps the following volumes:
./mongo/init-db.d/:/docker-entrypoint-initdb.d/
: Initialization scripts./mongo/db:/data/db
: Data persistence
Data Persistence
The MongoDB data is persisted in the ./mongo/db
directory. This ensures that your data is not lost when the container is stopped or restarted.
Connecting to MongoDB
From Docker Containers
Other containers can connect to MongoDB using the following connection string:
mongodb://${MONGO_DB_USERNAME}:${MONGO_DB_PASSWORD}@database:27017/${MONGO_DB_DATABASE}?authSource=admin
From Host Machine
You can connect to MongoDB from your host machine using:
mongodb://${MONGO_DB_USERNAME}:${MONGO_DB_PASSWORD}@localhost:27017/${MONGO_DB_DATABASE}?authSource=admin
Using MongoDB Compass or Robo 3T
To connect using GUI tools like MongoDB Compass or Robo 3T:
- Host:
localhost
- Port:
27017
- Authentication:
Username/Password
- Username: Value of
MONGO_DB_USERNAME
from your.env
file - Password: Value of
MONGO_DB_PASSWORD
from your.env
file - Authentication Database:
admin
- Database: Value of
MONGO_DB_DATABASE
from your.env
file
Troubleshooting
Permission Issues
If you encounter permission issues with the MongoDB data directory, run:
sudo chown -R $USER:$USER ./mongo/db
Connection Issues
If you cannot connect to MongoDB, check:
- The MongoDB container is running:
docker ps
- Environment variables are set correctly in
.env
- Ports are not blocked by a firewall
- No other process is using port 27017
Initialization Script Issues
If the initialization script fails, check:
- The script has proper permissions:
chmod +x ./mongo/init-db.d/init-mongo.sh
- Line endings are set to LF:
git config --global core.autocrlf input
- No syntax errors in the script
Customizing Seed Data
To modify the seed data:
- Edit the
init-mongo.sh
file in theinit-db.d
directory - Rebuild and restart the containers:
docker-compose down docker-compose -f docker-compose.nginx.yml up --build