2

I have an user(username:hxh,password:1234) in my local mongodb,when I execute :

hxh@ubuntu:~/share/nodejs/appid2zgk7rv83v$ mongo
MongoDB shell version: 2.4.6
connecting to: test
> use admin
switched to db admin
> db.auth('hxh','1234')
1
>

This shows that my username and password is OK,but when I try to connect the mongodb in my nodejs app:

var Db = require('mongodb').Db;
var Server = require('mongodb').Server;  
var db = new Db('test', new Server('localhost', '27017')); 
db.open(function(err, db) {
  db.authenticate('hxh', '1234', function(err, result) { 
    .....
  });  
}); 

I got the error

MongoError: auth fails

So why I got this error?

2 Answers 2

1

try to create the user and pass in the test table: use test; db.createUser...

Sign up to request clarification or add additional context in comments.

Comments

0

May be you should pass your dbname i.e. 'admin' instead if 'test'

var db = new Db('admin', new Server('localhost', '27017'));

3 Comments

thanks,this works,but I just want to connect the test database,not admin what should I do?
digitalocean.com/community/articles/… may help you to connect to test database:
I know,just var secondDb = db.db("test");

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.