将分片添加到需要身份验证的 mongo 数据库的正确语法是什么

What is to correct syntax for adding a shard to an mongo db that requires authentication

我正在尝试使用 sh.addShard 命令将分片添加到 mongos,这里遵循此文档:https://docs.mongodb.com/v3.4/tutorial/add-shards-to-shard-cluster/ 给出的例子是:

sh.addShard( "rs1/mongodb0.example.net:27018" )

但是我尝试连接的数据库需要身份验证。

sh.addShard( "rs0/admin:XXXXXpasswordXXXXXXXX@XXX.XX.XX.XXX:27018" )

returns 此消息:

"More than one ':' detected. If this is an ipv6 address, it needs to be surrounded by '[' and ']'

运行 没有授权的命令 returns:

"errmsg" : "not authorized on admin to execute command { listDatabases: 1, nameOnly: true }"

如何在保持正确语法的同时向主机添加身份验证凭据。

据我所知,addShard() 不会获取任何身份验证凭据。

确保您在管理数据库下 use admin 并确保如果身份验证已打开,您将成功验证 db.auth("username", "password")

如果您的集群启用了 keyFile 身份验证,您需要将 keyFile 复制到每个主机并确保用户 运行 mongod 可以访问它 (https://docs.mongodb.com/manual/tutorial/enforce-keyfile-access-control-in-existing-replica-set/)