Doug Kress
Doug Kress

Reputation: 3537

PHP MongoDB WriteConcernException with replica set

I'm attempting to connect to a replica set from PHP, using MongoDB client (1.6.11), against both server versions 3.0.7 and 2.6.6.

However, when executing a 'save()', occasionally the save is executed against one of the "secondary" servers, and an exception is thrown:

PHP Warning: Uncaught exception 'MongoWriteConcernException' with message '127.0.0.3:27017: not master'

The commands used to replicate this are:

$options = [
'db' => 'admin',
'username' => '**********',
'password' => '**********',
'socketTimeoutMS' => 5000,
'connectTimeoutMS' => 100,
];

$mongo = new MongoClient('mongodb://127.0.0.1,127.0.0.2,127.0.0.3', $options);
$collection = $mongo->selectDB('test')->selectCollection('test');
for ($i=0; $i<10; $i++) {
    $collection->save(['a' => uniqid()], ['w' => 3]);
}

And the rs.status() returns:

{
"set" : "devSet",
"date" : ISODate("2016-05-26T22:02:16.564Z"),
"myState" : 1,
"members" : [
    {
        "_id" : 0,
        "name" : "127.0.0.1:27017",
        "health" : 1,
        "state" : 1,
        "stateStr" : "PRIMARY",
        "uptime" : 166106,
        "optime" : Timestamp(1464298125, 1),
        "optimeDate" : ISODate("2016-05-26T21:28:45Z"),
        "electionTime" : Timestamp(1464134229, 1),
        "electionDate" : ISODate("2016-05-24T23:57:09Z"),
        "configVersion" : 3,
        "self" : true
    },
    {
        "_id" : 1,
        "name" : "127.0.0.2:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 3616,
        "optime" : Timestamp(1464298125, 1),
        "optimeDate" : ISODate("2016-05-26T21:28:45Z"),
        "lastHeartbeat" : ISODate("2016-05-26T22:02:16.461Z"),
        "lastHeartbeatRecv" : ISODate("2016-05-26T22:02:15.853Z"),
        "pingMs" : 0,
        "syncingTo" : "127.0.0.3:27017",
        "configVersion" : 3
    },
    {
        "_id" : 2,
        "name" : "127.0.0.3:27017",
        "health" : 1,
        "state" : 2,
        "stateStr" : "SECONDARY",
        "uptime" : 166105,
        "optime" : Timestamp(1464298125, 1),
        "optimeDate" : ISODate("2016-05-26T21:28:45Z"),
        "lastHeartbeat" : ISODate("2016-05-26T22:02:14.959Z"),
        "lastHeartbeatRecv" : ISODate("2016-05-26T22:02:14.958Z"),
        "pingMs" : 0,
        "syncingTo" : "127.0.0.1:27017",
        "configVersion" : 3
    }
],
"ok" : 1
}

What am I missing?

Upvotes: 1

Views: 149

Answers (1)

Doug Kress
Doug Kress

Reputation: 3537

I found my mistake: Somewhere along the line, I dropped ["replicaSet" => "devSet"] from the options array.

Upvotes: 1

Related Questions