Overview
The Resume Generator uses MongoDB for data persistence. This guide helps you resolve common MongoDB connection issues, particularly with MongoDB Atlas.Connection Configuration
The database connection is handled inbackend/src/config/database.js:5-15 and initialized in backend/server.js:5.
The application uses Mongoose to connect to MongoDB with the connection string from the
MONGO_URI environment variable.Common Connection Errors
MongooseServerSelectionError: Could not connect to any servers
MongooseServerSelectionError: Could not connect to any servers
Cause: MongoDB server is unreachable. Common reasons include:Common mistakes:You should see:
- Incorrect connection string
- IP not whitelisted in MongoDB Atlas
- Network connectivity issues
- Firewall blocking connection
1. Verify Connection String
Check yourbackend/.env file:- Missing protocol (
mongodb+srv://) - Incorrect cluster URL
- Missing database name
- Special characters in password not URL-encoded
2. Whitelist Your IP Address (MongoDB Atlas)
Steps:- Go to MongoDB Atlas
- Select your project
- Navigate to Network Access (left sidebar)
- Click IP Access List
- Click Add IP Address
- Options:
- Add Current IP Address (for your current IP)
- Allow Access from Anywhere (
0.0.0.0/0) - for development only - Add specific IP addresses for production
3. Check Cluster Status
- Ensure your MongoDB Atlas cluster is running (not paused)
- Free tier clusters pause after inactivity
- Resume the cluster from the Atlas dashboard if paused
4. Test Connection
After whitelisting, restart your backend:"Connected to Database"MongoServerError: Authentication failed
MongoServerError: Authentication failed
Error Message: Replace:
Example:
"bad auth: Authentication failed"Cause: Incorrect database username or password.Solutions:1. Verify Database User Credentials
- Go to MongoDB Atlas → Database Access
- Check your database username exists
- If password is incorrect, edit the user and reset password
2. Update Connection String
Inbackend/.env:YOUR_USERNAMEwith your database username (not your Atlas login email)YOUR_PASSWORDwith your database password
3. URL-Encode Special Characters
If your password contains special characters, they must be URL-encoded:| Character | Encoded |
|---|---|
@ | %40 |
: | %3A |
/ | %2F |
? | %3F |
# | %23 |
& | %26 |
- Password:
P@ssw0rd! - Encoded:
P%40ssw0rd!
4. Restart Backend
MongoParseError: Invalid connection string
MongoParseError: Invalid connection string
Cause: The
MONGO_URI format is incorrect.Solutions:Correct Format for MongoDB Atlas:
Components:
- Protocol:
mongodb+srv://(for Atlas) ormongodb://(for local) - Username: Your database user
- Password: URL-encoded password
- Cluster: Your cluster hostname (e.g.,
cluster0.abc123.mongodb.net) - Database: Your database name (optional, defaults to
test) - Options: Query parameters for connection settings
Get Connection String from Atlas:
- Go to MongoDB Atlas → Database
- Click Connect on your cluster
- Choose Connect your application
- Copy the connection string
- Replace
<password>with your actual password - Replace
<database>with your database name (or usetest)
Common Mistakes:
- Missing
://after protocol - Spaces in the connection string
- Quotes around the connection string in
.envfile (not needed) - Using MongoDB Compass connection string instead of application string
Environment variable not loaded
Environment variable not loaded
Symptom: Backend logs show
undefined for process.env.MONGO_URICause: .env file is missing, in wrong location, or not loaded.Solutions:1. Verify .env File Location
The.env file must be in the backend/ directory:2. Check .env File Contents
backend/.env should contain:3. Verify dotenv is Loaded
Checkbackend/server.js:1 contains:4. Restart Backend
Environment variables are loaded at startup:If you change
.env values, you must restart the backend server for changes to take effect.Connection timeout
Connection timeout
Error:
"Server selection timed out after 30000 ms"Cause: Cannot reach MongoDB server within timeout period.Solutions:1. Check Internet Connection
- Ensure you have active internet connection
- Test connectivity:
ping cluster0.abc123.mongodb.net
2. Verify Firewall Settings
- Corporate/university firewalls may block MongoDB ports (27017)
- Try from a different network
- Contact network administrator if on restricted network
3. Check VPN
- Some VPNs block database connections
- Try disconnecting VPN or whitelisting VPN IP in Atlas
4. Verify Cluster Region
- Choose cluster region closest to your location for better latency
- Check Atlas dashboard for cluster status
Local MongoDB Setup
If you prefer using a local MongoDB instance instead of Atlas:1. Install MongoDB Locally
2. Start MongoDB Service
3. Update Connection String
backend/.env:
4. Verify Connection
Database Connection Best Practices
Security
- Use strong passwords for database users
- Whitelist only necessary IP addresses
- Never commit
.envfiles to version control - Rotate credentials if exposed
Performance
- Choose cluster region near your application
- Use connection pooling (handled by Mongoose)
- Monitor connection errors in production
- Set appropriate timeout values
Verification Checklist
Before seeking further help, verify:-
.envfile exists inbackend/directory -
MONGO_URIis correctly formatted - Database username and password are correct
- Special characters in password are URL-encoded
- Your IP is whitelisted in MongoDB Atlas Network Access
- MongoDB Atlas cluster is running (not paused)
- Backend server is restarted after changing
.env - You see “Connected to Database” in console
Additional Resources
Common Issues
General troubleshooting for JWT, file uploads, and more
MongoDB Atlas Docs
Official MongoDB Atlas documentation