无法连接到Azure中的MongoDB [英] Cannot connect to MongoDB in Azure
问题描述
我在Azure上有一个MongoDB,我正在尝试使用npm模块连接到它 mongodb :
I have a MongoDB on Azure and I am trying to connect to it using the npm module mongodb:
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://myuser:mypassword@myhost.documents.azure.com:10355/?ssl=true", function (err, db) {
db.close();
});
密码
我的密码具有以下内容特征:
Password
My password has the following characteristics:
- 包含字母,小写,大写
- 无空格
- 包含数字
- 包含特殊字符,例如
=
,@
,$
依此类推
- Contains letters, lowercase, uppercase
- No white space
- Contains numbers
- Contains special characters like
=
,@
,$
and so on
执行上述代码时,我得到以下信息:
I get the following when executing the code above:
Error: Password contains an illegal unescaped character
at parseConnectionString (C:\Users\myuser\Documents\myproj\node_modules\mongodb\lib\url_parser.js:280:13)
但是文档并没有说明如何解决这个问题。我想这是一个编码问题。如何解决这个问题?
However the documentation does not tell much about how to solve this issue. I guess it is an encoding problem. How to fix this?
推荐答案
像@这样的字符被限制,因为它们搞乱了URL的结构。
之所以这样,是因为MongoDB将其解释为@ separator。而不是:
Characters like @ are restricted as they mess up the structure of the URL. The reason for this is because MongoDB interprets it as the @ separator. Instead of this:
var mongoClient = require("mongodb").MongoClient;
mongoClient.connect("mongodb://myuser:myp@ssword@myhost.documents.azure.com:10355/?ssl=true", function (err, db) {
db.close();
});
使用此
mongoClient.connect("mongodb://myuser:myp%40ssword@myhost.documents.azure.com:10355/?ssl=true", {
uri_decode_auth: true
}, function (err, db) {
db.close();
});
要对密码进行编码,请使用 encodeURIComponent(密码)
To encode the password, use encodeURIComponent(password)
您也可以使用此语法。
mongoClient.connect("mongodb://myhost.documents.azure.com:10355/?ssl=true",
{user: 'username', pass: 'p@ssword'}, function (err, db) {
db.close();
});
在更高版本中,使用
auth: {
user: 'username',
password: 'p@ssword',
}
如下所示
mongoClient.connect("mongodb://myhost.documents.azure.com:10355/?ssl=true", {
auth: {
user: 'username',
password: 'p@ssword',
}}, function (err, db) {
db.close();
});
这篇关于无法连接到Azure中的MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!