【问题标题】:azure cosmodb '=' in connection string fails in mongoose.connect(...)连接字符串中的 azure cosmos db '=' 在 mongoose.connect(...) 中失败
【发布时间】:2020-08-03 03:45:48
【问题描述】:
MongoError: database name must be a string

Nodejs 脚本:

// Azure tells me to use mongodb://mydb:LONG-STRING-ENDING-WITH==@mydb.documents.azure.com:10255/?ssl=true
// I change the = to %3D - I've confirmed this is the correct escaping
readonly serverConnection = 'mongodb://mydb:LONG-STRING-ENDING-WITH%3D%3D@mydb.documents.azure.com:10255/?ssl=true');

...
const mongoDB = await mongoose.connect(
    this.serverConnection,
    {
      useNewUrlParser: true,
      reconnectTries: Number.MAX_VALUE,
      reconnectInterval: 1000
    }
  );

如果我删除 ==%3D%3D,或更改为其他内容(例如 XX),则错误是预期的:

{ MongoError: Authentication Failed

所以不是字符串的长度,必须是==。

为什么会这样?可以做些什么来解决这个问题?

【问题讨论】:

  • 我遇到了同样的问题...我从 azure 门户复制了节点的显式连接字符串,但它不起作用。我希望有人能解释为什么会这样!

标签: mongoose azure-cosmosdb


【解决方案1】:

我找到了部分答案。删除:

useNewUrlParser: true

并忽略警告。目前。它告诉你这个解决方案在未来的某个时候不会起作用。

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.

如果有人能详细说明如何在连接字符串中使用新解析器和==,我将不胜感激。而且很高兴知道为什么== 或转义版本是一个问题。

【讨论】:

    【解决方案2】:

    我找到了一个很好的答案,您不必放弃新的 url 解析器。您必须在连接字符串的末尾添加数据库名称,如下所示:

    mongodb://<connectionstring/url>:port/<DATABASENAME>?ssl=True
    

    在我看来,这是一个干净的解决方案。希望能帮到你

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多