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

前端 未结 26 2618
轻奢々
轻奢々 2020-11-30 04:34

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(\"m         


        
相关标签:
26条回答
  • 2020-11-30 05:12

    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
    
    0 讨论(0)
  • 2020-11-30 05:14

    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.

    0 讨论(0)
  • 2020-11-30 05:16

    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
    
    0 讨论(0)
  • 2020-11-30 05:18

    I connected to a VPN and the connection accomplished. I was using school's WiFi which has some restrictions apparently.

    0 讨论(0)
  • 2020-11-30 05:20

    You need to initialize your mongoDB database first, you can run "mongod" in your terminal and then it will be working fine.

    0 讨论(0)
  • 2020-11-30 05:21

    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'.

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