MongoServerSelectionError: connect ECONNREFUSED ::1:27017 - node.js

Before updating nodejs my code was working fine but after updating my nodejs version.
whenever i am connecting to the server(localhost), after connecting in a few seconds it gives me this error.
enter image description here
and in vs code console it gives
enter image description here
and this is my code:
enter image description here
I am trying to connect to my local database using the localhost server.

The error message indicates the application is attempting to connect to the IPv6 address ::1, i.e. localhost.
The code you have shows uses IPv4 localhost address 127.0.0.1, and appears to connect successfully at that point.
Some time later, another connection is attempted that goes to the IPv6 address.
This suggests that both:
something later uses localhost instead of an IP, and the operating system resolves that to ::1
mongod is not listening on the IPv6 address
To fix this, you could do one or more of:
modify the mongod.conf so that it listens on ::1 in addition to 127.0.0.1
search the code to find where localhost is used and change it to the IP4 address
modify the local operating system so localhost only resolves to an IP4 address (this is probably a bad idea)

Related

Why can I connect to Mongo in node using 127.0.0.1, but not localhost?

I'm connecting to Mongo using the Node library, and mongo is up and running on port 27017.
If I set my uri to mongodb://127.0.0.1:27017 it connects, but if I set it to mongodb://localhost:27017 it doesn't connect (times out).
I'm on Linux, and my /etc/hosts looks like this:
127.0.0.1 localhost
::1 localhost
My guess is it has something to do with ipv6, but I have very little understanding of ipv6 to be honest. Can someone explain what's happening here, and if I should do something differently to be able to connect to localhost?
As you pointed out, it seems that localhost resolves to IPv6 address ::1 and not 127.0.0.1.
You can keep using 127.0.0.1 or another option would be changing the address mongod service binds to, for instance, ::1 (you can bind to IPv4 and IPv6 at the same time).

how to connect same mongodb (in compass using hostname: localhost and port : 27017) local in 2 different PC's [duplicate]

I follow this mongoose document enter link description here
mongoose.connect('mongodb://localhost/waterDB');
Using This, I can connect local machine waterDBmongoDB DataBase
My Personal Machine Local IP : 192.168.1.5
My Server Machine Local IP : 192.168.1.100
Both machine have waterDB DataBase . There is no username and password for both DB
I wanted to connect Server Machine waterDB Inside My Personal Machine.
According To This : mongoose.connect('mongodb://username:password#host:port/database?options...');
I try : mongoose.connect('mongodb://192.168.1.100:27017/waterDB');
But,
MongoError: failed to connect to server [192.168.1.100:27017] on first connect
at null.<anonymous> (/home/water/node_modules/mongodb-core/lib/topologies/server.js:313:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
..........
Any solution for err ?
Thank (#_#)
It might be a problem with your MongoDB instance listening on localhost only.
You can change the bind address in MongoDB's configuration file. The config file may be located in /etc/mongodb.conf or /etc/mongod.conf. There are also 2 config file formats:
Old format (still supported):
bind_ip = 0.0.0.0
YAML (version 2.6+):
net:
bindIp: 0.0.0.0
After changing the config file you have to restart the MongoDB server.
Try without the Port no.
mongoose.connect('mongodb://192.168.1.100/waterDB');
this should work for you.
But make sure both are connected on the same network, if you are connected on other network than your server is, then it wont work
You will have to use SSH tunnel in this case. Refer to the following link which shows how you can create SSH tunnel.
Node.js SSH Tunneling to MongoDB using Mongoose
For me (using windows and mongo version 6, and remotely connecting from Mac), after changing config file to
net:
port: 27017
bindIp: "*" # OR 0.0.0.0
Then starting server with: mongod, it was still binding to 127.0.0.1 (localhost). I had to start the server with: mongod --bind_ip_all

How to configure apache in local development in rails

I am trying setup a loacl domain on my pc for that I have apache server install I want to open my rails s inside this abcd.com instead of 127.0.0.1:3000. In my host I have changed host file like this:
127.0.0.1 localhost
127.0.0.1 abcd.com
But when I am trying to open abcd.com with this command I am getting this message:
Address already in use - bind(2) for "127.0.0.1" port 80 (Errno::EADDRINUSE)
When I stop apache it works on localhost any help
You need to write all hostnames behind the ip like this :
127.0.0.1 localhost abcd.com

ECONNREFUSED when using node with nano and couchdb

I was using nodejs + nano + couchdb for my application successfully up until today. For some reason all of a sudden I'm getting ECONNREFUSED when I try to run my application. If I try to query the database using the web browser or using a different application (java application) it works fine. I'm uncertain why just in this scenario it stopped working. I've been researching for the past 2 days and can't find any help. I believe this might have something to do with too many open connections, but that's a little bit out of my realm of knowledge. Can anyone provide me with any insight on debugging this issue or any direction I could go in? I should mention this couchdb lives on iriscouch
Add more information about stack that you're using. But basically it's server machine doesn't want to allow connecting. Also try run your app with DEBUG=*, nano will log via console.log almost everything.
E.g. change in package.json start command to node changetoyourapp.js DEBUG=*
I faced yesterday same issue with nodejitsu/iriscouch. Issue disappeared after some restarts.
Check the version of your node vs the expected node version of nano. It is possible that nano does not work with node > 16.
This is down to Node v18 now preferring an IPv6 address over and IPv4 address if two exist for the same hostname.
i.e. if your /etc/hosts contains entries like this:
127.0.0.1 localhost
::1 localhost
Node v16 will say that "localhost" resolves to 127.0.0.1 where Node v18 will say "localhost" resolves to ::1, the IPv6 equivalent. As CouchDB doesn't listen on an IPv6 port by default, then a connection to ::1 will be refused.
Solutions:
Use 127.0.0.1 instead of localhost in your URLs.
Use a domain name that resolves unambiguously to an IPv4 address e.g. 127.0.0.1 my.pretend.host in your /etc/hosts file.
Revert to Nodev16 which preferred IPv4 addresses in its dns lookup.
Make CouchDB bind to an IPv6 address by changing bind_address = ::1 in couchdb.ini. You can then do curl 'http://USER:PASS#[::1]:5984/.
See
https://github.com/apache/couchdb-nano/issues/313#issuecomment-1321760360

How to disable loopback interface in Linux (Fedora)?

So that requests to localhost are treated as if coming from remote host in LAN?
Have you tried updating your /etc/hosts file, replacing:
127.0.0.1 localhost
With your actual IP address? e.g.,
192.168.0.42 localhost
(This doesn't actually disable the loopback address, you can still connect to 127.0.0.1, but connecting to localhost should come in through your network interface, which I believe is what you're asking)

Resources