There are a lot of people getting errors while connecting to the server. So, In this MongoDB tutorial, We are going to resolve this error while “MongoDB failed to connect to server”. We will also see the root cause of the issue.
We will also cover this in different operating systems. These are the following topics that we are going to cover in this tutorial:
- MongoDB failed to connect to server
- MongoDB compass failed to connect
- MongoDB failed to connect to 127.0.0.1(localhost)
- MongoDB failed to connect to bus host is down
- MongoDB failed to connect to localhost 27017 ubuntu
- MongoDB failed to connect to localhost 27017 mac
MongoDB failed to connect to server
This problem occurs when you try to connect to your MongoDB server and you are failed to connect to your MongoDB server.

As you can see, when we try to connect to the server we got the error “couldn’t connect to the server”. To resolve this error you have to start the server so you can easily connect to the server.
Follow the following steps so you can start the server and connect it:
- Open the services folder in your system and search MongoDB server (MongoDB).
- Here, services are the program that run in the background without a user interface and enable system features. A service is software that performs automated tasks, responds to hardware events, or listens for data requests from other software.

- See, MongoDB server is not in running state that why we are failed to connect to the server.
- Now to start the MongoDB server, you have to click on Start button on right side this will start you MongoDB server.

- After starting the MongoDB services, connect to MongoDB server.
- Open command prompt and write command mongo in the shell.

- Now, we successfully connect to the MongoDB server.
This way you can resolve the error when your system failed to connect to the server.
Read: MongoDB drop collection
MongoDB compass failed to connect
This problem occurs when the compass attempted to connect to your database on the specified host and port.
The error occurs when:
- When you provide no hostname or invalid hostname to the compass connect
- Incorrect port
- MongoDB server has been shut down or server hostname has changed
- firewall actively blocking connection to your local network
Solution:
Now, we discuss some of the solutions and the solution may depend on how your MongoDB environment is configured.
These are some of the solutions:
- Ensure that your MongoDB instance is running: Compass must connect to a running MongoDB instance. Also check you have installed MongoDB and have a running mongod process. You should also check that the port where MongoDB is running matches the port you provide in the compass connect.
- Hostname and Port: Varify that you have specified your Hostname and Port correctly in the compass conect.
- MongoDB shutdown or server moved: Sometimes it is possible that MongoDB server has shutdown or host name has changed. To resolve this update the hostname or if server is shutdown start the server.
- Firewall: If you believe that your firewall may be blocking your connection to the port, try to connect from a different loaction.
Read: How to store images in MongoDB
MongoDB failed to connect to 127.0.0.1 (localhost)
The MongoDB failed to connect error “Error: couldn’t connect to the server 127.0.
0.1
:27017
” is a general error that means your client/server cannot connect to a server on the specified IP and port. Here, 127.0.0.1(localhost) is the hostname and 27017 is the port number.
There could be any reason for this error:
- Firewall or network configuration blocked you to connect to the server.
- The MongoDB server is not listening the IP or port.
Now, you have to analyse that what could be the reason behind the error and then solve the error:
- If the problem occurs due to the firewall or network configuration then you have to stop it and then restart the server so you can easily connect to the MongoDB server.
How to stop firewall?
You need to follow the below steps to stop the firewall services:
- Go to system settings and click on update & security.
- Click on Windows Security right side and click on Firewall & network protection.

- Now, you can see different network settings and you can turn off all the them or either which want to turn off.

This way you can stop the firewall services.
Now start the MongoDB server, this time you can easily start it without any error. Go to the command prompt write command mongo and this will start the server.

Here, you successfully connected to the server(127.0.0.1:27017).
- If the error occurs due to a port issue or the port is already occupied by some other services then you can change the port and start the MongoDB server on another port. Use below command to start the server on another port
C:\Program Files\MongoDB\Server\5.0\bin>mongo --port 4000
The default port of MongoDB is 27017, 27018, and 27019. If these ports are already used by some other services then you change the port.

After changing the port you successfully connected to 127.0.0.1:4000.
In this topic, you have learned “stop to the firewall and change the port of MongoDB” and after that, you successfully connect to 127.0.0.1
Also Read: Import JSON and insert JSON into MongoDB
MongoDB failed to connect to bus host is down
In ubuntu, when I was connecting to the MongoDB server and checking the MongoDB server-status then my system couldn’t find the mongod.service and failed to connect due to the bus host is down.
So to fix this error, you have to execute the below command
sudo systemctl enable mongodb
This command will help you to restore all the services. After this, you have to copy the correct config to /etc/mongod.conf and execute the below command.
sudo service mongodb restart
This command will restart your MongoDB server. After this check the status of MongoDB.
sudo service mongodb status

Now, you can see after restarting the service, MongoDB status is active(running). This way you resolve the error and connect to the MongoDB server.
Read: How to check if MongoDB is installed + MongoDB Version
MongoDB failed to connect to localhost 27017 ubuntu
When I tried to connect to the MongoDB server, received the error “MongoDB failed to connect“. This error may occur when the MongoDB server is not running or inactive.

Let’s get into this, Check the MongoDB server is running fine or not. In my case error came due to the MongoDB server was down.
To resolve this error, you have to follow some of the following steps:
- Verify the MongoDB server status
sudo service mongodb status
This command will show you the status of the MongoDB database like it is active or inactive.

As you can see in the output it is inactive(dead), That is the reason behind the server was not running.
- Now, start the MongoDB server by using command
sudo service mongodb start
This command will start the MongoDB server.
- Again, verify the MongoDB server status
sudo service mongodb status

This command will help you to start the MongoDB server.
- Now, type mongo in the shell and this will start MongoDB server

This way you can easily start the server and resolve the problem when your system failed to connect to the MongoDB server.
Read: Create tables in MongoDB
MongoDB failed to connect to localhost 27017 mac
When I start the MongoDB server in mac os then this error occurred “failed to connect to localhost”. This error occurs when the MongoDB service is not running on the mac.
To resolve this error, you have to start the MongoDB service using the below command:
brew services start mongodb
This command will start the MongoDB services and after this, you have to simply write in the shell mongod command.
mongod
This command will start the mongo daemon.
mongo
Now, again open the shell and write the mongo command. This will start the MongoDB interactive client. Now, you use the MongoDB database.
This way you can resolve the error in mac os and connect to the MongoDB server.
You may also like reading the following articles.
- MongoDB sort by field
- MongoDB sort by date
- How does MongoDB create collection
- Pros and cons of MongoDB
- Unable to locate package mongodb-org
- MongoDB is not recognized as an internal or external command
In this tutorial, we have learned “How to resolve the error when MongoDB failed to connect” in different operating systems. These are the following topics that we covered in this tutorial
- MongoDB failed to connect to server
- MongoDB compass failed to connect
- MongoDB failed to connect to 127.0.0.1(localhost)
- MongoDB failed to connect to bus host is down
- MongoDB failed to connect to localhost 27017 ubuntu
- MongoDB failed to connect to localhost 27017 mac
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.