Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

I used mongoose to connect my database to my app.

I got this unexpected error.

My app.js look like this

const express = require('express');
const mongoose = require('mongoose');
const authRoutes = require('./routes/authRoutes');
const cookieParser = require('cookie-parser');
const { requireAuth, checkUser } = require('./middleware/authMiddleware');
const run = require('./admin/connection');

const app = express();

// middleware
app.use(express.static('public'));
app.use(express.json());
app.use(cookieParser());

const {default : AdminBro} = require('admin-bro');
const buildAdminRouter = require('./admin/admin.router');
const options = require('./admin/admin.options');
const port = 3000;


const url = 'mongodb://localhost:27017/dbName';
let mongooseDb;
const databaseConnect = async () => {
  mongooseDb = await mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex:true })
  .then((result) => app.listen(3000))
  .catch((err) => console.log(err));

  const db = mongoose.connection;
  db.on('error', console.error.bind(console, 'connection error:'));
  db.once('open', function() {
    console.log("we are connected to database");
  });
  
    const admin = new AdminBro(options)
    const router = buildAdminRouter(admin);
    app.use(admin.options.rootPath, router);

  
};
databaseConnect();

I already installed mongodb database.

I already find similar question in stackoverflow but that answers didn't resolve my issue. Any help will be appreciated. Thanks

like image 577
Shizu Avatar asked Jan 10 '21 18:01

Shizu


People also ask

What is connect Econnrefused?

ECONNREFUSED error means that connection could not be made with the target service (in your case localhost:8080 ). Check your service running on port 8080.

How do I access MongoDB on localhost?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.


Video Answer


2 Answers

If It's MAC OS, run following command and try again:

brew services restart mongodb-community

the commands that follow will be the following:

Stopping mongodb-community... (might take a while)
==> Successfully stopped mongodb-community (label: homebrew.mxcl.mongodb-community)
==> Successfully started mongodb-community (label: homebrew.mxcl.mongodb-community)
like image 136
Robbie Winter Avatar answered Oct 02 '22 12:10

Robbie Winter


I faced a similar problem.Give a try to this one:

  1. Open your C drive.
  2. Make a folder named 'data'.
  3. Make another folder inside this data folder name 'db'.

You can see in the documentation that By default MongoDB try to search these directories, We have to create these manually

like image 36
Roshan Kumar Avatar answered Oct 02 '22 12:10

Roshan Kumar