How can I troubleshoot this error message?
“Error: couldn’t connect to server 127.0.0.1
:27017
” is a general error message indicating that your client/driver cannot connect to a server on the specified hostname/IP and port. In this specific example, 127.0.0.1
is the hostname or IP and 27017
is the port.
Typical reasons for this error are:
- Your connection from client to server is blocked by firewall or network configuration
- A MongoDB server is not listening on the requested Host/IP and port (check they are both correct)
- You are connecting to a remote MongoDB deployment that is not listening to the public IP you are connecting to.
What should I check for my MongoDB Atlas cluster?
Please refer to Troubleshoot Connection Issues in the MongoDB Atlas documentation.
What should I check for a self-hosted deployment?
All modern versions of MongoDB bind to localhost
by default so you have to explicitly adjust IP Binding to allow external connections.
Please review the MongoDB Security Checklist and consider available security measures to avoid opening your deployment to being compromised. In particular, I strongly recommend always:
Follow the general security practice of Principle of Least Privilege and consider how you can reduce unnecessary risk to your deployment. For example, instead of opening up your deployment to the world, consider using a VPN or SSH tunnel to provide secure remote access.
What should I check for MongoDB Compass?
The above suggestions should be helpful, but the Compass documentation also includes a reference for Compass Connection Errors.
What should I check for macOS Catalina (or newer)?
If you run mongod
without providing a configuration file, it will use a hardcoded default of /data/db
which is not created as part of the Homebrew installation and not supported on macOS Catalina (which no longer allows creating paths on the root filesystem).
The documented steps for Run MongoDB Community Edition on macOS are correct (and in recommended order of approach):
- To run MongoDB (i.e. the
mongod
process) as a macOS service , issue the following:
brew services start mongodb-community@4.2
To run MongoDB (i.e. the mongod
process) manually as a background process , issue the following:
mongod --config /usr/local/etc/mongod.conf --fork
If you want to run mongod
without using either of these options, you can also explicitly pass the --dbpath
parameter. Per SERVER-46398, this suggestion has been added to recent releases of MongoDB (3.6.19+, 4.0.19+, 4.2.7+, 4.4.0-rc4).
I have a similar problem and still need help. What should I do?
If you are having a similar issue connecting to your MongoDB deployment, please start a new discussion topic with details specific to your environment such as:
- MongoDB server version
- O/S version
- How you installed MongoDB (package manager, tarball, etc) or the installation guide you followed
- Command-line or steps used to start your MongoDB server
- Client or driver version you are trying to connect with
It would be very useful to include information on any troubleshooting steps you have already tried.
Regards,
Stennie