MongoDB bindIp 127.0.0.1 refuses remote clients, 0.0.0.0 works
bindIp: 127.0.0.1 works locally but remote clients get connection refusedShort answer
127.0.0.1 is the loopback address, so a mongod bound only there answers the machine itself. A remote client reaches the server's network address, which is not in the list. Add that address to bindIp, comma separated. 0.0.0.0 works because it listens on every IPv4 address, so secure the server first.
Fix it fast
1Find out which cause you have
See which addresses mongod is actually listening on.
sudo ss -ltnp | grep 270172Match what you see, then jump to its fix
- 1
ss shows 127.0.0.1:27017 onlyOnly the loopback address is in bindIpFix - 2
mongod will not start after the edit, or the setting has no effectbindIp and bindIpAll are both set, or a command-line flag overrides the fileFix - 3
Clients using an IPv6 address still cannot connectAn IPv6 address was added without enabling IPv6Fix
- 1
What does the error look like?
bindIp: 127.0.0.1 works locally but remote clients get connection refusedbindIp: 0.0.0.0 works from remote clients
Cause 1 of 3
Only the loopback address is in bindIp
1Check
mongod binds to localhost by default. Remote clients connect to the machine's private or public interface address, which must be in the list.
grep -A3 '^net:' /etc/mongod.conf
ip -4 addr show2Fix
Add the interface address next to localhost, as a comma-separated list. Use the private network address, not every interface.
net:
port: 27017
bindIp: 127.0.0.1,10.0.1.253Verify
Restart mongod and look at the listening addresses again.
sudo systemctl restart mongod
sudo ss -ltnp | grep 27017Confirmed by mongodb.com
Cause 2 of 3
bindIp and bindIpAll are both set, or a command-line flag overrides the file
1Check
net.bindIp and net.bindIpAll cannot be used together; setting both makes mongod throw an error and stop. A --bind_ip flag on the command line overrides the config file.
grep -n -E 'bindIp|bindIpAll' /etc/mongod.conf
ps -o args= -C mongod2Fix
Keep one of them. To listen everywhere, use bindIpAll or a bindIp of 0.0.0.0, not both.
net:
port: 27017
bindIpAll: true3Verify
Start mongod and confirm it stays up.
sudo systemctl status mongodConfirmed by mongodb.com
Cause 3 of 3
An IPv6 address was added without enabling IPv6
1Check
Putting an IPv6 address in bindIp does not enable IPv6 support by itself; net.ipv6 must also be true.
grep -n -E 'ipv6|bindIp' /etc/mongod.conf2Fix
Enable IPv6 explicitly alongside the address.
net:
ipv6: true
bindIp: 127.0.0.1,::13Verify
Restart mongod and confirm it now listens on the IPv6 loopback.
sudo ss -ltnp | grep 27017Confirmed by mongodb.com
How do I stop it coming back?
- Bind to the specific private address, not 0.0.0.0, and keep it inside your VPC.
- Turn on authentication and limit port 27017 in the firewall or security group before binding beyond localhost.
- Use DNS names in bindIp for replica set members, as the MongoDB docs recommend.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
Installing the Airflow MySQL provider fails on pkg-config or mysqlclient
Building the mysqlclient wheel fails during the install
The Airflow MySQL provider depends on mysqlclient, which pip compiles against the MySQL C client library.
2 causes, each with a fixDatabases
Compiling a C or C++ program that includes mysql.h fails
undefined reference to `mysql_init'
The compiler only finds mysql.h when the MySQL client development files are installed and their folder is on the include path.
3 causes, each with a fixDatabases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fixDatabases
manage.py test stops with 'permission denied to create database' on PostgreSQL
django.db.utils.ProgrammingError: permission denied to create database
Django creates a separate database called test_ plus your database name, using the DATABASES USER.
2 causes, each with a fixDatabases
An EC2 instance cannot reach its RDS database
Could not open connection to the host, on port <port>: Connect failed
RDS denies all access until a security group allows it.
3 causes, each with a fix