News
3 new Serverspace GPT API Language Models available now!
JT
February 14 2024
Updated April 15 2025

How to Fix MongoDB Error: ECONNREFUSED 127.0.0.1:27017

Databases

A common issue faced by many MongoDB users is the inability to connect to their database servers. This problem is particularly evident after recent incidents at MongoDB Inc. that left users with idle database servers. When attempting to connect via the mongosh client, users often receive the following error message:

mongodb://127.0.0.1:27017/?directconnection=true&serverselectiontimeoutms=2000&appname=mongosh+2.1.1
Error
Screenshot №1 — Error

This issue typically occurs when trying to connect locally to the MongoDB server, but it can also arise in a network setup where the database resides on another server.

Understanding MongoDB Client-Server Communication

To understand how to resolve the issue, it's important to grasp the basic interaction between the client (mongosh) and the database server. In this case, both the client and server are expected to reside on the same machine. Therefore, the communication takes place through the loopback interface. However, in your situation, the database might be on a different server on the network.

When the client sends a connection request to the server (typically to port 27017), it will either establish a connection and allow access to the MongoDB shell, or it will return an error message if something goes wrong. Common causes for this error include the database daemon being down, incorrect configuration, or network connectivity issues.

All steps in the tutorial can be performed on cloud servers.

The daemon has been deactivated

The first step in resolving this issue is to ensure that the MongoDB service (daemon) is running. Use the following systemctl command to check the status of the mongod service:

systemctl status mongod
Disabled daemon
Screenshot №2 — Disabled daemon

If the service is not running, the status will show something like “inactive (dead)”. To start the service, use the following command:

systemctl start mongod

Once started, verify the service is running correctly by checking the logs:

journalctl -xeu mongod
Logs of unit
Screenshot №3 — Logs of unit

This will give you detailed logs of the MongoDB service, allowing you to see if any errors occurred during startup. If you see a message like "job for unit finished successfully", it means the service is up and running.

Connection and configuration error

If starting the MongoDB daemon doesn’t resolve the issue, it’s important to verify your configuration and network setup. Often, the error message includes the address mongodb://127.0.0.1:27017, which indicates that MongoDB is being accessed locally. To connect to a remote MongoDB instance, you’ll need to modify your connection string as follows:

mongosh mongodb://myuser:mypassword@123.456.789.0:27017/mydatabase

Make sure to replace myuser, mypassword, 123.456.789.0, and mydatabase with your actual credentials and database details.

Next, ensure that the MongoDB server is reachable from your client machine. You can check the port availability using netcat:

apt install netcat-traditional -y
nc -zv 127.0.0.1 27017
If the port is closed or unreachable, proceed to the next step.
Test port
Screenshot №4 — Test port

Open the MongoDB configuration file to ensure it’s correctly set up to accept network connections:

nano /etc/mongod.conf
Config
Screenshot №5 — Config
Verify that the bindIp option is set to 0.0.0.0 (for external connections) or 127.0.0.1 (for local connections). Additionally, ensure that the port is set to 27017 or another desired port. If any changes are made, save the file with Ctrl + O, and restart the MongoDB service:
systemctl restart mongod

System user has no rights

MongoDB uses specific system users and groups to run the service, and it's important that these users have the proper permissions to access the MongoDB configuration files. To check the file permissions on the configuration file:

ls -la /etc/mongod.conf
Rights
Screenshot №6 — Rights

Ensure that the file owner has read and write permissions and that others (including the MongoDB service user) have at least read permissions. If permissions are incorrect, use the chmod command to correct them:

chmod 644 /etc/mongod.conf
Change rights
Screenshot №7 — Change rights

Once the permissions are set correctly, restart the MongoDB service again:

systemctl restart mongod

In general, understanding the causes of this error and using appropriate tools to diagnose and resolve the problem will help you successfully restore MongoDB operation and troubleshoot database server connectivity issues.

FAQ - Frequently Asked Questions

  • Q: How can I connect to MongoDB remotely?
    A: To connect to MongoDB remotely, use the following connection string format:
mongosh mongodb://username:password@hostname:port/database

Replace username, password, hostname, port, and database with your own MongoDB credentials and information.

  • Q: What should I do if MongoDB is not starting?
    A: If MongoDB is not starting, first check the logs using:
journalctl -xeu mongod

Look for any errors that might indicate issues with the configuration file, disk space, or other system resources. If there are no clear issues, ensure that the MongoDB service has sufficient permissions and that the mongod.conf file is correctly configured.

  • Q: Can I change the default MongoDB port?
    A: Yes, you can change the default MongoDB port by modifying the port field in the mongod.conf configuration file:
net:
port: 27018 # Change this to your desired port
Remember to restart the MongoDB service after making changes.
  • Q: How do I ensure MongoDB listens on all IP addresses?
    A: To allow MongoDB to listen on all IP addresses, change the bindIp setting in the mongod.conf file:
net:
bindIp: 0.0.0.0 # Allows connections from all IP addresses

This will enable MongoDB to accept connections from external IPs. Make sure to restart the service after making changes.

Conclusion

By following these steps, you can troubleshoot and resolve common MongoDB connection issues such as ECONNREFUSED, unresponsive services, and incorrect configuration. From verifying that the MongoDB daemon is running, to checking configuration files and network access, each step helps pinpoint and eliminate potential obstacles in your database connection process.

Understanding how to properly manage MongoDB’s service lifecycle, permissions, and connection parameters is essential for both local development and production environments. Ensuring correct configuration and access control not only improves reliability but also enhances the security and scalability of your MongoDB setup.

Whether you're hosting your database on a local machine or a remote cloud server, these best practices will help you maintain a stable and accessible MongoDB environment.

Vote:
5 out of 5
Аverage rating : 5
Rated by: 2
33145 North Miami, FL 2520 Coral Way apt 2-135
+1 302 425-97-76
700 300
ITGLOBAL.COM CORP
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.