Proper connection string to use a replica set (PHP)

1,574 views
Skip to first unread message

MK

unread,
Jul 13, 2011, 10:49:03 PM7/13/11
to mongodb-user
I have box1 (primary), box2, box3, and an arbiter, boxbal.

box1 also is the web server.

My connection string is:
mongodb://box1:27017,box2:27017
with the slaveOkay=true option

If box2 goes down, a php error is thrown and the site won't load.

What's the proper way to connect to the replica set to avoid this type
of failure?

k

unread,
Jul 14, 2011, 10:42:18 AM7/14/11
to mongodb-user
If a machine goes down, you will probably get an exception thrown in
the PHP driver, which you should catch. Then you can retry and it
should work. What version are you using?

Kristina Chodorow

unread,
Jun 20, 2012, 5:08:16 PM6/20/12
to mongodb-user
No.


On Jun 20, 4:50 pm, Adam Monsen <hair...@gmail.com> wrote:
> On Wednesday, July 13, 2011 7:49:03 PM UTC-7, MK wrote:
>
> > I have box1 (primary), box2, box3, and an arbiter, boxbal.
>
> > box1 also is the web server.
>
> > My connection string is:
> > mongodb://box1:27017,box2:27017
>
> (probably offtopic): Does the arbiter (boxbal) need to be in that
> connection string?

Adam Monsen

unread,
Jun 20, 2012, 5:36:21 PM6/20/12
to mongod...@googlegroups.com
Cool, thank you. Should it be?

I also found https://groups.google.com/d/topic/mongodb-user/WwhOnk0aKE4/discussion , but I couldn't find clear direction or a best practice with regard to whether or not an arbiter should be in the list of hosts to connect to. http://www.mongodb.org/display/DOCS/Replica+Set+Internals#ReplicaSetInternals-NodeTypes seems to imply that it shouldn't be in the list.

Kristina Chodorow wrote:
No.

Adam Monsen wrote:

Kristina Chodorow

unread,
Jun 20, 2012, 6:58:21 PM6/20/12
to mongodb-user
No, it shouldn't. It won't cause any harm if you're not using
authentication. If you are using auth, arbiters don't hold any data
(including user info) so the driver won't be able to log in, wasting
time on connection.


On Jun 20, 5:36 pm, Adam Monsen <hair...@gmail.com> wrote:
> Cool, thank you. *Should* it be?
>
> I also
> foundhttps://groups.google.com/d/topic/mongodb-user/WwhOnk0aKE4/discussion
> , but I couldn't find clear direction or a best practice with regard to
> whether or not an arbiter should be in the list of hosts to connect to.http://www.mongodb.org/display/DOCS/Replica+Set+Internals#ReplicaSetI...seems

Kristina Chodorow

unread,
Jun 21, 2012, 10:06:39 AM6/21/12
to mongodb-user
Actually, it looks like this hasn't been fixed yet:
https://jira.mongodb.org/browse/PHP-382.


On Jun 20, 7:29 pm, Chadwick Sorrell <mirot...@gmail.com> wrote:
> Did you happen to have an example of PHP code that you got working?  I have
> this exact situation.

Adam Monsen

unread,
Jun 21, 2012, 5:49:38 PM6/21/12
to mongod...@googlegroups.com
On 06/20/2012 03:58 PM, Kristina Chodorow wrote:
> No, it shouldn't.

Cool, thanks!

> It won't cause any harm if you're not using authentication.

I don't have a solid repro, but while testing failover today I found
that including the arbiter in my connection string did cause harm to my
application (and I do not use MongoDB authentication). See
http://stackoverflow.com/questions/11125389/doctrine-odm-mongodb-not-retrying-queries
... after removing the arbiter from the connection string I did not see
"MongoCursorException: not master and slaveok=false" all day long in my
application log.

signature.asc

Kristina Chodorow

unread,
Jun 22, 2012, 3:08:41 PM6/22/12
to mongodb-user
"I can not even get a connection to a SINGLE mongo db" caught my eye.
What happens if you run:

$m = new Mongo("mongodb://cache1-zone");
print_r($m->admin->command(array("ismaster" => 1)));



