使用 Golang 和 Robo3T 连接到 MongoDB ReplicaSet 时出现问题
Problem connecting to MongoDB ReplicaSet using Golang and Robo3T
我正在努力连接到我的 MongoDB 副本集,但似乎没有任何效果。
我已经部署了具有 3 个节点的副本集,每个节点都在一个单独的 AWS 可用区但在同一区域中。
下面是我用来连接副本集的代码。
mongoURL := "mongodb://myuser:password@<publicIP>:27017,<publicIP>:27017,<publicIP>:27017/?replicaSet=replica01&authSource=admin"
clientOptions := options.Client().ApplyURI(mongoURL)
clientOptions = clientOptions.SetMaxPoolSize(100) //100 is default driver setting
log.Println("Connection String: " + clientOptions.GetURI())
client, err := mongo.Connect(ctx, clientOptions)
我还创建了 DNS 映射并使用了它,但它也不起作用。
下面是连接URL
mongodb://adminuser:password@rs1.domain.com:27017,rs2.domain.com:27017,rs3.domain.com:27017/?replicaSet=replica01&authSource=admin
Robo3T
当我尝试使用 public IP 连接副本集与 Robo3T 时,出现以下错误
Cannot connect to replica set "IP-RS"[public-ip:27017]. Set's
primary is unreachable.
Reason: No member of the set is reachable. Reason: Connect failed
使用子域名连接副本集时出现如下错误
Cannot connect to replica set "SRV Replica"[rs1.domain.com:27017].
Set's primary is unreachable.
Reason: Failed to initialize MongoWorker. Reason: connect failed
这是解决方案。
问题是主机名。我在配置副本集时使用了主机名。
这是我的配置
$ config = {
_id : "mongo-cluster",
members : [
{_id : 0, host : "mongo-1:27017"},
{_id : 1, host : "mongo-2:27017"},
{_id : 2, host : "mongo-3:27017"},
]
}
$ rs.initiate(config)
解决方案
我将 hostname
更改为 private-ip
。
请注意,由于我的 3 个副本节点位于同一 AWS 区域但位于不同的可用区,因此我使用 private ip
在节点之间建立连接。
$ config = {
_id : "replica01",
members : [
{_id : 0, host : "private-ip:27017"},
{_id : 1, host : "private-ip:27017"},
{_id : 2, host : "private-ip:27017"},
]
}
rs0:PRIMARY> rs.reconfig(config)
MongoDB 所有 3 个节点上的 conf 设置
File: /etc/mongod.conf
net:
port: 27017
bindIp: 127.0.0.1, 172.16.1.x (private-ip)
security:
keyFile: /opt/mongo/mongo-keyfile
replication:
replSetName: replica01
所有 3 个节点上的 Hostentry
File: /etc/hosts
172.16.1.x mongo-1
172.16.1.x mongo-2
172.16.1.x mongo-3
您的每台机器的名称应与 /etc/hosts
文件中定义的名称相匹配
$ sudo nano /etc/hostname
mongo-1
副本状态
> rs.status()
{
"set" : "replica01",
"date" : ISODate("2021-03-13T12:42:45.971Z"),
"myState" : 2,
"term" : NumberLong(8),
"syncSourceHost" : "172.16.1.x:27017",
"syncSourceId" : 2,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"lastCommittedWallTime" : ISODate("2021-03-13T12:42:41.356Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"readConcernMajorityWallTime" : ISODate("2021-03-13T12:42:41.356Z"),
"appliedOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"durableOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"lastAppliedWallTime" : ISODate("2021-03-13T12:42:41.356Z"),
"lastDurableWallTime" : ISODate("2021-03-13T12:42:41.356Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1615639331, 1),
"members" : [
{
"_id" : 1,
"name" : "172.16.1.x:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 159132,
"optime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2021-03-13T12:42:41Z"),
"syncSourceHost" : "172.16.1.x:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 2,
"configTerm" : 8,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "172.16.1.x:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 62213,
"optime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2021-03-13T12:42:41Z"),
"optimeDurableDate" : ISODate("2021-03-13T12:42:41Z"),
"lastHeartbeat" : ISODate("2021-03-13T12:42:45.616Z"),
"lastHeartbeatRecv" : ISODate("2021-03-13T12:42:44.778Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1615477956, 1),
"electionDate" : ISODate("2021-03-11T15:52:36Z"),
"configVersion" : 2,
"configTerm" : 8
},
{
"_id" : 3,
"name" : "172.16.1.x:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 62213,
"optime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2021-03-13T12:42:41Z"),
"optimeDurableDate" : ISODate("2021-03-13T12:42:41Z"),
"lastHeartbeat" : ISODate("2021-03-13T12:42:45.095Z"),
"lastHeartbeatRecv" : ISODate("2021-03-13T12:42:45.321Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "172.16.1.x:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 2,
"configTerm" : 8
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1615639361, 1),
"signature" : {
"hash" : BinData(0,"2ujactkx3HytFTyWhh+JkzyQy+8="),
"keyId" : NumberLong("6936222258616598531")
}
},
"operationTime" : Timestamp(1615639361, 1)
}
转到连接字符串
mongoURL := "mongodb://username:pass@2.16.1.x:27017,172.16.1.x:27017,172.16.1.x:27017/mydb?replicaSet=replica01&authSource=admin"
我正在努力连接到我的 MongoDB 副本集,但似乎没有任何效果。
我已经部署了具有 3 个节点的副本集,每个节点都在一个单独的 AWS 可用区但在同一区域中。
下面是我用来连接副本集的代码。
mongoURL := "mongodb://myuser:password@<publicIP>:27017,<publicIP>:27017,<publicIP>:27017/?replicaSet=replica01&authSource=admin"
clientOptions := options.Client().ApplyURI(mongoURL)
clientOptions = clientOptions.SetMaxPoolSize(100) //100 is default driver setting
log.Println("Connection String: " + clientOptions.GetURI())
client, err := mongo.Connect(ctx, clientOptions)
我还创建了 DNS 映射并使用了它,但它也不起作用。
下面是连接URL
mongodb://adminuser:password@rs1.domain.com:27017,rs2.domain.com:27017,rs3.domain.com:27017/?replicaSet=replica01&authSource=admin
Robo3T
当我尝试使用 public IP 连接副本集与 Robo3T 时,出现以下错误
Cannot connect to replica set "IP-RS"[public-ip:27017]. Set's primary is unreachable.
Reason: No member of the set is reachable. Reason: Connect failed
使用子域名连接副本集时出现如下错误
Cannot connect to replica set "SRV Replica"[rs1.domain.com:27017]. Set's primary is unreachable.
Reason: Failed to initialize MongoWorker. Reason: connect failed
这是解决方案。
问题是主机名。我在配置副本集时使用了主机名。
这是我的配置
$ config = {
_id : "mongo-cluster",
members : [
{_id : 0, host : "mongo-1:27017"},
{_id : 1, host : "mongo-2:27017"},
{_id : 2, host : "mongo-3:27017"},
]
}
$ rs.initiate(config)
解决方案
我将 hostname
更改为 private-ip
。
请注意,由于我的 3 个副本节点位于同一 AWS 区域但位于不同的可用区,因此我使用 private ip
在节点之间建立连接。
$ config = {
_id : "replica01",
members : [
{_id : 0, host : "private-ip:27017"},
{_id : 1, host : "private-ip:27017"},
{_id : 2, host : "private-ip:27017"},
]
}
rs0:PRIMARY> rs.reconfig(config)
MongoDB 所有 3 个节点上的 conf 设置
File: /etc/mongod.conf
net:
port: 27017
bindIp: 127.0.0.1, 172.16.1.x (private-ip)
security:
keyFile: /opt/mongo/mongo-keyfile
replication:
replSetName: replica01
所有 3 个节点上的 Hostentry
File: /etc/hosts
172.16.1.x mongo-1
172.16.1.x mongo-2
172.16.1.x mongo-3
您的每台机器的名称应与 /etc/hosts
文件中定义的名称相匹配
$ sudo nano /etc/hostname
mongo-1
副本状态
> rs.status()
{
"set" : "replica01",
"date" : ISODate("2021-03-13T12:42:45.971Z"),
"myState" : 2,
"term" : NumberLong(8),
"syncSourceHost" : "172.16.1.x:27017",
"syncSourceId" : 2,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"lastCommittedWallTime" : ISODate("2021-03-13T12:42:41.356Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"readConcernMajorityWallTime" : ISODate("2021-03-13T12:42:41.356Z"),
"appliedOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"durableOpTime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"lastAppliedWallTime" : ISODate("2021-03-13T12:42:41.356Z"),
"lastDurableWallTime" : ISODate("2021-03-13T12:42:41.356Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1615639331, 1),
"members" : [
{
"_id" : 1,
"name" : "172.16.1.x:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 159132,
"optime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2021-03-13T12:42:41Z"),
"syncSourceHost" : "172.16.1.x:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 2,
"configTerm" : 8,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "172.16.1.x:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 62213,
"optime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2021-03-13T12:42:41Z"),
"optimeDurableDate" : ISODate("2021-03-13T12:42:41Z"),
"lastHeartbeat" : ISODate("2021-03-13T12:42:45.616Z"),
"lastHeartbeatRecv" : ISODate("2021-03-13T12:42:44.778Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1615477956, 1),
"electionDate" : ISODate("2021-03-11T15:52:36Z"),
"configVersion" : 2,
"configTerm" : 8
},
{
"_id" : 3,
"name" : "172.16.1.x:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 62213,
"optime" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDurable" : {
"ts" : Timestamp(1615639361, 1),
"t" : NumberLong(8)
},
"optimeDate" : ISODate("2021-03-13T12:42:41Z"),
"optimeDurableDate" : ISODate("2021-03-13T12:42:41Z"),
"lastHeartbeat" : ISODate("2021-03-13T12:42:45.095Z"),
"lastHeartbeatRecv" : ISODate("2021-03-13T12:42:45.321Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "172.16.1.x:27017",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 2,
"configTerm" : 8
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1615639361, 1),
"signature" : {
"hash" : BinData(0,"2ujactkx3HytFTyWhh+JkzyQy+8="),
"keyId" : NumberLong("6936222258616598531")
}
},
"operationTime" : Timestamp(1615639361, 1)
}
转到连接字符串
mongoURL := "mongodb://username:pass@2.16.1.x:27017,172.16.1.x:27017,172.16.1.x:27017/mydb?replicaSet=replica01&authSource=admin"