Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

98
Visualizações
Mongoose.connect() fails when database is specified in connection string

I'm running a local MongoDB server and am trying to connect to it from a node.js server using the Mongoose library.

I'm using SCRAM-SHA-1 as the authentication mechanism

I'm able to connect to it with the admin account when I don't specify a database to connect to, using a connection string such as:

mongodb://{username}:{password}@{address}:{port}?authMechanism=SCRAM-SHA-256

But when I try to specify which database to connect to, using a connection string such as:

mongodb://{username}:{password}@{address}:{port}/database123?authMechanism=SCRAM-SHA-256

the authentication fails.

For reference, my code looks like:

    const mongoose = require('mongoose');

    let conn_string = `mongodb://mongo_uname:mongo_pass@192.168.1.1:3000/database123?authMechanism=SCRAM-SHA-256`

    mongoose
    .connect(database_uri, {
        useUnifiedTopology: true
    })
    .then(() => console.log('MongoDB database Connected...'))
    .catch((err) => console.log(err))

Is there a different way to specify which database to connect to with mongoose, or is there something I can do differently when constructing the connection string to make it work?

Cheers.

almost 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

import mongoose from "mongoose";

const mongoDB = "mongodb://localhost:27017/twlight";
mongoose.Promise = global.Promise;

mongoose.connect(mongoDB, { useNewUrlParser: true })
.then(() => {
  console.log("Database is connected");
},
(err) => {
console.log("There is problem while connecting database " + err);
});

Please try

Note: twilight is database name

almost 4 years ago · Santiago Trujillo Relatório

0

If you are using Mongoose v6+, you should NOT specify useUnifiedTopology.

That is deprecated and if you include it, Mongoose will throw the error. Try to include the connection string only:

mongoose.connect(database_uri)
almost 4 years ago · Santiago Trujillo Relatório

0

My error ended up being in the configuration of the database user I was trying to connect with, they weren't even set up to read or write to the database I was attempting to connect to.

almost 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda