Mongodb: failed to connect to server on first connect

前端 未结 13 2970
陌清茗
陌清茗 2020-12-15 15:52

I get the following error:

Warning { MongoError: failed to connect to server [mongodb:27017] on first connect
    at Pool. (/Users/michaelks         


        
相关标签:
13条回答
  • 2020-12-15 16:06

    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.

    0 讨论(0)
  • 2020-12-15 16:13

    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.

    0 讨论(0)
  • 2020-12-15 16:14

    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

    0 讨论(0)
  • 2020-12-15 16:17

    Adding one more answer as the solution is from different contexts:

    MongoDb Atlas problem solution

    I connected to MongoDB Atlas and whitelisted my machine's IP by following the below steps:

    • Go to the security tab

    • Go to the network access tab under the security tab

    • Go to the IP whitelist tab

    • Enter the IP address of your machine

    0 讨论(0)
  • 2020-12-15 16:19

    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)

    0 讨论(0)
  • 2020-12-15 16:19

    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.

    0 讨论(0)
提交回复
热议问题