[英]Pymongo won't connect to Mongo replica set
I can't seem to connect pymongo to a replica set I've created.我似乎无法将 pymongo 连接到我创建的副本集。 The message I'm getting doesn't seem to make much sense as it's not what I'm seeing.
我收到的消息似乎没有多大意义,因为它不是我所看到的。
I'll first explain my configuration.我先说明一下我的配置。
Basically I have three machines 10.141.0.156, 10.141.0.158, 10.141.0.159基本上我有三台机器 10.141.0.156, 10.141.0.158, 10.141.0.159
I have 3 mongod instances, each running on a different machine, each running with:我有 3 个 mongod 实例,每个实例都在不同的机器上运行,每个实例都运行:
mongod --configsvr --dbpath /mnt/sdb/ --port 27019
mongod --configsvr --dbpath /mnt/sdb/ --port 27019
Next I've added the 3 mongod machines to a replica set:接下来,我将 3 台 mongod 机器添加到副本集:
{
"set" : "replSet",
"date" : ISODate("2016-04-01T12:23:12.552Z"),
"myState" : 2,
"term" : NumberLong(2),
"syncingTo" : "10.141.0.158:27017",
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "10.141.0.156:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2751,
"optime" : {
"ts" : Timestamp(1459513383, 6),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-04-01T12:23:03Z"),
"syncingTo" : "10.141.0.158:27017",
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "10.141.0.158:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2749,
"optime" : {
"ts" : Timestamp(1459513383, 6),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-04-01T12:23:03Z"),
"lastHeartbeat" : ISODate("2016-04-01T12:23:11.054Z"),
"lastHeartbeatRecv" : ISODate("2016-04-01T12:23:11.579Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1459510633, 1),
"electionDate" : ISODate("2016-04-01T11:37:13Z"),
"configVersion" : 2
},
{
"_id" : 2,
"name" : "10.141.0.159:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2749,
"optime" : {
"ts" : Timestamp(1459513383, 6),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2016-04-01T12:23:03Z"),
"lastHeartbeat" : ISODate("2016-04-01T12:23:11.261Z"),
"lastHeartbeatRecv" : ISODate("2016-04-01T12:23:12.240Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "10.141.0.158:27017",
"configVersion" : 2
}
],
"ok" : 1
}
Next I've started the mongos on the first machine 10.141.0.156:接下来我在第一台机器 10.141.0.156 上启动了 mongos:
mongos --configdb replSet/10.141.0.156:27017,10.141.0.158:27017,10.141.0.159:27017 --port 27077
mongos --configdb replSet/10.141.0.156:27017,10.141.0.158:27017,10.141.0.159:27017 --port 27077
At this point everything is running normally and I'm not getting any errors.此时一切正常,我没有收到任何错误。
Then I try to connect to the replica set with pymongo 3.2.1:然后我尝试使用 pymongo 3.2.1 连接到副本集:
MongoClient("10.141.0.156", 27077, replicaSet='replSet')
When I try either to read all the keys, or to write, I get:当我尝试读取所有键或写入时,我得到:
*** ServerSelectionTimeoutError: No replica set members available for replica set name "replSet"
*** ServerSelectionTimeoutError:没有可用于副本集名称“replSet”的副本集成员
Despite seeing the replica set in the mongo shell.尽管在 mongo shell 中看到了副本集。
For replicaset you'll need to start mongod
instances which will participate in replicaset;对于副本集,您需要启动将参与副本集的
mongod
实例; with --replSet
flag.带有
--replSet
标志。
mongod --dbpath /mnt/sdb/ --port 27019 --replSet "your_replicaset_name"
with --configsvr option, you are starting a config server for sharding, not replicaset.使用 --configsvr 选项,您正在启动用于分片的配置服务器,而不是副本集。 for more information, see https://docs.mongodb.org/manual/reference/program/mongod/#bin.mongod
有关更多信息,请参阅https://docs.mongodb.org/manual/reference/program/mongod/#bin.mongod
See this tutorial about how to setup replicaset: https://docs.mongodb.org/manual/tutorial/deploy-replica-set/请参阅有关如何设置副本集的本教程: https : //docs.mongodb.org/manual/tutorial/deploy-replica-set/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.