ChatGPT解决这个技术问题 Extra ChatGPT

MongoError: connect ECONNREFUSED 127.0.0.1:27017

I'm using NodeJS wih MongoDB using mongodb package. When I run mongod command it works fine and gives "waiting for connection on port 27017". So, mongod seems to be working. But MongoClient does not work and gives error when I run node index.js command-

MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

https://i.stack.imgur.com/lXL3f.png

I have install mongo db 3.4 and my code is-

var MongoClient = require('mongodb').MongoClient;
var dburl       =   "mongodb://localhost:27017/test";
MongoClient.connect(dburl, function(err, db) {
  if (err) {
    throw err;
  }
  console.log('db connected');
  db.close();
});

I have created data/db directories on root and given write permissions. mongod.conf file takes db path as-

storage: dbPath: /var/lib/mongo

But it seems that it is actually taking db path as data/db and not var/lib/mongo

It working earlier but suddenly stopped.

Go inside location C:\Program Files\MongoDB\Server\3.4\bin and run mongod.exe and try again
Thanks. But I am on amazon linux server. How to test there?
replace localhost with 127.0.0.1 and try. not sure about the result but try and update us. and also try with running below command from terminal ,sudo service mongod start
I already tried this, didn't work. sudo service mongod start gives error- Starting mongod (via systemctl): Job for mongod.service failed because the control process exited with error code. See "systemctl status mongod.service" and "journalctl -xe" for details.
I am really surprised how many answer provide a Windows solution (and they are even top rated) despite this question clearly refers to Linux.

S
Sonu Sourav

This happened probably because the MongoDB service isn't started. Follow the below steps to start it:

Go to Control Panel and click on Administrative Tools. Double click on Services. A new window opens up. Search MongoDB.exe. Right click on it and select Start.

The server will start. Now execute npm start again and the code might work this time.


For windows 10 path is Control Panel\System and Security\Administrative Tools
you saved my life
Save my life............
P
Page not found

My apps stopped working after upgrading from Nodejs 14 to 17.

The error I got was

MongoServerSelectionError: connect ECONNREFUSED ::1:27017

The solution was simply replacing localhost by 0.0.0.0. I.e. in my source code I had to change

const uri = "mongodb://localhost:27017/";
const client = new MongoClient(uri);

to

const uri = "mongodb://0.0.0.0:27017/";
const client = new MongoClient(uri);

I am posting this as an answer because I couldn't find the solution anywhere on the Internet and I wasted a lot of time on this one...


MongoDB seems to be not compatible with NodeJS v17. It should be listening on IPv6 "::1" by default and it would work with v17 without any problem.
What makes mongodb://0.0.0.0:27017/ ?, try to connect to every ip?
for me replacing localhost with 127.0.0.1 worked, thanks!
Thanks a lot, brother. Can you please tell me why this error occurred? why localhost is not working
mongodb://0.0.0.0:27017/ worked for me 👍🏻
m
matrixb0ss

I've forgot to start MongoDB at all. Go to separate tab in terminal and type:

sudo service mongod start

F
Farid Ansari

For windows - just go to Mongodb folder (ex : C:\ProgramFiles\MongoDB\Server\3.4\bin) and open cmd in the folder and type "mongod.exe --dbpath c:\data\db"

if c:\data\db folder doesn't exist then create it by yourself and run above command again.

All should work fine by now.))


@nikhilsugandh - really? The screenshot clearly shows a Linux environment, not Windows.
H
HeshanHH

For Windows users.

This happens because the MondgDB server is stopped. to fix this you can,

1 search for Services ,for that search using the left bottom corner search option. 2 then there is a gear icon named with 'Services'. 3 after clicking that you will get a window with lot of services 4 scroll down and you will find 'MongoDB server (MongoDB)'. 5 so you will see that there is an option to start the service. 6 start it. 1 then open your command prompt. 2 type mongod

now you can connect to your server.

this steps also working fine if your sql server service down. (find sql server services)


thanks , worked for me . i just started this option 'MongoDB server (MongoDB)
M
Manohar Reddy Poreddy

I had below error:

Error: connect ECONNREFUSED 127.0.0.1:27017

In my case, this issue occurred since MongoDB is not installed at all.

The solution was to install MongoDB from below location:
https://www.mongodb.com/download-center/community

After installing the ECONNREFUSED error did not appear again.

Hope that helps.


N
Nitin Bhanderi

Try to start mongoDB server by giving the --dbpath with mongod.

sudo mongod --dbpath /var/lib/mongo/data/db &

'&' in the last will start the mongodb server as service on your server.

Hope it Works.


l
lahiru dilshan

Search services.msc in your device and click start to run the mongoDB server

https://i.stack.imgur.com/CKcTE.png

After started running MongoDB server you will able to run your program


U
U.A

For Ubuntu users run sudo systemctl restart mongod


D
Doniyor Boltayev

Press CTRL+ALT+DELETE/task manager , then go to services , find MongoDB , right click on it. start. go back to terminal . npm start it will work.


easy solution, it works on my windows 10. thank you
W
Wamala Emmanuel Nsubuga

I had the same error because i had not installed mongoDB. Make sure that you have mongodb installed and if not, you can download it from here https://www.mongodb.com/download-center/community


If you have it installed and are still having said problem, you forgot to start the services
"install mongodb" is not an answer for sure...people who post this are users of MongoDB.
N
Nizar Kadri

For Windows users: Mongo version 4.4 Use following commands: NET STOP MONGODB – To stop MongoDB as a service,if this returns "mongoDb service is not running then use below command to start service" NET START MONGODB – To start MongoDB as a service.

This worked for me.


F
Fotios Tsakiris

On a mac I had to run brew services start mongodb-community@4.4

Check: Install MongoDB Community Edition on macOS


k
krnitheesh16

If you already installed "MongoDB", if you accidentally exit from the MongoDB server, then "restart your system".

This method solved for me...

[On Windows only]

And also another method is there:

press:

Windows + R

type:

services.msc

and click "ok", it opens "services" window, and then search for "MongoDB Server" in the list. After you find "MongoDB Server", right-click and choose "start" from the pop-up menu.

The MongoDb Server will start running.

Hope it works!!


S
Sahil

just start the mongo server from terminal

Ubuntu -

     sudo systemctl start mongod

thx matte. in my case, I was forgetting to run it as sudo.
Solved my problem on Ubuntu 20
h
hariom nagar

I just found a solution in the internet , If you are using latest nodejs (v17.x) , then try updating mongodb url from localhost to 127.0.0.1

ref: https://www.mongodb.com/community/forums/t/mongooseserverselectionerror-connect-econnrefused-127-0-0-1-27017/123421/3


Saved my day, thank you
y
yogesh patel

I was facing the same issue on windows 10. I just uninstall MongoDB and installed it again and it started working. It solved my problem.


It is better to comment on your short answers and ideas like other friends
This is because the service was disabled. Just open Services > Enable and Start mongod service. You can achieve the same using the command line. Just consult the documentation for exact syntax.
B
Bayram Binbir

Method 1

If It’s MAC OS, run following command and try again:

brew services restart mongodb-community

Stopping mongodb-community... (might take a while) ==> Successfully stopped mongodb-community (label: homebrew.mxcl.mongodb-community) ==> Successfully started mongodb-community (label: homebrew.mxcl.mongodb-community)


E
Eng_Farghly

You can run Cmd as administrator and write this command to start Mongodb service:

 net start MongoDB

OR

Click window key + r key Type services.msc and press ok Wait open service Search mongodb Click right key and start press

and reopen mongodb


F
Farrukh

most probably your mongo db service is not started. For ubuntu : sudo service mongod start For windows : go to services and start the MongoDB service

also install link for mondoDB service https://www.mongodb.com/download-center/community

I had this problem and i solved it thanks to this man's answer https://stackoverflow.com/a/47433551/7917608


R
Richard Rosario

I was having the same problem today.. and has been searching for answers.. I am on Ubuntu... however, I could not find the correct one that works on this thread.. after much research the following worked for me finally!! :)

First, after running

mongod dbpath

if appeared that mongodb was looking for the data/db directory.. which was missing in my installed mongodb app.. so I ran the following commands:

$ sudo mkdir -p /data/db

then run,

$ sudo chown -R $USER /data/db

chown - changes ownership of files/dirs. Ie. owner of the file/dir changes to the specified one, but it doesn't modify permissions. As detailed here: https://unix.stackexchange.com/questions/402062/how-are-chown-and-chmod-command-different-in-the-given-operation

Finally, run

`$ sudo systemctl enable mongod.service

It will give a message: Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service and started the service.


M
Mohamed Ahmed

Check where your .sock file is located. For my case, it was in /tmp folder.

Then run the following commands.

chown mongodb:mongodb /tmp/mongodb-27017.sock

sudo systemctl start mongod

mongosh

This worked for me.

For more reference


a
akbar

If you use MongoDb Compass make sure that you have installed MongoDB Community Server.


N
Novichok

I had the same issue on Windows.

Ctrl+C to close mongod, and start mongod again.

Am not sure, but it seems that initially mongod was working opening and closing connections. Then, it was not able to close some earlier connections and reached limit for opened ones. After restarting mongod, it works again.


o
otoloye

You probably need to continue running your DB process (by running mongod) while running your node server.


w
wise king

Please ensure that your mongo DB is set Automatic and running at Control Panel/Administrative Tools/Services like below. That way you wont have to start mongod.exe manually each time.

https://i.stack.imgur.com/SHkfB.png


that has nothing to do with the OPs question.
A
Anonymous Unknown

The solution was to install MongoDB from below location: https://www.mongodb.com/download-center/community


From Review: Hi, this post does not seem to provide a quality answer to the question. Please either edit your answer to explain why your solution works, or just post it as a comment to the question.
M
Mohit Rakhade

because you didn't start mongod process before you try starting mongo shell.

Start mongod server

mongod

Open another terminal window

Start mongo shell

mongo

this is only for windows machine
G
Gokul Ramakrishnan

In my particular instance, I was connected to a VPN and that was blocking the localhost connection to MongoDB somehow. It worked when I disconnected my VPN. It's a special case, but good to know nonetheless. So anything impeding your network connectivity, Get that fixed.


G
Gabriel Arghire

If you are on WSL and trying to access MongoDB from Windows, then you have to install Mongo for WSL.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now