Home > Blockchain >  Can not connection to mongodb url after add admin user
Can not connection to mongodb url after add admin user

Time:02-16

I create a web that connects to MongoDB,everything works fine before create admin user in mongodb [role:root]
the url api wrote in .env file
before add user:
the url : mongodb://localhost:27017/shrimp
after:
the url :mongodb://admin:password@localhost:27017/shrimp
I try:
and then i try add other user still don't work
the url :mongodb://user:newpassword@localhost:27017/shrimp

the weird is I use mongodb://admin:password@localhost:27017/shrimp into mongoDB Compass it can connect, but use mongodb://user:newpassword@localhost:27017/shrimp it said Authentication failed.

EDIT
the tool I use:
mongoose
window 10
nextjs

the mongodb://user:newpassword@localhost:27017/shrimp can connect to mongoDB Compass now : mongodb://user2:1234@localhost:27017/shrimp?authSource=shrimp
and I try using same code to the dbconnection.js:

const URL ="mongodb://user2:1234@localhost:27017/shrimp?authSource=shrimp"


localhost:3000/api/some_api still empty.

I'm sure the dbconnection.js code has no problem, I think the problem could be the mongo URL format, or authentication.

do mongodb need to set something so that can let the server access or do I miss something?

CodePudding user response:

What library or driver are you using? Regardless, you could try adding ?authenticationDatabase=admin to end of the url.

CodePudding user response:

Seems you need to add a new database user in your MongoDB account to be able to gain access.

Go to MongoDB and log in to your account.
Choose Database Access under Security.
Click on "Add New Database User".
Create a new user (remember the username & password) and add user.

  • Related