On Jun 22, 2:34 pm, AllanD <a_d...@bellsouth.net> wrote:
> Kristina,
>
> Hello, I am not sure if I need to start a new discusion or just join in
> right here.  I have read what seems like hundreds of blog post, mongo
> reports, google reports, mongo user manuals, etc... but still not solution
> to what seems to be a simple problem.
>
> My issue is with trying to connec to a replica set on
> mongodb-sunos5-x86_64-2.0.6 using php pecl driver 1.2.10 (downloaded
> yesterday).
>
> For my environment I have 3 seperate servers, a primary, secondary, and
> arbitrator.  They seem to be configured properly and everything works fine
> from the mongo prompt.  Updates to primary are replicated to secondary, and
> when one goes down the other is promoted by the arbitrator, it looks good,
> HOWEVER--- I simply can not connect from any of my web servers to the mongo
> servers.  The option of using localhost is no substitution for actual real
> servers.
>
> There are no network problem, I can ping by hostname to and from every
> server.
>
> When I submit my webpage, the console on the mongo server says it got
> connection, but the php returns "error message: couldn't determine master".
>
> I am pleading with you to take a serious look here and see why I can not
> get a connection that can determine the mongo master.  My configurations
> are as follows.
>
> THE MONGO SERVERS THEMSELVES.
>
> *MONGO SERVER 1: *
>
> db.adminCommand("getCmdLineOpts")
> PRIMARY> db.adminCommand("getCmdLineOpts")
> {
>         "argv" : [
>                 "mongod",
>                 "--replSet",
>                 "koalacache",
>                 "--port",
>                 "27017",
>                 "--dbpath",
>                 "/cache/cache1",
>                 "--directoryperdb",
>                 "--smallfiles"
>         ],
>         "parsed" : {
>                 "dbpath" : "/cache/cache1",
>                 "directoryperdb" : true,
>                 "port" : 27017,
>                 "replSet" : "koalacache",
>                 "smallfiles" : true
>         },
>         "ok" : 1}
>
> *MONGO SERVER 2: *
> SECONDARY> db.adminCommand("getCmdLineOpts")
> {
>         "argv" : [
>                 "mongod",
>                 "--replSet",
>                 "koalacache",
>                 "--port",
>                 "27017",
>                 "--dbpath",
>                 "/cache/cache2",
>                 "--directoryperdb",
>                 "--smallfiles"
>         ],
>         "parsed" : {
>                 "dbpath" : "/cache/cache2",
>                 "directoryperdb" : true,
>                 "port" : 27017,
>                 "replSet" : "koalacache",
>                 "smallfiles" : true
>         },
>         "ok" : 1}
>
> *MONGO SERVER 3 (ARBITER): *
> ARBITER>
> db.adminCommand("getCmdLineOpts")
> {
>         "argv" : [
>                 "mongod",
>                 "--replSet",
>                 "koalacache",
>                 "--port",
>                 "27017",
>                 "--dbpath",
>                 "/cache/arb",
>                 "--directoryperdb",
>                 "--smallfiles",
>                 "--oplogSize",
>                 "1"
>         ],
>         "parsed" : {
>                 "dbpath" : "/cache/arb",
>                 "directoryperdb" : true,
>                 "oplogSize" : 1,
>                 "port" : 27017,
>                 "replSet" : "koalacache",
>                 "smallfiles" : true
>         },
>         "ok" : 1}
>
> *MONGO SERVER STATUS*
> PRIMARY> rs.status()
> {
>         "set" : "koalacache",
>         "date" : ISODate("2012-06-22T18:41:49Z"),
>         "myState" : 1,
>         "members" : [
>                 {
>                         "_id" : 0,
>                         "name" : "cache1-zone:27017",
>                         "health" : 1,
>                         "state" : 1,
>                         "stateStr" : "PRIMARY",
>                         "optime" : {
>                                 "t" : 1340321540000,
>                                 "i" : 1
>                         },
>                         "optimeDate" : ISODate("2012-06-21T23:32:20Z"),
>                         "self" : true
>                 },
>                 {
>                         "_id" : 1,
>                         "name" : "cache2-zone:27017",
>                         "health" : 1,
>                         "state" : 2,
>                         "stateStr" : "SECONDARY",
>                         "uptime" : 21153,
>                         "optime" : {
>                                 "t" : 1340321540000,
>                                 "i" : 1
>                         },
>                         "optimeDate" : ISODate("2012-06-21T23:32:20Z"),
>                         "lastHeartbeat" : ISODate("2012-06-22T18:41:49Z"),
>                         "pingMs" : 0
>                 },
>                 {
>                         "_id" : 2,
>                         "name" : "cache-arb-zone:27017",
>                         "health" : 1,
>                         "state" : 7,
>                         "stateStr" : "ARBITER",
>                         "uptime" : 100,
>                         "optime" : {
>                                 "t" : 0,
>                                 "i" : 0
>                         },
>                         "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
>                         "lastHeartbeat" : ISODate("2012-06-22T18:41:49Z"),
>                         "pingMs" : 0
>                 }
>         ],
>         "ok" : 1}
>
> *THE MONGO DRIVER STATUS FROM PHPINFO()*
> php version 5.4.2
> apache 2.4.2 (unix)
> mongoMongoDB SupportenabledVersion1.2.10
> DirectiveLocal ValueMaster Valuemongo.allow_empty_keys00
> mongo.allow_persistent11mongo.auto_reconnect11mongo.chunk_size262144262144
> mongo.cmd$$mongo.default_hostlocalhostlocalhostmongo.default_port2701727017
> mongo.is_master_interval6060mongo.long_as_object00mongo.native_long00
> mongo.no_id00mongo.ping_interval55mongo.utf811
>
> **
> *THE PHP CODE I USING TO CONNECT (WHICH FAILS).*
>
> <?php
>
> try {
> $m = new Mongo("mongodb://cache1-zone,cache2-zone", array("replicaSet" =>
> true, "persist" => 'sess'));
>
> $m->connect;
> echo("we got connect = ");
> var_dump($m->connected);
>
> echo("we got status = ");
> var_dump($m->status);
>
> echo("we got server = ");
> var_dump($m->server);
>
> print_r($m->admin->command( array( "ismaster" => 1 ) ));
> $db = new MongoDB($m, "allantestdb");
> $c = $db->selectCollection("example");
> $c->setSlaveOkay(true);
>
> var_dump($c->findOne());
>
> }
>
> catch (MongoCursorException $e) {
>     echo "error message: ".$e->getMessage()."\n";
>     echo "error code: ".$e->getCode()."\n";
>
> }
>
> *THE OUTPUT SHOWING ERROR DETERMINING MASTER.*
> we got connect = bool(true)
> we got status = NULL
> we got server = object(MongoDB)#2 (2) { ["w"]=> int(1) ["wtimeout"]=>
> int(10000) }
> error message: couldn't determine master error code: 14
> ?>
>
> Please help me get a connection string to a replication set.  I can not
> even get a connection to a SINGLE mongo db.
>
> It is frustrating to see the potential of Mongo, have setting up all these
> servers, and not even be able to connect to what is suposed to be a high
> availability database solution.
>
> Sorry, your help is appreciated.
>
> Thank You,
> Allan

