2016-08-04 183 views
2

我試圖通過連接到我的副本集的主節點find_onepymongo無法連接到主

MongoClient(hostname, replicaSet="rs0", read_preference=ReadPreference.PRIMARY) 

但它導致一個錯誤:

ServerSelectionTimeoutError: No replica set members match selector "Primary()"

我能夠成功地讀取使用SECONDARY_PREFERRED。我也嘗試連接使用MongoReplicaSetClient沒有成功。我猜這是由於配置不好,但我應該尋找什麼?

rs.status:

rs0:PRIMARY> rs.conf() 
{ 
     "_id" : "rs0", 
     "version" : 111313, 
     "protocolVersion" : NumberLong(1), 
     "members" : [ 
       { 
         "_id" : 1, 
         "host" : "ANDROMEDA:27017", 
         "arbiterOnly" : false, 
         "buildIndexes" : true, 
         "hidden" : false, 
         "priority" : 1, 
         "tags" : { 

         }, 
         "slaveDelay" : NumberLong(0), 
         "votes" : 1 
       }, 
       { 
         "_id" : 2, 
         "host" : "mongo02.db.com:27017", 
         "arbiterOnly" : false, 
         "buildIndexes" : true, 
         "hidden" : false, 
         "priority" : 0.5, 
         "tags" : { 

         }, 
         "slaveDelay" : NumberLong(0), 
         "votes" : 1 
       }, 
       { 
         "_id" : 3, 
         "host" : "mongo03.db.com:27017", 
         "arbiterOnly" : false, 
         "buildIndexes" : true, 
         "hidden" : false, 
         "priority" : 0.5, 
         "tags" : { 

         }, 
         "slaveDelay" : NumberLong(0), 
         "votes" : 1 
       } 
     ], 
     "settings" : { 
       "chainingAllowed" : true, 
       "heartbeatIntervalMillis" : 2000, 
       "heartbeatTimeoutSecs" : 10, 
       "electionTimeoutMillis" : 10000, 
       "getLastErrorModes" : { 

       }, 
       "getLastErrorDefaults" : { 
         "w" : 1, 
         "wtimeout" : 0 
       } 
     } 
} 
rs0:SECONDARY> rs.status() 
{ 
     "set" : "rs0", 
     "date" : ISODate("2016-08-04T08:58:02.293Z"), 
     "myState" : 2, 
     "term" : NumberLong(90), 
     "syncingTo" : "mongo03.db.com:27017", 
     "heartbeatIntervalMillis" : NumberLong(2000), 
     "members" : [ 
       { 
         "_id" : 1, 
         "name" : "ANDROMEDA:27017", 
         "health" : 1, 
         "state" : 1, 
         "stateStr" : "PRIMARY", 
         "uptime" : 2503, 
         "optime" : { 
           "ts" : Timestamp(1470299746, 1), 
           "t" : NumberLong(90) 
         }, 
         "optimeDate" : ISODate("2016-08-04T08:35:46Z"), 
         "lastHeartbeat" : ISODate("2016-08-04T08:58:01.109Z"), 
         "lastHeartbeatRecv" : ISODate("2016-08-04T08:58:01.803Z"), 
         "pingMs" : NumberLong(28), 
         "electionTime" : Timestamp(1469600522, 1), 
         "electionDate" : ISODate("2016-07-27T06:22:02Z"), 
         "configVersion" : 111313 
       }, 
       { 
         "_id" : 2, 
         "name" : "mongo02.db.com:27017", 
         "health" : 1, 
         "state" : 2, 
         "stateStr" : "SECONDARY", 
         "uptime" : 7604104, 
         "optime" : { 
           "ts" : Timestamp(1470299746, 1), 
           "t" : NumberLong(90) 
         }, 
         "optimeDate" : ISODate("2016-08-04T08:35:46Z"), 
         "syncingTo" : "mongo03.db.com:27017", 
         "configVersion" : 111313, 
         "self" : true 
       }, 
       { 
         "_id" : 3, 
         "name" : "mongo03.db.com:27017", 
         "health" : 1, 
         "state" : 2, 
         "stateStr" : "SECONDARY", 
         "uptime" : 2503, 
         "optime" : { 
           "ts" : Timestamp(1470299746, 1), 
           "t" : NumberLong(90) 
         }, 
         "optimeDate" : ISODate("2016-08-04T08:35:46Z"), 
         "lastHeartbeat" : ISODate("2016-08-04T08:58:01.948Z"), 
         "lastHeartbeatRecv" : ISODate("2016-08-04T08:58:01.802Z"), 
         "pingMs" : NumberLong(28), 
         "syncingTo" : "ANDROMEDA:27017", 
         "configVersion" : 111313 
       } 
     ], 
     "ok" : 1 
} 
+0

你能展示你的副本集配置嗎? – styvane

+0

@Styvane我更新了'rs.status'。希望能給你更多的信息。 –

+1

您是否嘗試過檢查ANDROMEDA:27017的主機名是否可以從您嘗試連接應用程序的主機解析?看起來這是一個很好的選擇,因爲你的輔助主機似乎都有FQDN。 – eoinbrazil

回答

2

在具有類似於ServerSelectionTimeoutError: No replica set members match selector "Primary()"的錯誤消息的情況下,並且其中副本集狀態函數輸出,rs.status(),示出了主成員使用非FQDN(在這種情況下爲ANDROMEDA:27017),那麼應用程序很可能無法解析網絡上的主要主機。

可以輕鬆地檢查這個運行的應用程序中使用來自主機的命令行下面的命令:

$ dig ANDROMEDA 
$ ping ANDROMEDA 
$ mongo --host ANDROMEDA:27017 

如果你沒有在主機上安裝了蒙戈shell中運行你的應用程序,你可以使用改爲Telnet。

$ telnet ANDROMEDA 27017 

這些輸出將允許您檢查您的應用程序主機和您的mongod主機之間的連接,以確定是否導致問題。

0

我使用此連接字符串:

MongoClient('mongodb://mongo01.db.com:27017,mongo02.db.com:27017,mongo03.db.com:27017/mydb',replicaSet="rs0", read_preference=ReadPreference.PRIMARY)