I get the following error:
Warning { MongoError: failed to connect to server [mongodb:27017] on first connect
at Pool. (/Users/michaelks
I was running mongod in a PowerShell instance. I was not getting any output in the powershell console from mongod. I clicked on the PowerShell instance where mongod was running, hit enter and and execution resumed. I am not sure what caused the execution to halt, but I can connect to this instance of mongo immediately now.
I was trying to connect mlab with my company wifi connection and it was giving this error. But when I switched to personal network it worked and got connected.
if it is cluster MongoDB then you need to add your current IP to the cluster, to add your current IP address you need to complete few steps below-
step 1- go to login page of Mongodb and login with valid credential - https://cloud.mongodb.com/user#/atlas/login
step 2- CLick Network Access from left sidebar under Security Section
Step 3 - Click Add IP Address
Step 4 - Click Add Current IP Address or Allow Connection From Any Where
now try to connect - npm start
and for Local MongoDB use mongo String like "mongodb+srv://username:pass%40ord@clastername-blqnk.mongodb.net/test?retryWrites=true&w=majority"
password must be encoded like example string
Adding one more answer as the solution is from different contexts:
I connected to MongoDB Atlas and whitelisted my machine's IP by following the below steps:
To connect to mongodb
with mongoose
, you can use :
mongoose.connect('mongodb://localhost/users_test');
or
mongoose.connect('localhost/users_test');
or
mongoose.connect('localhost','users_test');
But not mongoose.connect('mongodb:localhost/users_test');
, it doesnt match the right hostname (mongodb
instead of localhost
)
While connected to a wifi network, mongodb://localhost/db_name
worked as expected.
When I wasn't connected to any wifi network, this couldn't work. Instead I used,
mongodb://127.0.0.1/db_name
and it worked.
Probably a problem to do with ip configurations.