AllanD

unread,
Jun 22, 2012, 3:32:04 PM6/22/12
to mongod...@googlegroups.com
Thanks for the quick reply,
I commented out all my other code and ran your within the try catch and get the following.
 
Fatal error: Uncaught exception 'MongoConnectionException' with message 'Error 0' in /usr/local/apache2/htdocs/www/xxx/test-session.php:21 Stack trace: #0 /usr/local/apache2/htdocs/www/xxx/test-session.php(21): Mongo->__construct('mongodb://cache...') #1 {main} thrown in/usr/local/apache2/htdocs/www/xxx/test-session.php on line 21
 
I am here and can throw more code test at it if you need me too.
Allan

AllanD

unread,
Jun 22, 2012, 3:36:13 PM6/22/12
to mongod...@googlegroups.com
Sorry Kristina,
I forgot to mention there is no admin db in mongo,
not sure if its supposed to be there or not.
only local and some other little test ones.
 
see my results from your code below.
Allan

Kristina Chodorow

unread,
Jun 22, 2012, 3:52:06 PM6/22/12
to mongodb-user
Thanks, unfortunately, looks like your app's error handlers are
catching the error so I can't tell what it is from that. Can you run:

try {
$m = new Mongo("mongodb://cache1-zone");
}
catch (MongoException $e) {
echo $e->getMessage()."\n";
var_dump($e);
}


