MongoDB Database connection failed to my express backend project

I am using ArchCraft. Recently I installed MongoDB on my OS and when I type show dbs it gives me the exact result. But when I run my backend project using npm start it says database connection failed. Then I check my MongoDB Compass but there was not any database created.

Screenshot_2022-03-20-05-14-08_1920x1080

//database connection
mongoose
  .connect(process.env.DATABASE_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => {
    console.log("Database Connection Successfull");
  })
  .catch(() => {
    console.log("Database Connection Failed");
  });

here is my database connection source code. here I am using dotenv. In this file I write this code DATABASE_URL = mongodb://localhost:27017/MEVN_STACK, When I use Ubuntu it works perfectly but now it isn’t working. Please help me to find out the reason.

Are default file permissions different between Ubuntu and Arch? Maybe you don’t have create permissions under Arch.

1 Like

I solved this problem recently. Replacing localhost by 0.0.0.0 is works for me. Can you please explain to me what’s the difference between localhost and 0.0.0.0

If requests are only accepted at localhost, the server must be able to process requests on 127.0.0.1 on. However, if no specific interface is specified, i.e. 0.0.0.0 is used, then the server listens on all interfaces, which means it may be something like 192.168.0.12 or 127.0.0.1 or something else. 0.0.0.0 means pretty much, it’ll accept a request on any interface.

1 Like