MongoDB – couldn’t connect to server 127.0.0.1:27017

Mongo – couldn’t connect to server 127.0.0.1:27017

Today in this article we will cover basic steps to resolve the error MongoDB – couldn’t connect to server 127.0.0.1.

Today in this article, we will cover below aspects,

Issue Description

Mongo connection using shell command or using Mongo Atlas UI portal gives below error,


Error: couldn't connect to server localhost:27017, connection attempt failed: SocketException: Error connecting to localhost:27017 (127.0.0.1:27017) :: caused by :: No connection could be made because the target machine actively refused it. :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1
'ssl' is not recognized as an internal or external command,
operable program or batch file.
couldnt connect to server 127 0 0 127017 mongodb ResolvedMongoDB couldnt connect to server 12700127017 | TheCodeBuzz't connect to server localhost:27017, connection attempt failed: SocketException: Error connecting to localhost:27017 (127.0.0.1:27017) :: caused by :: No connection could be made because the target machine actively refused it. :
connect@src/mongo/shell/mongo.js:372:17
@(connect):2:6
exception: connect failed
exiting with code 1
'ssl' is not recognized as an internal or external command,
operable program or batch fil

Resolution

Please use the below steps to verify the issue,

  • Recheck your connections string.

As indicated above your command should append with mongo followed by a connection string.

Once verified, please cross-check if the MongoDB service is running on your machine.

For Windows,

Use the below command to run the MongoDB service,

sc start mongodb

Alternatively, you can use Service Control Manager (SCM) to start or restart the service if running already.

couldnt connect to server 127 0 0 127017 mongodb service control command ResolvedMongoDB couldnt connect to server 12700127017 | TheCodeBuzz't connect to server 127.0.0.1, couldn't connect to server localhost:27017, connection attempt failed: SocketException

For Linux/WSL/Ubuntu

You could try the below command to start the MongoDB service for Ubuntu/Linux

sudo service mongodb status


sudo service mongodb start

Other references :

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



2 thoughts on “MongoDB – couldn’t connect to server 127.0.0.1:27017”

Leave a Comment

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