【问题标题】:MongoDB: Can't connect to new replica set masterMongoDB:无法连接到新的副本集主
【发布时间】:2019-12-07 18:22:08
【问题描述】:

尝试为我的 Node.JS 应用程序设置 MongoDB。我运行这个命令:

mongo "mongodb+srv://cluster0-gjc2u.mongodb.net/test"  --username <myusername>

并且每次都得到这个响应。

MongoDB shell version v4.2.1
Enter password:
connecting to: mongodb://cluster0-shard-00-00-gjc2u.mongodb.net:27017,cluster0-shard-00-01-gjc2u.mongodb.net:27017,cluster0-shard-00-02-gjc2u.mongodb.net:27017/test?authSource=admin&compressors=disabled&gssapiServiceName=mongodb&replicaSet=Cluster0-shard-0&ssl=true
2019-12-07T12:14:39.630-0600 I  NETWORK  [js] Starting new replica set monitor for Cluster0-shard-0/cluster0-shard-00-00-gjc2u.mongodb.net:27017,cluster0-shard-00-01-gjc2u.mongodb.net:27017,cluster0-shard-00-02-gjc2u.mongodb.net:27017
2019-12-07T12:14:39.630-0600 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cluster0-shard-00-00-gjc2u.mongodb.net:27017
2019-12-07T12:14:39.631-0600 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cluster0-shard-00-01-gjc2u.mongodb.net:27017
2019-12-07T12:14:39.631-0600 I  CONNPOOL [ReplicaSetMonitor-TaskExecutor] Connecting to cluster0-shard-00-02-gjc2u.mongodb.net:27017
2019-12-07T12:14:40.259-0600 I  NETWORK  [ReplicaSetMonitor-TaskExecutor] Confirmed replica set for Cluster0-shard-0 is Cluster0-shard-0/cluster0-shard-00-00-gjc2u.mongodb.net:27017,cluster0-shard-00-01-gjc2u.mongodb.net:27017,cluster0-shard-00-02-gjc2u.mongodb.net:27017
2019-12-07T12:14:40.799-0600 I  NETWORK  [js] Marking host cluster0-shard-00-00-gjc2u.mongodb.net:27017 as failed :: caused by :: Location40659: can't connect to new replica set master [cluster0-shard-00-00-gjc2u.mongodb.net:27017], err: AuthenticationFailed: bad auth Authentication failed.


*** It looks like this is a MongoDB Atlas cluster. Please ensure that your IP whitelist allows connections from your network.


2019-12-07T12:14:40.800-0600 E  QUERY    [js] Error: can't connect to new replica set master [cluster0-shard-00-00-gjc2u.mongodb.net:27017], err: AuthenticationFailed: bad auth Authentication failed. :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2019-12-07T12:14:40.803-0600 F  -        [main] exception: connect failed
2019-12-07T12:14:40.804-0600 E  -        [main] exiting with code 1

我已将我的 IP 地址列入白名单,并确保一切正常。什么可能导致此问题,我该如何解决?为什么会出现这个问题?

【问题讨论】:

    标签: javascript node.js mongodb replicaset


    【解决方案1】:

    我尝试的是:

    1. 创建新用户
    2. 确保连接应用程序时 mongoURI 的用户名和密码与连接 mongo shell 时的用户名和密码相同。
    3. 在命令行中运行连接字符串在应用程序中

    【讨论】:

      【解决方案2】:

      我意识到“稍等片刻”并不是一个非常有建设性的答案,但我遇到了同样的问题并且没有找到解决方案的运气,所以将它放置了几个小时,然后回来发现它运行良好。

      说明您的更改已部署的文字具有误导性。不知道为什么需要几个小时才能启动,但作为参考,我在使用 M0 Sandbox 集群层时发现了这一点。

      【讨论】:

        【解决方案3】:

        所以你必须注意两件事。

        1) 首先,在您的 $PATH 中添加 mongodb,如果您还没有主文件夹,请在主文件夹下创建一个 .bash_profile,然后插入以下内容。(对于 Ubuntu)

        导出 PATH="$PATH:/usr/bin/mongo" 确保将 mongo 的位置放在计算机上。如果您不知道位置,请在终端上输入whereis mongo。 保存后在终端输入source ~/.bashrc

        2) 最后,从 mongodb atlas 复制连接链接,并在被要求输入 usernamepassword 时复制链接

        请提供用于访问数据库而不是 mongdb atlas 帐户的凭据集。

        【讨论】:

          【解决方案4】:

          此解决方案可能特定于 Cloud MongoDB 提供的 mLabs 到 MongoDB Atlas 迁移工具。

          我的决定是:

          1. 使用相同的用户名重新创建用户
          2. 从特定的 mLabs 授权更改为 Atlas 内置角色
          3. 设置新凭据,我也避免使用特殊字符。

          祝你好运!

          【讨论】:

            【解决方案5】:

            创建一个没有任何特殊字符的简单密码,它只包含字母和数字。

            我无法访问,但更改密码对我有用。

            【讨论】:

              猜你喜欢
              • 2013-05-25
              • 1970-01-01
              • 1970-01-01
              • 2020-12-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-03-27
              相关资源
              最近更新 更多