On Jun 22, 3:36 pm, AllanD <a_d...@bellsouth.net> wrote:
> Sorry Kristina,
> I forgot to mention there is no admin db in mongo,
> not sure if its supposed to be there or not.
> only local and some other little test ones.
>
> see my results from your code below.
> Allan
>
>
>
>
>
>
>
> On Friday, June 22, 2012 2:32:04 PM UTC-5, AllanD wrote:
> > Thanks for the quick reply,
> > I commented out all my other code and ran your within the try catch and
> > get the following.
>
> > *Fatal error*: Uncaught exception 'MongoConnectionException' with message
> > 'Error 0' in /usr/local/apache2/htdocs/www/xxx/test-session.php:21 Stack
> > trace: #0 /usr/local/apache2/htdocs/www/xxx/test-session.php(21):
> > Mongo->__construct('mongodb://cache...') #1 {main} thrown in*
> > /usr/local/apache2/htdocs/www/xxx/test-session.php* on line *21*

AllanD

unread,
Jun 22, 2012, 4:01:06 PM6/22/12
to mongod...@googlegroups.com

Kristina,

I removed all my code and ran yours, getting the following result.
 
 
PHP  CODE
 
try {
    $m = new Mongo("mongodb://cache1-zone");
}
catch (MongoException $e) {
    echo $e->getMessage()."\n";
    var_dump($e);
}
GIVES THE FOLLOWING OUTPUT
 
 
Error 0 object(MongoConnectionException)#2 (7) { ["message":protected]=> string(7) "Error 0" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(56) "/usr/local/apache2/htdocs/www/xxx/test-session.php" ["line":protected]=> int(5) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(56) "/usr/local/apache2/htdocs/www/xxx/test-session.php" ["line"]=> int(5) ["function"]=> string(11) "__construct" ["class"]=> string(5) "Mongo" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(21) "mongodb://cache1-zone" } } } ["previous":"Exception":private]=> NULL }
 
Allan 

Kristina Chodorow

unread,
Jun 22, 2012, 4:16:16 PM6/22/12
to mongodb-user
Are you using Drupal or some other framework? Do you have an error
handler set (http://www.php.net/manual/en/function.set-error-
handler.php)? Error 0 isn't getting generated by the driver.


On Jun 22, 4:01 pm, AllanD <a_d...@bellsouth.net> wrote:
> > Kristina,
> > I removed all my code and ran yours, getting the following result.
>
> > *PHP  CODE*
>
> try {
>     $m = new Mongo("mongodb://cache1-zone");}
>
> catch (MongoException $e) {
>     echo $e->getMessage()."\n";
>     var_dump($e);}
>
> *GIVES THE FOLLOWING OUTPUT*

Kristina Chodorow

unread,
Jun 22, 2012, 4:17:06 PM6/22/12
to mongodb-user
Or an exception handler (http://www.php.net/manual/en/function.set-
exception-handler.php)?

AllanD

unread,
Jun 22, 2012, 4:22:28 PM6/22/12
to mongod...@googlegroups.com
No, there is no set_error_handler within the code.
 
The php.ini contains
 
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
 
 
 
 
 

Kristina Chodorow

unread,
Jun 22, 2012, 4:24:28 PM6/22/12
to mongodb-user
Also, can you:

