4

I have just started learning about MongoDB and I am trying to host my Node.js application locally via MongoDB Server 6.0 (without using Mongoose or Atlas).

I copied the async JavaScript code given in the MongoDB documentation. I made sure to run mongod before executing the below code:

MongoDB server started

const { MongoClient } = require("mongodb");

// Connection URI
const uri =
  "**mongodb://localhost:27017**";

// Create a new MongoClient
const client = new MongoClient(uri);

async function run() {
  try {
    // Connect the client to the server (optional starting in v4.7)
    await client.connect();

    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully to server");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);

It's throwing an error:

Image of the error it's throwing

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sushant
  • 69
  • 7

2 Answers2

13

Problem is, the localhost alias resolves to IPv6 address ::1 instead of 127.0.0.1

However, net.ipv6 defaults to false.

The best option would be to start the MongoDB with this configuration:

net:
  ipv6: true
  bindIpAll: true

or

net:
  ipv6: true
  bindIp: localhost

Then all variants will work:

C:\>mongosh "mongodb://localhost:27017" --quiet --eval "db.getMongo()"
mongodb://localhost:27017/?directConnection=true&appName=mongosh+1.6.0

C:\>mongosh "mongodb://127.0.0.1:27017" --quiet --eval "db.getMongo()"
mongodb://127.0.0.1:27017/?directConnection=true&appName=mongosh+1.6.0

C:\>mongosh "mongodb://[::1]:27017" --quiet --eval "db.getMongo()"
mongodb://[::1]:27017/?directConnection=true&appName=mongosh+1.6.0

C:\>mongosh "mongodb://%COMPUTERNAME%:27017" --quiet --eval "db.getMongo()"
$ mongosh "mongodb://$HOSTNAME:27017" --quiet --eval "db.getMongo()"
mongodb://******:27017/?directConnection=true&appName=mongosh+1.6.0

If you don't run MongoDB as a service then it would be

mongod --bind_ip_all --ipv6 <other options>

NB, I don't like configuration

net:
  bindIp: <ip_address>

In my opinion this makes only sense on a computer with multiple network interfaces. However, since MongoDB does not support multiple interfaces it does not make much sense either. Also depending on your network settings, this IP may change at any time.

Use bindIp: localhost if you like to permit connections only from the local machine, e.g. while maintenance or when used as backend database for a web-service. Use bindIpAll: true if you like to permit connections also from remote machines.

Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
-1

You can try this:

mongoose.connect("mongodb://0.0.0.0:27017").then(() => {
  console.log("database connected)).catch((err) => {
  console.log("error while connecting to database")
})
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 2
    I don't think IP `0.0.0.0` will work, usually it means "**any** IP-Address". Apart from that, you should always print proper error instead of static text `"error while connecting to database"` which does not provide any information. – Wernfried Domscheit May 07 '23 at 15:33
  • I worked for me – Liang Jun 04 '23 at 12:47