MongoDB password with “@” in it

2020-01-24 20:17发布

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?

12条回答
放荡不羁爱自由
2楼-- · 2020-01-24 21:00

For those connecting with Mongo Compass.(MacOSx) Simply go to your cluster->Security(Tab)at mongodb.com

Cluster-Security

Then

edit your password(press the edit button on your username): enter image description here

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

EditPassword in Dialog Popup

Next:

Close your mongo db compass application if its running:(Quit Mongo)

Quit Mongo Compass

Next Step:

Return to the Overview Tab in mongodb.com.and select Connect

Return to OverView: return to overview and select connect

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): Connect with MongoDB Compass

select Version

Then:

Copy the URI String presented to you:

Copy Uri String

Reopen MongoDB Compass application:

And it will give you the option/popup to use the URI String detected:Click Yes uri string detected

Final step:

Enter your new password and Connect. Now your connection should now be successful.Enter new password and Connect

查看更多
▲ chillily
3楼-- · 2020-01-24 21:01

Try this one, my friends:

    mongoose.connect("mongodb://localhost:27017/test?authSource=admin",
                     {user: 'viettd', pass: 'abc@123'});

test is my db name
admin is my the db for authenticating
viettd is my username
abc@123 is my password

查看更多
ゆ 、 Hurt°
4楼-- · 2020-01-24 21:08

If 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.

MongoClient = require('mongodb').MongoClient;
let options = {
    useNewUrlParser: true,
    auth: {
        user: 'your_usr',
        password: 'your_pwd'
    }
};
MongoClient.connect(url, options, callback);

Or if you wanna include auth info in your url, do this:

let url = "mongodb://username:" + encodeURIComponent("p@ssword") + "@localhost:27017/database"
查看更多
Deceive 欺骗
5楼-- · 2020-01-24 21:09

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:

const mongoose = require('mongoose')
const mongodbUri = require('mongodb-uri')
let mongooseUri = mongodbUri.formatMongoose(config.mongo.uri)
mongoose.connect(mongooseUri, config.mongo.options)

I found this suggestion in mongoose's GitHub issue #6044.

查看更多
Anthone
6楼-- · 2020-01-24 21:10

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',

查看更多
迷人小祖宗
7楼-- · 2020-01-24 21:11

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

查看更多
登录 后发表回答