简体   繁体   中英

Can't connect Mongoose to MongoDB with authentication

I created a user with readwrite role for MongoDB database myDatabase .

After starting MongoDB server with --auth , in Mongo shell:

mongo -u userName -p userPassword --authenticationDatabase myDatabase

Connects properly and can read/write myDatabase

In mongoose tried to connect to MongoDB using

mongoose.connect('mongodb://localhost/myDatabase', {user:'userName', password:'userPassword'});

Also

mongoose.connect('mongodb://localhost/myDatabase', {user:'userName', password:'userPassword', {auth: {authdb:'myDatabase'}});

Able to connect to myDatabase but get authorization error when read/write

MongoError: not authorized for query on myDatabase.myCollection

How can I fix this?

I would suggest to try the following:

1) Connect with string URI format https://docs.mongodb.org/manual/reference/connection-string/

2) Which version of mongoose are you using? According to the docs http://mongoosejs.com/docs/connections.html you should provide 'pass' property but you are passing it as 'password'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM