Mongoose 无法连接到 mLab 环境
Posted
技术标签:
【中文标题】Mongoose 无法连接到 mLab 环境【英文标题】:Mongoose is unable to connect to mLab environment 【发布时间】:2019-01-09 13:45:20 【问题描述】:我有一个应用程序尝试使用 Mongoose 连接到 mLab 环境。我将以下代码配置为尝试连接到 mLab MongoDB 实例。
mongoose.connect(process.env.MONGODB_URI);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => );
变量process.env.MONGODB_URI
等于
mongodb://user:password@ds123456.mlab.com:12345/someRandomName
(为了隐私而伪装的标识符)。
适用的软件版本
节点:v10.7.0
NPM:6.2.0
猫鼬:5.2.6
当我尝试运行应用程序时,出现以下错误。
节点./bin/www
(node:2555) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option useNewUrlParser: true to MongoClient.connect.
connection error: Error: Slash in host identifier
at parseConnectionString (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:219:15)
at parseHandler (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:129:14)
at module.exports (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/url_parser.js:25:12)
at deprecated (internal/util.js:70:15)
at connect (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:179:3)
at connectOp (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/operations/mongo_client_ops.js:283:3)
at executeOperation (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/utils.js:420:24)
at MongoClient.connect (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/node_modules/mongodb/lib/mongo_client.js:168:10)
at Promise (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/lib/connection.js:493:12)
at new Promise (<anonymous>)
at NativeConnection.Connection.openUri (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/lib/connection.js:490:19)
at Mongoose.connect (/Users/nathanielsuchy/Documents/control-panel/node_modules/mongoose/lib/index.js:230:15)
at Object.<anonymous> (/Users/nathanielsuchy/Documents/control-panel/app.js:20:10)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
我得出两个结论。一是我需要使用新的 URL 解析器,二是在我的数据库连接字符串格式中没有看到一个问题。我应该如何着手解决这个问题?
【问题讨论】:
这里的主要问题是Error: Slash in host identifier
。另一件事只是一个警告。
@Akrion 问题是主机名中没有斜杠。我无法确定问题。我的连接字符串与docs.mongodb.com/manual/reference/connection-string 中指定的格式匹配
您的密码是否包含“@”或其他特殊字符?
猫鼬 5.2.6 mongoose.connect(Stringconnection, useNewUrlParser: true )
@Akrion 不是 @ 符号,而是各种其他特殊字符。
【参考方案1】:
正确的格式是mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
您的密码是否包含“@”或其他特殊字符?如果是这样:
const dbUrl = `mongodb://adminUsername:$encodeURIComponent('adminPassword')@localhost:27017/mydb`;
还要检查这个:MongoDB password with "@" in it
【讨论】:
这有效,但在使用环境变量时无效。以上是关于Mongoose 无法连接到 mLab 环境的主要内容,如果未能解决你的问题,请参考以下文章
无法从 AWS EC2 实例连接到 Mlab.com 数据库