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

前端 未结 26 2619
轻奢々
轻奢々 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:04

    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.

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

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

    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, along with the correct installation of mongoDB from the link given in the above solution, and including the correct promise handling code:

    mongoose.connect('mongodb://localhost/testdb').then(() => {
    console.log("Connected to Database");
    }).catch((err) => {
        console.log("Not Connected to Database ERROR! ", err);
    });
    
    0 讨论(0)
  • 2020-11-30 05:06

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

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

    just run mongod in terminal on the base folder if everything has been set up like installing mongo db and the client for it like mongoose. After running the command run the project file that you are working on and then the error shouldn't appear.

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

    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.

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