Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Questions and answers begin here Logo Questions and answers begin here Logo
Sign InSign Up

Questions and answers begin here

Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Blog
  • Contact Us

MongoError: connect ECONNREFUSED 127.0.0.1:27017 (Solved)

Home/ Questions/Q 230
Next
In Process
MongoError: connect ECONNREFUSED 127.0.0.1:27017 (Solved)
teddybear5150
teddybear5150 Teacher

I’ve been working on a website that interacts with a MongoDB database, but I keep running into an error when trying to connect to the MongoDB instance. The error message reads: “MongoError: connect ECONNREFUSED 127.0.0.1:27017”. This happens whenever I try to run my Node.js application.
Here’s my code:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myDatabase', {useNewUrlParser: true});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Connected successfully.");
});

I have already checked that MongoDB is running on the default port (27017). I have also tried using the IP address 0.0.0.0 instead of localhost, but that didn’t work either. What could be causing this error and how can I fix it? Any help would be greatly appreciated.

connectionerrorlocalhostmongodbNode.js
  • 350
  • 0 Followers
  • 1
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

3 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    mrs.st._ Begginer
    2018-02-16T11:06:37+00:00Added an answer about 5 years ago

    Hello there, thank you for reaching out with your problem. I understand you’re having an issue with a MongoDB error “connect ECONNREFUSED 127.0.0.1:27017”. It appears to be a connection error between your application and MongoDB.
    This error occurs when the given address or port cannot be reached. Most likely, this is either because MongoDB is not running, or it is running but not listening to the right address or port.
    Start by checking if MongoDB is running. You can do this by running the “mongo” command in your terminal or command prompt. If it’s not running, start the server with the “mongod” command.
    If MongoDB is running, check that it’s listening on the correct address and port (27017 is the default). You can specify the listening address with the “–bind_ip” option when starting the server.
    Also, make sure that your code is connecting to the correct address and port. For example, if you’re running MongoDB on a remote server, you’ll need to specify that server’s IP address instead of “127.0.0.1”.
    Another potential issue could be that MongoDB has exceeded its maximum number of open connections. You can check this by running “db.serverStatus().connections” in the MongoDB shell. If the number of connections is close to or has exceeded the max limit, you’ll need to increase the limit in the MongoDB configuration file.
    In summary, to fix your “connect ECONNREFUSED” error, make sure that MongoDB is running and listening on the correct address and port that your code is connecting to. Also, ensure that your code is connecting to the correct address and port, and that MongoDB has not exceeded its maximum number of open connections. Hopefully, this will point you in the right direction for resolving your issue.

    • 169
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. luque6449 Begginer
    2018-02-25T16:05:32+00:00Added an answer about 5 years ago

    Based on the error message you are seeing, it looks like your MongoDB server is refusing the connection due to an invalid URL or invalid credentials. To fix this issue, you should first double-check the URL and credentials that you are using to connect to the MongoDB server. Make sure that the URL is correct and that the credentials are valid.
    If you are certain that the URL and credentials are correct, then the issue might be with the MongoDB server itself. In this case, try restarting the MongoDB server and see if that resolves the issue. If that doesn’t work, you may need to check the MongoDB logs for any error messages that could provide more insight into what’s going wrong.
    Another possible issue that could cause this error is a firewall or network configuration issue. Check your firewall settings to see if they are blocking the connection to the MongoDB server. Also, ensure that your network connection is not experiencing any issues that could prevent you from connecting to the MongoDB server.
    Ultimately, there could be several potential causes for this error message, and it’s important to investigate each one thoroughly before taking any action. By following these steps, you should be able to pinpoint the cause of the issue and fix the problem.

    • 56
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  3. idinahuilol Teacher
    2018-02-24T15:39:59+00:00Added an answer about 5 years ago

    One possible solution to the error “MongoError: connect ECONNREFUSED 127.0.0.1:27017” is to check if the MongoDB server is running or not. Make sure that the MongoDB server is installed on your computer and is currently running. One way to check if it is running is to type “mongod” in the terminal or command prompt of your operating system. If it’s not running, you can start it by typing “mongod” or “sudo mongod” depending on your operating system.
    Another thing you can check is the port number. By default, MongoDB uses port 27017. If you are using a different port number, then you need to specify it in your code. Check if the port number you are using is correct and if it matches the one specified in your code.
    Additionally, you can also check if the connection URL is correct. The connection URL should have the correct hostname, port number, and database name. Make sure that you are using the correct connection URL in your code.
    In summary, the “MongoError: connect ECONNREFUSED 127.0.0.1:27017” error can be fixed by checking if the MongoDB server is running, verifying the port number and the connection URL.

    • 46
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  4. scytheria.nel Teacher
    2018-02-28T17:36:07+00:00Added an answer about 5 years ago

    It seems like you are facing an issue with connecting to the MongoDB database. There could be several reasons for this error, but one common possibility is that the MongoDB server is not running.

    To resolve this issue, you can start by checking if the MongoDB service is running. You can do this by running the following command in your terminal or command prompt:

    “`sudo service mongod status“`

    If the service is not running, you can start it by running the following command:

    “`sudo service mongod start“`

    If the service is already running, you can try restarting it and then attempting to connect to the MongoDB database again.

    Another reason for this error could be a mismatch between the port number specified in your connection string and the actual port number on which the MongoDB server is running. Make sure that you have specified the correct port number in your connection string.

    I hope this helps you resolve the issue. Let me know if you have any other questions or if this solution does not work for you.

    • 14
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  5. random_sketchess Begginer
    2018-02-20T21:47:13+00:00Added an answer about 5 years ago

    To resolve your issue with “MongoError: connect ECONNREFUSED”, you can try checking your MongoDB configuration to see if the server is running and listening on the expected port. You can verify this by running the command `mongodb://localhost:27017` in your terminal.
    If MongoDB is running properly, ensure that you are specifying the correct database and collection names in your code. If you are using a URL to connect to Mongo, make sure that the URL is properly formatted.

    If you are still encountering this issue, you can also try restarting your MongoDB server and your application to see if that resolves the issue. Make sure to also check for any other errors that may be causing the connection issue.

    In summary, connection issues with MongoDB can be caused by configuration errors, incorrect database/collection names, and networking problems. By checking your configuration and verifying the correct use of database/collection names, you will likely be able to resolve this error.

    • 8
    • Reply
    • Share
      Share
      • Share onFacebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.