0

I've an issue trying to connect with my database. Already tried the following solutions that I found on google:

  • restarting MongoDB service on Mac
  • manually open db with cmd located on bin file of MongoDB

It seems Mongodb is running - as it's connected in VS and Mongodb Compass. But I can't figure out the issue. Can anyone help me? Thanks :)

const express = require('express')
const app = express()
const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/subscribers')
const db = mongoose.connection
db.on('error', (error) => console.error(error))
db.once('open', () => console.log('Connected to Database'))

app.listen(3000, () => console.log('Server Started'))
may_
  • 1
  • 3
  • What's the output of `ps aux | grep mongo`? – Rafael Dec 28 '21 at 20:26
  • Hi, I get : `leticiaamorim 403 0.2 0.2 5537912 19396 ?? S 12:10pm 2:08.85 /usr/local/opt/mongodb-community/bin/mongod --config/usr/local/etc/mongod.conf leticiaamorim 7875 0.0 0.0 4268408 696 s001 R+ 9:03pm 0:00.00 grep mongo` – may_ Dec 28 '21 at 21:08
  • Mongoose is trying to connect to MongoDB over your IPv6 loopback address, but the MongoDB server on your machine appears to be only bound to IPv4 addresses. My guess is that your `/etc/hosts` file only has an IPv6 `localhost` entry. The following two options are the most straightforward: 1) Tell MongoDB to bind to the IPv6 loopback address by passing the `--ipv6` option when starting `mongod`. 2) Substitute `127.0.0.1` for `localhost` in your connection string. See [MongoDB IP Binding](https://docs.mongodb.com/manual/core/security-mongodb-configuration/) for more information. – Rafael Dec 28 '21 at 22:38
  • Does this answer your question? [MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 wont get fixed](https://stackoverflow.com/questions/69763130/mongooseserverselectionerror-connect-econnrefused-127017-wont-get-fixed) – Joe Dec 29 '21 at 05:52
  • 1
    @Rafael '--ipv6' solved my problem, thank you!! ;) – may_ Dec 29 '21 at 11:55

0 Answers0