Connect user to local database

I have user name John which is defined for database mydb2 (below screen shot).
I use the following command to connect to this database. but it is not working
mongosh --host -u user -p password --authenticationDatabase John mydb2
do you know by chance the problem? thanks


Here is the documentation for the format of a MongoDB URI. Check your connection string against this documentation, please.

Hello, thanks for your prompt reply. But non of the instructions in that link works with the local connection. Or I am missing somthing. I want to connect to a database using a user of that database. your link has the following type of example which I think are suited for remote connections.


In the given example, change, to locahost:27017 … this assumes you’re running MongoDB locally on port 27017 and that you have it configured to listen on the loopback interface.

If the above is how you’re trying to connect and not a typo, then you’re trying to authenticate to the database John.

Try the following:

mongosh -u John -p --authenticationDatabase mydb2

You don’t need to pass in the host/port if you’re connecting to localhost on port 27017 (these are the defaults). If you don’t pass in a value for the password you will be prompted for it. This is more secure than providing the password as it won’t be saved in your history.

If you’re still having problems, please provide the error you’re receiving as that will help provide more specific help.


Dear Doug_Duncan,
thank you so much for this excellent reply. Very nice of you!
have good one,

1 Like