How to connect with username/password to mongodb using native node.js driver
Asked Answered
W

5

34

I'm using native mongo driver in Joyent cloud, the node.js application runs fine locally but in Joyent when I run with the username/password that they provided it fails to connect.

This is the code I use to connect:

var db = new MongoDB(dbName, new Server('localhost', 27017 , {auto_reconnect: true}), {w: 1});
db.open(function(e, db){
if (e) {
    console.log(e);
} else{
    console.log('connected to database :: ' + dbName);
    //db.admin().authenticate('admin', '+(uihghjk', function(de , db){
    // if(e){
    //     console.log("could not authenticate");
    // }else {
    //console.log('connected to database :: ' + dbName);
    // }
    // });
}
});

What's stopping me from connecting successfully?

Wadley answered 20/4, 2013 at 19:24 Comment(1)
The above commented code works fine for authentication. The problem was they mixed up with credentials provided for mongodb, to verify login and password, ssh to joyent and enter $(mdata-get mongodb_pw), verify the given pswd works in "mongo -uadmin -p$(mdata-get mongodb_pw) admin"Wadley
M
51

Easier if you just use MongoClient

MongoClient.connect('mongodb://admin:password@localhost:27017/db', function (err, db) {
Menace answered 29/4, 2013 at 5:19 Comment(2)
This is very correct, but how can I hide the credentials from the repository? is there a way to use a hash key or something like that?Unskillful
@Unskillful in one project with mongoose I see the whole path with credentials stored in a config file which is not committed to repository, then variable used in connect.Beckon
C
9

the working code would be like this

const MongoClient = require('mongodb').MongoClient;
const uri = "mongodb+srv://<username>:<password>@cluster0-saugt.mongodb.net/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(err => {
    // creating collection
    const collection = client.db("test").collection("devices");
    // perform actions on the collection object
    client.close();
});
Crustaceous answered 29/6, 2019 at 9:22 Comment(1)
In order to leverage the DNS seed list, use a connection string prefix of mongodb+srv as explainedExplicative
A
6

Standard URI connection scheme is:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

For Example:

mongodb://admin:[email protected]/

Reference: https://docs.mongodb.com/manual/reference/connection-string/

Agraphia answered 12/9, 2018 at 6:20 Comment(0)
T
2

Use <username> : <password> right after // in the URL.

I am using Mongoose to connect and my username and password is admin admin

mongodb+srv://admin:[email protected]

This worked for me.

Tidwell answered 24/12, 2021 at 11:58 Comment(1)
The only thing this "new" answer does is to repeat what half of the other answers are already saying, including another well-received answer.Negotiation
I
2

This worked for me.

`mongodb://<username>:<password>@<host>:<port>/<dbname>?authSource=admin`
Ileenileitis answered 27/3 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.