MongoDB – couldn’t connect to server

Like Tweet Pin it Share Share Email

In this article we will guide you to fix – couldn’t connect to server error. While working with MongoDB you may get this error while starting it.

The error message “couldn’t connect to server” usually indicates that there is a problem connecting to the MongoDB server running on your local machine.

Reasons for couldn’t connect to server error 

There are multiple reasons due to which the MongoDB server may not start. In this article we have explained the major reasons one by one.

Check that the MongoDB server is running: 

You can check whether MongoDB server is running or not. To verify it, open a new terminal window and run the command “mongod”. If the server is already running then you should see a message indicating that it’s listening on the specified port. 

In case MongoDB server is not running on your local machine. You can start the MongoDB server by running the “mongod” command.

In the case of Linux machines you can use the command “sudo systemctl status mongod”. In case it’s not running, start the service using the “sudo systemctl start mongod” (on Linux).

Use “sc query MongoDB” (on Windows) to check if the MongoDB service is running. If it is not running use”net start MongoDB” (on Windows) command to start it.

Ensure that the MongoDB server is running by checking the output of the “ps” command on Unix-based systems.

In the case of Windows check the Task Manager. If the server is not running, start it by running the “mongod” command. In Task Manager you can also start it manually.

Hope you are able to start the MongoDB server. In case you need any help please post in the comment box.

Check the MongoDB port conflict issue:

The default MongoDB port is 27017. Make sure that the port is not blocked by your firewall, and that there are no other applications using the same port. 

If you have configured your MongoDB server to listen on a different port, then please specify that port in your connection string.

Open terminal/cmd and type “netstat -an | grep 27017 to verify whether MongoDB is listening on port 27017 or not. If it’s not, you may need to update the MongoDB configuration file to listen on the correct port.

If port 27017 is being used by another process, you need to run MongoDB on a different port. Specify the –port option when starting the mongod server. But make sure to change the port number in your connection string as well.

In unix based machines check the configuration file, typically located at /etc/mongod.conf. Look for the “port” option in the file, and ensure that it is set to 27017, the default port for MongoDB.

Firewall may block the connection: 

A firewall may be blocking the connection between the client and the server. To check this you can disable the firewall and test.

In case you have a firewall running on your machine, make sure that it is not blocking incoming connections to MongoDB. You may need to configure your firewall to allow incoming traffic on port 27017.

MongoDB configuration may be wrong: 

Check your MongoDB configuration file (usually located in /etc/mongod.conf or /usr/local/etc/mongod.conf) to make sure that the server is listening on the correct IP address and port.

Check your connection string: 

The format of the connection string should be “mongodb://<hostname>:<port>/<database>”. If you’re using the default settings, the connection string should be “mongodb://localhost:27017/<database>”.

Mostly mentioning wrong connection string is a reason for MongoDB – couldn’t connect to server So please make sure you are using proper connection string.

Check MongoDB logs:

You can look into MongoDB server logs. You may find any error messages that might indicate why the server is not starting or is unable to accept connections.

Check MongoDB version: 

Finally, Make sure that your MongoDB client version matches the version of the MongoDB server. In case they are different, you may need to update one or the other.


Hope this article helped you to fix – MongoDB – couldn’t connect to server error. In case you are not able to fix it, you can post your complete issue in the MongoDB community forum.

You may like to watch our YouTube video and install MongoDB in 2 minutes. You can also try reading Install MongoDB Compass in macOS – step by step guide.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *