36

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?

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" Commented Apr 21, 2013 at 3:16

5 Answers 5

52

Easier if you just use MongoClient

MongoClient.connect('mongodb://admin:password@localhost:27017/db', function (err, db) {
Sign up to request clarification or add additional context in comments.

2 Comments

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?
@Mavro 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.
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();
});

1 Comment

In order to leverage the DNS seed list, use a connection string prefix of mongodb+srv as explained
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/

Comments

5

This worked for me.

`mongodb://<username>:<password>@<host>:<port>/<dbname>?authSource=admin`

1 Comment

It saved me. Thanks
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.

1 Comment

The only thing this "new" answer does is to repeat what half of the other answers are already saying, including another well-received answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.