I'm trying to connect to a MongoDB database with a username and password using Mongoose in Node.js. All the docs say that the connection string should look like
mongodb://username:password@host:port/db
However, the password contains the '@' character in it. How can I make a connection string out of this that mongoose will understand? Can I escape the '@' in the password or is there another method of connecting I have to use?
For those connecting with Mongo Compass.(MacOSx) Simply go to your cluster->Security(Tab)at mongodb.com
Then
edit your password(press the edit button on your username):
You will get a modal/popup/dialog:Press edit password underneath your name(the button is greyed out by default but appears just underneath your name)->Then press Update User
Next:
Close your mongo db compass application if its running:(Quit Mongo)
Next Step:
Return to the Overview Tab in mongodb.com.and select Connect
Return to OverView:
Next Step:
In the popup dialog select Connect with MongoDB Compass then in the next view select a version you want to use(preferably VersionNumber earlier):
Then:
Copy the URI String presented to you:
Reopen MongoDB Compass application:
And it will give you the option/popup to use the URI String detected:Click Yes
Final step:
Enter your new password and Connect. Now your connection should now be successful.
Try this one, my friends:
test
is my db nameadmin
is my the db for authenticatingviettd
is my usernameabc@123
is my passwordIf you use Mongodb native Node.js driver, this is what works for me as of 3.1 driver version. Assume your url doesn't contain auth info.
Or if you wanna include auth info in your url, do this:
This solution requires an extra dependency, but it was what finally worked for me.
Add
mongodb-uri
to your project and the following lines to your code:I found this suggestion in
mongoose
's GitHub issue #6044.I have also faced the same issue. I have solved by adding encoded password into connection string. And it works just well.
(1) Encode your password from https://www.url-encode-decode.com
(2) Replace your password with encoded one.
(3) It should work well.
For example:
Actual Password: ABCDEX$KrrpvDzRTy`@drf.';3X
Encoded Password: ABCDEX%24KrrpvDzRTy%60%40drf.%27%3B3X
mongodb://user1:ABCDEX%24KprpvDzRTy%60%40drf.%27%3B3X@dstest.com:1234,ds1234-test.com:19889/mongo-dev?replicaSet=rs-ds123546978&ssl=true',
sometimes you need to connect to the DB using other tools that accept string only as connection string. so just change the @ sign with %40