Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

MongoDB bindIp 127.0.0.1 refuses remote clients, 0.0.0.0 works

ErrorbindIp: 127.0.0.1 works locally but remote clients get connection refused

Short 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.

  • Applies to MongoDB 8.0, Ubuntu 24.04, Amazon Linux 2023
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which addresses mongod is actually listening on.

    sudo ss -ltnp | grep 27017
  2. 2Match what you see, then jump to its fix

    1. 1ss shows 127.0.0.1:27017 onlyOnly the loopback address is in bindIpFix
    2. 2mongod 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. 3Clients using an IPv6 address still cannot connectAn IPv6 address was added without enabling IPv6Fix

What does the error look like?

  • bindIp: 127.0.0.1 works locally but remote clients get connection refused
  • bindIp: 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 show

2Fix

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.25

3Verify

Restart mongod and look at the listening addresses again.

sudo systemctl restart mongod
sudo ss -ltnp | grep 27017

Confirmed by mongodb.com

Did this fix it?

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 mongod

2Fix

Keep one of them. To listen everywhere, use bindIpAll or a bindIp of 0.0.0.0, not both.

net:
  port: 27017
  bindIpAll: true

3Verify

Start mongod and confirm it stays up.

sudo systemctl status mongod

Confirmed by mongodb.com

Did this fix it?

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.conf

2Fix

Enable IPv6 explicitly alongside the address.

net:
  ipv6: true
  bindIp: 127.0.0.1,::1

3Verify

Restart mongod and confirm it now listens on the IPv6 loopback.

sudo ss -ltnp | grep 27017

Confirmed by mongodb.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.