启动MongoDB时遇报错:Failed to set up listener: SocketException: Cannot assign requested address
Fixing "Failed to set up listener: SocketException: Cannot assign requested address" in MongoDB
Hey there, I’ve run into this exact error a few times when setting up MongoDB—let’s walk through the most common fixes to get your service up and running again. This error almost always means MongoDB can’t bind to the IP address it’s configured to use, so we’ll target that first.
Common Causes & Solutions
Check your
bindIpconfiguration- First, locate your MongoDB config file:
- Linux: Typically
/etc/mongod.conf - Windows: Usually in
C:\Program Files\MongoDB\Server\[version]\bin\mongod.cfg
- Linux: Typically
- Open the file and look for the
netsection, specifically thebindIpline. If this is set to an IP that doesn’t exist on your machine (like a typo, or an IP from a disconnected network adapter), MongoDB can’t bind to it. - Fix options:
- Use
127.0.0.1to restrict access to localhost (great for development) - Use
0.0.0.0to allow binding to all available IPs (test environments only—lock this down in production!) - Replace with your machine’s actual public/private IP (e.g.,
192.168.1.50)
- Use
- After updating the config, restart the MongoDB service:
- Linux:
sudo systemctl restart mongod - Windows: Restart the MongoDB service via the Services Manager
- Linux:
- First, locate your MongoDB config file:
Verify the port isn’t occupied
- MongoDB defaults to port 27017. If another program is using this port, MongoDB can’t listen on it.
- Check for port usage:
- Linux/macOS:
sudo lsof -i :27017ornetstat -tulpn | grep 27017 - Windows:
netstat -ano | findstr :27017
- Linux/macOS:
- If the port is taken, either stop the conflicting program or update the
portvalue in your MongoDB config to an unused port (e.g., 27018), then restart the service.
Check firewall/security group rules
- Sometimes firewalls block MongoDB from binding to an external IP or listening on its port.
- Linux: Allow the port with
sudo ufw allow 27017(adjust for iptables if needed) - Windows: Add an inbound rule in Windows Defender Firewall to allow MongoDB’s executable or port 27017.
Quick test with a temporary start command
- To confirm the config is the issue, try starting MongoDB directly with a valid bind IP:
mongod --bind_ip 127.0.0.1(localhost only)mongod --bind_ip_all(all IPs)
- If this works, you know the problem is in your permanent config file—just update it to match this working setting.
- To confirm the config is the issue, try starting MongoDB directly with a valid bind IP:
内容的提问来源于stack exchange,提问作者Vijay Thopate
相关产品推荐
相关产品推荐