1. ssh to you application server (where PHP is running).
2. Connect to cache1-zone via the mongo shell (run "mongo cache1-zone:
27017/test")

Make sure you're on the application server, not one of the mongod
server machines.

AllanD

unread,
Jun 22, 2012, 4:26:20 PM6/22/12
to mongod...@googlegroups.com
Forgot to mention this is 64 bit environment and pecl mongo driver loads as 64 bit to 64 bit apache.  Nothing special.

On Wednesday, July 13, 2011 9:49:03 PM UTC-5, MK wrote:

AllanD

unread,
Jun 22, 2012, 4:35:40 PM6/22/12
to mongod...@googlegroups.com
Kristina,
Yes, the ssh works.  I was able to ssh and connect to mongo primary cache1-zone.
 
connecting to: cache1-zone:27017/test
PRIMARY> show dbs
comedy  0.0625GB
local   1.06201171875GB
test    0.0625GB
Allan

AllanD

unread,
Jun 22, 2012, 9:07:46 PM6/22/12
to mongod...@googlegroups.com

Kristina,

As a side note I turned on logging to file on all 3 of my mongo nodes.  I re-ran my php code and found that the arbitrator was receiving the connections and accepting them, but still the php returned error as before.
 
One thing I did to prevent arbiter from accepting these connections was to set its priority to 0 and turn on hide as follows.
 

cfg = rs.conf()

cfg.members[2].priority = 0

cfg.members[2].hidden = true

rs.reconfig(cfg)

The result was that connections from php logged the following on our primary node.
 
Fri Jun 22 20:26:50 [initandlisten] connection accepted from 172.16.1.20:46834 #121
Fri Jun 22 20:26:50 [conn121] end connection 172.16.1.20:46834
 
The php code still returned the following:
 
Error 0 object(MongoConnectionException)#2 (7) { ["message":protected]=> string(7) "Error 0" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(56) "/usr/local/apache2/htdocs/www/koalazone/test-session.php" ["line":protected]=> int(5) ["trace":"Exception":private]=> array(1) { [0]=> array(6) { ["file"]=> string(56) "/usr/local/apache2/htdocs/www/koalazone/test-session.php" ["line"]=> int(5) ["function"]=> string(11) "__construct" ["class"]=> string(5) "Mongo" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(21) "mongodb://cache1-zone" } } } ["previous":"Exception":private]=> NULL }
 
This is a result of executing the following php code.
 
try {
$m = new Mongo("mongodb://cache1-zone");
}
catch (MongoException $e) {
    echo $e->getMessage()."\n";
    var_dump($e);
}
 
Note:
if I run the following a connection is received by both primary and secondary each time php is issued.
//$m = new Mongo("mongodb://cache1-zone", array("replicaSet" => "koalacache"));
 
If I run the following then connection pooling is in play and the
//$m = new Mongo("mongodb://cache1-zone", array("replicaSet" => "koalacache", "persist" => 'sess' ));
ALSO, running the 2 statements above returns absolutely NOTHING from php error.  But still I think I do not have a connection.  Please instruct (provide official test code) as to how to query or insert a record so we can progress beyond this point.
 
I appreciate your help.
Allan
 

Kristina Chodorow

unread,
Jun 25, 2012, 10:56:53 AM6/25/12
to mongodb-user
To test the connection, run the ismaster command (print_r($m->admin-
>command(array("ismaster" => 1)));). We should get one connection
working before you worry about a replica set connection. Also, the
"persist" option is only there for backwards compatibility, it doesn't
actually do anything.

The error you're getting is not generated by the PHP driver, so
something in your application must be intercepting it (maybe a default
exception handler?). As the driver seems to be connecting to MongoDB
(given the messages in your logs), it is difficult to know what is
going wrong without the error message.

AllanD

unread,
Jul 2, 2012, 12:28:24 PM7/2/12
to mongod...@googlegroups.com
Kristina,
I have still not been able to get a connection EVEN TO A SINGLE MONGO DB.
Using the latest php driver from pecl, and the latest mongo db binaries off your site.  I am even using YOUR code to connect.  I have made many posts here but still can not make a single connect.  it keeps reporting CAN NOT DETERMINE MASTER. 
 
I am going to have to abot my plans to use MongoDB without your focussed attention to making a simple connection work.
 
Allan

Derick Rethans

unread,
Jul 3, 2012, 6:00:39 AM7/3/12
to mongod...@googlegroups.com
On Mon, 2 Jul 2012, AllanD wrote:

> I have still not been able to get a connection EVEN TO A SINGLE MONGO DB.
> Using the latest php driver from pecl, and the latest mongo db binaries off
> your site. I am even using YOUR code to connect. I have made many posts
> here but still can not make a single connect. it keeps reporting CAN NOT
> DETERMINE MASTER.

I also have no idea where the "Error 0" comes from, but it's the first
time that you are mentioning "Can not determine master". Is this
something you when you run the ismaster command on the shell, or when
you run it in PHP? That bit was a bit unclear in your reply.

cheers,
Derick

--
http://mongodb.org | http://derickrethans.nl
twitter: @derickr and @mongodb
Reply all
Reply to author
Forward
0 new messages