Reputation: 1250
I'm new in nodeJS, started learning by following a trailer on youtube, everything goes well until I added the connect function if mongodb,
mongo.connect("mongodb://localhost:27017/mydb")
when I run my code on cmd (node start-app), get the following error,
MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
Could someone explain me which step I missed ? my code :
var express = require("express");
var MongoClient = require('mongodb');
var url = "mongodb://localhost:27017/mydb";
var webService = require("./webService");
var server = express();
MongoClient.connect(url, function (err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
server.use(express.urlencoded({ extended: true }));
server.set('views', __dirname);
server.get('/', function (request, response) {
response.sendFile(__dirname + '/MainPage.html');
});
server.get('/Sign', function (request, response) {
response.render(__dirname + '/Sign.ejs');
});
server.post("/signUp", webService.signUp);
server.post("/createUser", webService.createUser);
server.listen(5500);
Upvotes: 96
Views: 399141
Reputation: 19
If this problem faces you in localhost (windows),here you are these steps: 1)open run.exe 2)search for services.msc 3)explore for mongodb service after that restart it
Upvotes: -1
Reputation: 229
While trying to connect to mongoDb through mongoose, there's the connection string as below:
mongodb+srv://userName:[email protected]/dbToConnect?retryWrites=true&w=majority&appName=Cluster0
Here
In some cases both(for me this is the case). After updating, got connected to remote mongoDb.
Upvotes: 0
Reputation: 5180
Following the logic behind @CoryM's answer above :
After trying EVERY solution google came up with on stack overflow, I found what my particular problem was. I had edited my hosts file a long time ago to allow me to access my localhost from my virtualbox.
Removing this entry solved it for me...
I had edited my hosts file too for Python Machine Learning setup 2 months ago. So instead of removing it because I still need it, I use 127.0.0.1
in place of localhost
and it worked :
mongoose.connect('mongodb://127.0.0.1/testdb')
Latest edit: Run mongosh
in your terminal to get the Connecting to:
URL info
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2
Upvotes: 17
Reputation: 437
Let's say you do have mongoDB
installed on your PC, in that case, Some times it happens, maybe PC/MongoDB mall function, that your pc doesn't connect to mongoDB local host. I just simply ran mongod
in my terminal to connect manually.
just run
mongod
in your terminal
Upvotes: 0
Reputation: 2204
I faced the same issue and I have done some research about it and found that this problem is solved by running the command on your terminal.
sudo service mongod start
then run mongo
on terminal
Upvotes: 20
Reputation: 8220
I solved this problem by upgrading major version of mongoose:
Before doing this, make sure (using mongo shell) that you have the correct URL and a running mongo server is available at that URL and the problem still persists.
"dependencies": {
- "mongoose": "^5.4.13",
+ "mongoose": "^6.2.4",
}
Upvotes: 3
Reputation: 4985
Your IP address probably changed.
If you've recently restarted your modem, this changes your IP which was probably whitelisted on Atlas.
Soooo, you'll need to jump back onto Atlas and add your new IP address to the whitelist under Security>Network Access.
Upvotes: 9
Reputation: 211
this was my erros:
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.2 MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
SOLUTION:
The best Answes is if you using mac M1 and M2
use restrat with sudo like below
MongoNetworkError solution
sudo brew services restart [email protected]
Upvotes: 3
Reputation: 159
For me the problem resolved when I started the MongoDB on port other than 27017
. Even though nothing was running on 27017
but the problem resolved when I started it on another port.
To do that navigate to the /etc/mongod.conf
and change the port: 27017
to some other port like port: 27019
.
Then restart the service by:
sudo systemctl restart mongod.service
.
And then try to connect to MongoDB by specifying the --port
parameter like:
mongod --port 27019
, or
mongo --port 27019
Best!
Upvotes: 1
Reputation: 1895
I was trying to connect, without starting the service. This is how i fixed the error (MacOS env).
$ brew services start [email protected]
$ mongosh // connected to db and fixed the error.
$ brew services stop [email protected]
Upvotes: 0
Reputation: 118
In my case the problem was that there was another instance of mongoDB server running I had shutdown my computer without stopping the server hence when I tried running mongosh it gives me that error. Try restarting the computer it will shutdown all the servers and the erro was gone.
Upvotes: 0
Reputation: 123
if you are a Mac user just upgrade your homeBrew from terminal:
$ brew upgrade
$ mongod --config usr/local/etc/mongod.config
$ Xcode-select --install
$ mongo
Upvotes: 1
Reputation: 614
If the error happens on macbook run this command to keep the mongodb server running.
mongod --config /usr/local/etc/mongod.conf --fork
The issue majorly is that your mongodb server is rejecting the connection it might be that the server is not on/active eventhough it has been installed on your macbook.
Upvotes: 0
Reputation: 169
Your node version has been upgraded. So please follow below step
usefull command of nvm
nvm list available (will show available version of node)
Upvotes: -1
Reputation: 12293
You have to install MongoDB
database server first in your system and start it.
Use the below link to install MongoDB
https://docs.mongodb.com/manual/installation/
If you have installed MongoDB check if the server is in which state (start/stop). Try to connect through mongo shell client.
Upvotes: 61
Reputation: 1631
You have to install MongoDB database server first in your system and start it.
Use the below link to install MongoDB
If you have already installed MongoDB database in your system then you have to check that your DB is in start position or not with the help of following steps:
Upvotes: 1
Reputation: 225
So when none of the above solutions worked for me, after installing everything correctly, I thought to restart the system.
It's working now.
Note that I did everything said above, but no luck. The only restart worked for me.!! You may also want to restart once.
Upvotes: 0
Reputation: 9
Your firewall blocked port 27017 which used to connect to MongoDB.
Try to find which firewall is being used in your system, e.g. in my case is csf, config file placed at
/etc/csf/csf.conf
find TCP_IN & TCP_OUT as follow and add port 27017 to allowed incoming and outgoing ports
# Allow incoming TCP ports
TCP_IN = "20,21,22,25,53,80,110,143,443,465,587,993,995,2222,27017"
# Allow outgoing TCP ports
TCP_OUT = "20,21,22,25,53,80,110,113,443,587,993,995,2222,27017"
Save config file and restart csf to apply it:
csf -r
Upvotes: -1
Reputation: 741
If the mongoDB server is already installed and if you are unable to connect from a remote host then follow the below steps,
Login to your machine, open mongodb configuration file located at /etc/mongod.conf
and change the bindIp
field to specific ip / 0.0.0.0
, after that restart mongodb server.
sudo vi /etc/mongod.conf
The file should contain the following kind of content:
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
storage:
journal:
enabled: true
processManagement:
fork: true
net:
bindIp: 127.0.0.1 // change here to 0.0.0.0
port: 27017
setParameter:
enableLocalhostAuthBypass: false
Once you change the bindIp
, then you have to restart the mongodb, using the following command
sudo service mongod restart
Now you'll be able to connect to the mongodb server, from remote server.
Upvotes: 3
Reputation: 8645
My problem was the wrong port number for mongoDB
server.
I had:
DATABASE_URL= "mongodb://localhost:3000/node-express-mongodb-server"
in my .env
file (my environmental variables), but I had written it before running mongoDB server
. So when I ran the mongoDB server
, it wrote a different port number and I had to change it. I changed it to the right port number (which was written on my cmd window by mongoDB
):
DATABASE_URL= "mongodb://localhost:27017/node-express-mongodb-server"
and now it works fine.
Upvotes: 1
Reputation: 315
I don't know if this might be helpful, but when I did this it worked:
Command mongo
in terminal.
Then I copied the URL which mongo command returns, something like
mongodb://127.0.0.1:*port*
I replaced the URL with this in my JS code.
Upvotes: 2
Reputation: 1
You need to initialize your mongoDB database first, you can run "mongod" in your terminal and then it will be working fine.
Upvotes: -2
Reputation: 172
I guess you must be connecting to cloud.mongodb.com to your cluster.
One quick fix is to go to the connection tab and add your current IP address(in the cluster portal of browser or desktop app). The IP address must have changed due to a variety of reasons, such as changing the wifi.
Just try this approach, it worked for me when I got this error.
Upvotes: 0
Reputation: 512
This worked for me.
mongoose.Promise = global.Promise;
.connect(
"mongodb://127.0.0.1:27017/mydb",
{ useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true}).then(db => {
console.log("Database connected");
}).catch(error => console.log("Could not connect to mongo db " + error));
I was using localhost, so i changed it to:
mongodb://127.0.0.1:27017/mydb
Upvotes: 4
Reputation: 7206
I connected to a VPN and the connection accomplished. I was using school's WiFi which has some restrictions apparently.
Upvotes: -1
Reputation: 1
This one helped me. Try creating a new folder, if your MongoDB is installed in C:\Program Files the folder should be called db and in a folder data. C:\data\db
When you start the mongod there should be a log where the db 'isnt found'.
Upvotes: 0
Reputation:
If Install before Mongodb
Just Start with this code :
brew services start mongodb-community
next => mongod
If Not Install before this Way
1.brew tap mongodb/brew
2.brew install mongodb-community
3.brew services start mongodb-community
4.mongod
Upvotes: -3
Reputation: 197
mongoose.connect('mongodb://localhost:27017/').then(() => {
console.log("Connected to Database");
}).catch((err) => {
console.log("Not Connected to Database ERROR! ", err);
});
Better just connect to the localhost Mongoose Database only and create your own collections. Don't forget to mention the port number. (Default: 27017)
For the best view, download Mongoose-compass for MongoDB UI.
Upvotes: 0
Reputation: 19
I had this issue while working at the local Starbucks and I remembered that when I initially set up my database through Mongo Atlas. I set my IP address to be able to access the database. After looking through several threads, I changed my IP address on Atlas and the issue went away. Hope this helps someone.
Upvotes: 1
Reputation: 1054
Many of them don't add this, especially in AWS EC2 Instance, I had the same issue and tried different solutions. Solution: one of my database URL inside the code was missing this parameter 'authSource', adding this worked for me.
mongodb://myUserName:MyPassword@ElasticIP:27017/databaseName?authSource=admin
Upvotes: 56