1. Check if MongoDB is Running using below cmd
sudo systemctl status mongod
If MongoDB is not running, start it:
sudo systemctl start mongod If MongoDB is disabled, enable it: sudo systemctl enable mongod 2. Verify MongoDB is Listening on the Correct IP netstat -tulnp | grep mongod OR ss -tulnp | grep mongod
It should show:
tcp LISTEN 0 128 0.0.0.0:27017 0.0.0.0:* users:(("mongod",pid=1234,fd=11))
If it only shows 127.0.0.1:27017
, MongoDB is not allowing remote connections.
Fix: Allow Remote Connections
1.Open MongoDB config file:
sudo nano /etc/mongod.conf
2. Find this line:
bindIp: 127.0.0.1
3.Change it to:
bindIp: 0.0.0.0 4. Save & exit (CTRL + X
, thenY
, thenENTER
). 5. Restart MongoDB: sudo systemctl restart mongod 3. Open Firewall for MongoDB
sudo ufw allow 27017/tcp
sudo ufw reload
Then check if the firewall is open:
sudo ufw status
Ensure you see:
27017 ALLOW Anywhere 4. Test Connection from the Server
Try connecting to MongoDB locally from your Ubuntu server:
mongosh use admin db.runCommand({ connectionStatus: 1 }) If this works, MongoDB is running fine locally. 5. Use the Correct Connection String in MongoDB Compass
Try connecting with:
mongodb://your ip address:27017
If MongoDB has authentication enabled, use:
mongodb://username:[email protected]:27017/?authSource=admin Still Not Working?
Run this on your Ubuntu server and share the output:
sudo ss -tulnp | grep 27017 sudo ufw status cat /etc/mongod.conf | grep bindIp
Comments