Rumah > Soal Jawab > teks badan
问题描述
三台 Windows Server 2012 R2 安装 MongoDB 3.0.8 做复制集,批量upsert有时报 “waiting for replication timed out(64):stdClass::__set_state(array('wtimeout' => true,))” 错误。
相关代码
$mongoManager = new MongoDB\Driver\Manager( 'mongodb://test:test123@192.168.100.101:27017,192.168.100.102:27017,192.168.100.103:27017/test' );
$mongoWriteConcern = new MongoDB\Driver\WriteConcern( MongoDB\Driver\WriteConcern :: MAJORITY, 1000 );
$bulk = new MongoDB\Driver\BulkWrite();
foreach( $data as $i ) {
$i = (array)$i;
$bulk->update( array('unique' => $i['unique']), array('$set' => $i), array('upsert' => true, 'multi' => true) );
}
try {
$mongoResult = $mongoManager->executeBulkWrite( 'test.data', $bulk, $mongoWriteConcern );
} catch ( MongoDB\Driver\Exception\BulkWriteException $e ) {
$mongoResult = $e->getWriteResult();
// Check if the write concern could not be fulfilled
if ( $writeConcernError = $mongoResult->getWriteConcernError() ) {
printf( "%s(%d):%s\n",
$writeConcernError->getMessage(),
$writeConcernError->getCode(),
var_export( $writeConcernError->getInfo(), true )
);
}
// Check if any write operations did not complete at all
foreach ( $mongoResult->getWriteErrors() as $writeError ) {
printf( "Write #%d:%s(%d)\n",
$writeError->getIndex(),
$writeError->getMessage(),
$writeError->getCode()
);
}
} catch ( MongoDB\Driver\Exception\Exception $e ) {
printf( "Other errors:%s\n", $e->getMessage() );
}
报错信息
waiting for replication timed out(64):stdClass::__set_state(array(
'wtimeout' => true,
))
复制集信息
test:PRIMARY> rs.status()
{
"set" : "test",
"date" : ISODate("2016-01-19T01:01:09.279Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.100.101:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 57786,
"optime" : Timestamp(1453165025, 125),
"optimeDate" : ISODate("2016-01-19T00:57:05Z"),
"electionTime" : Timestamp(1453108221, 1),
"electionDate" : ISODate("2016-01-18T09:10:21Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.100.102:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 56945,
"optime" : Timestamp(1453165025, 125),
"optimeDate" : ISODate("2016-01-19T00:57:05Z"),
"lastHeartbeat" : ISODate("2016-01-19T01:01:07.726Z"),
"lastHeartbeatRecv" : ISODate("2016-01-19T01:01:08.781Z"),
"pingMs" : 1,
"syncingTo" : "192.168.100.101:27017",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "192.168.100.103:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 56018,
"optime" : Timestamp(1453165025, 125),
"optimeDate" : ISODate("2016-01-19T00:57:05Z"),
"lastHeartbeat" : ISODate("2016-01-19T01:01:07.847Z"),
"lastHeartbeatRecv" : ISODate("2016-01-19T01:01:07.581Z"),
"pingMs" : 1,
"syncingTo" : "192.168.100.101:27017",
"configVersion" : 2
}
],
"ok" : 1
}