node.js 包含“@”的 MongoDB 密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7486623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:35:39  来源:igfitidea点击:

MongoDB password with "@" in it

mongodbnode.jsmongoose

提问by iZ.

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

我正在尝试使用 Node.js 中的 Mongoose 使用用户名和密码连接到 MongoDB 数据库。所有的文档都说连接字符串应该看起来像

  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?

但是,密码中包含“@”字符。我怎样才能从中创建一个猫鼬会理解的连接字符串?我可以转义密码中的“@”还是必须使用其他连接方法?

回答by Andrey Hohutkin

Use this syntax:

使用以下语法:

mongoClient.connect("mongodb://username:p%40ssword@host:port/dbname?authSource=admin", { 
        useNewUrlParser: true
    }, function(err, db) {

    }
);

回答by vanduc1102

If your password has special characters:

如果您的密码有特殊字符:

const dbUrl = `mongodb://adminUsername:${encodeURIComponent('adminPassword')}@localhost:27017/mydb`;

回答by JohnnyHK

Use the optionsparameter of the mongoose.connectcall to specify the password instead of including it in the URL string:

使用调用的options参数mongoose.connect指定密码,而不是将其包含在 URL 字符串中:

mongoose.connect('mongodb://localhost/test',
                 {user: 'username', pass: 'p@ssword'},
                 callback);

回答by prisan

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.

(1) 从https://www.url-encode-decode.com编码您的密码
(2) 用编码的密码替换您的密码。
(3)它应该工作良好。

For example:
Actual Password: ABCDEX$KrrpvDzRTy`@drf.';3X
Encoded Password: ABCDEX%24KrrpvDzRTy%60%40drf.%27%3B3X

例如:
实际密码:ABCDEX$KrrpvDzRTy`@drf.';3X
编码密码:ABCDEX%24KrrpvDzRTy%60%40drf.%27%3B3X

mongodb://user1:ABCDEX%24KprpvDzRTy%60%40drf.%27%[email protected]:1234,ds1234-test.com:19889/mongo-dev?replicaSet=rs-ds123546978&ssl=true',

mongodb://user1:ABCDEX%24KprpvDzRTy%60%40drf.%27%[email protected]:1234,ds1234-test.com:19889/mongo-dev?replicaSet=rs-ds123546978&ssl=true

回答by Viet Tran

Try this one, my friends:

试试这个,我的朋友:

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

testis my db name
adminis my the db for authenticating
viettdis my username
abc@123is my password

test是我的数据库名称
admin是我的身份验证数据库
viettd是我的用户名
abc@123是我的密码

回答by AKASH

use pwd instead pass, that worked for me for version3.2

使用 pwd 代替 pass,这对我的 version3.2 有用

mongoose.connect('mongodb://localhost/test',
                 {user: 'username', pwd: 'p@ssword'},
                 callback);

回答by Thierry

None of the solutions mentioned above worked for me. I researched it further and found out that I had to include the useNewUrlParser parameter.

上面提到的解决方案都不适合我。我进一步研究了它,发现我必须包含 useNewUrlParser 参数。

mongoose.connect(db, {
    useNewUrlParser : true
    },
    err => {
    if (err){
        console.error('Error: ' + err)
    }
    else{
        console.log('Connected to MongoDb')
    }
})

From what I understand, you need a specific version of MongoDB in order to use this. For more details, check Avoid “current URL string parser is deprecated” warning by setting useNewUrlParser to true

据我了解,您需要一个特定版本的 MongoDB 才能使用它。有关更多详细信息,请检查通过将 useNewUrlParser 设置为 true 来避免“当前 URL 字符串解析器已弃用”警告

It is to get rid of the warning but clearly the version also affect the required parameter.

这是为了摆脱警告,但显然版本也会影响所需的参数。

I haven't tested all special characters but it definitely works with '@#$'.

我还没有测试所有特殊字符,但它绝对适用于“@#$”。

Hope this helps.

希望这可以帮助。

回答by toadead

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.

如果您使用 Mongodb 原生 Node.js 驱动程序,那么从 3.1 驱动程序版本开始,这对我有用。假设您的网址不包含身份验证信息。

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:

或者,如果您想在 url 中包含身份验证信息,请执行以下操作:

let url = "mongodb://username:" + encodeURIComponent("p@ssword") + "@localhost:27017/database"

回答by dang

sometimes you need to connect to the DB using other toolsthat accept string only as connection string. so just change the @ sign with %40

有时您需要使用仅接受字符串作为连接字符串的其他工具连接到数据库。所以只需用 %40 更改 @ 符号

回答by Oduwole Oluwasegun

Also, if your password contains a percentage, %, 
 Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI

for example, if your password is John%Doe, the new transformed password will be John%25Doe or
If password is Paul%20Wait, New Password will be Paul%2520Wait

mongoClient.connect("mongodb://username:John%25Doe@host:port/dbname", function(err, db) {
// password is John%Doe
    }`enter code here`
);