MongoDB:无法连接到新的副本集主

And*_*ung 5 javascript mongodb node.js replicaset

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

mongo "mongodb+srv://cluster0-gjc2u.mongodb.net/test"  --username <myusername>
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

Yon*_*wit 0

所以你必须注意两件事。

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

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

2)最后,从 mongodb 图集中复制连接链接,当要求username时password

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