求助:Windows10通过MongoDB Compass连接Ubuntu14 EC2实例失败
Hey there, let's troubleshoot this MongoDB connection issue together—since you can SSH into your EC2 Ubuntu 14 instance without problems, we know the basic network path is working. Let's go through the key checks step by step:
By default, MongoDB is configured to only listen on 127.0.0.1 (localhost), which means it won't accept connections from outside the instance—even if your security group allows port 27017. Here's how to fix that:
- SSH into your EC2 instance and open the MongoDB config file. On Ubuntu 14, this is usually either
/etc/mongodb.confor/etc/mongod.conf(depending on how you installed MongoDB). - Look for the
bind_ipline. Change its value to0.0.0.0(to allow connections from any IP) or your Windows 10 machine's public IP (more secure). - Restart the MongoDB service to apply changes:
sudo service mongodb restart # Or use this if you're using systemd: sudo systemctl restart mongod - Confirm the binding worked by running:
You should seenetstat -plntu | grep mongod0.0.0.0:27017or your specified IP next to the port.
Ubuntu 14 might have iptables rules blocking incoming traffic on port 27017, even if your AWS security group allows it:
- List current iptables rules:
sudo iptables -L -n - If there's no rule allowing port 27017, add one:
sudo iptables -A INPUT -p tcp --dport 27017 -j ACCEPT - Save the rules to make them persistent after reboot:
sudo mkdir -p /etc/iptables sudo iptables-save > /etc/iptables/rules.v4
Sometimes the service shows as "running" but isn't listening correctly. Let's verify:
- On the EC2 instance, test local connectivity first:
If this fails, check MongoDB's logs for startup errors:nc -zv localhost 27017 # Or use telnet if nc isn't installed: telnet localhost 27017sudo tail -f /var/log/mongodb/mongodb.log - Next, test connectivity from the instance to its own public IP:
If this fails, go back to step 1—your bind IP configuration is still incorrect.nc -zv <your-ec2-public-ip> 27017
Don't rule out issues on your end:
- Temporarily disable Windows Defender Firewall (just for testing) to see if it's blocking outgoing traffic to port 27017. If that works, add a permanent rule allowing connections to your EC2 instance's IP on port 27017.
- Test the connection using a tool like MongoDB Compass or the command line:
mongo "<your-ec2-public-ip>:27017" # Add authentication details if enabled: mongo "<your-ec2-public-ip>:27017" -u <username> -p <password> --authenticationDatabase admin
If you've enabled authentication on MongoDB, some clients might show "MongoDB not running" as a generic error when authentication fails, rather than a specific auth error. To check:
- SSH into your EC2 instance and open the MongoDB shell:
mongo - Switch to the admin database:
use admin - List existing users:
If you have users set up, make sure you're including the correct credentials in your connection string from Windows.db.getUsers()
内容的提问来源于stack exchange,提问作者Alon

