Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

249
Views
How to connect to a local database from server

trying to access to an external database in local via my server, I executed this code server side:

console.log('MONGODB_URI_LOCAL::', config.MONGODB_URI_LOCAL)

const mongooseLocal = require('mongoose');
const connectionOptions = { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false };
const conn = mongooseLocal.createConnection(config.MONGODB_URI_LOCAL, connectionOptions);
conn.on("error", console.error.bind(console, "connection error: "));
conn.once("open", async () => {
  console.log("Connected successfully");
});

And I get this error:

MongoError: Authentication failed

here is the format of the URI:

config.MONGODB_URI_LOCAL = mongodb://user_name:my_password@localhost:27017/my_db_name

This is the first time I try to connect to a local database so:

Is this possible to connect to a local database from server and read or write data ?

If yes How can I fix this issue ?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Here's an approach that you can use. This approach enables you to export your connection logic to other parts of your code.

const mongoose = require('mongoose')
const connect = async (callback) => {
    try{
        const connectionOptions = { 
            useCreateIndex: true, 
            useNewUrlParser: true, 
            useUnifiedTopology: true, 
            useFindAndModify: false 
        };
        await mongoose.connect(config.MONGODB_URI_LOCAL, connectionOptions)

        // On successful connection you can call your callback function
        callback()
    }catch(err){
        console.log(err)
        process.exit(1)
    }
}

connect()
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!