0

I just set up a replica set for a standalone Mongo instance using the following changes to my config and rs.initiate().

# in master/slave replicated mongo databases, specify here whether
# this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
master = true
#source = slave.example.com

# in replica set configuration, specify the name of the replica set
replSet = rs0

When I check the status of the replica set from within Mongo it seems to be setup properly:

rs0:PRIMARY> rs.status()
{
    "set" : "rs0",
    "date" : ISODate("2014-11-14T08:18:06Z"),
    "myState" : 1,
    "members" : [
        {
            "_id" : 0,
            "name" : "Server:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1980,
            "optime" : Timestamp(1415952019, 4),
            "optimeDate" : ISODate("2014-11-14T08:00:19Z"),
            "self" : true
        }
    ],
    "ok" : 1
}

However when I try to run a test I get the following error:

PHP Fatal error:  Uncaught exception 'MongoConnectionException' with message 'MongoClient::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known' in /var/www/html/Site/m.php:10
Stack trace:
#0 /var/www/html/Site/m.php(10): MongoClient->__construct('mongodb://MONGO...', Array)
#1 {main}

Next exception 'MongoConnectionException' with message 'No candidate servers found' in /var/www/html/Site/m.php:10
Stack trace:
#0 /var/www/html/Site/m.php(0): MongoClient->__construct()
#1 {main}
  thrown in /var/www/html/Site/m.php on line 10

The connection code is a simple test:

$uri = "mongodb://MONGOHOST:27017/provider";

/* 
 * We recommend explictly configuring a connection timeout (see tips & tricks
 * below). Specify the replica set name to avoid connection errors.
 */ 
$options = array("connectTimeoutMS" => 30000, "replicaSet" => "rs0");
$client = new MongoClient($uri, $options);
echo print_r($client)."\n";

However, I can connect if I omit the $options array with the following:

The connection code is a simple test:

$uri = "mongodb://MONGOHOST:27017/provider";

/* 
 * We recommend explictly configuring a connection timeout (see tips & tricks
 * below). Specify the replica set name to avoid connection errors.
 */ 
$client = new MongoClient($uri);
echo print_r($client)."\n";

Can anyone point out why I can't connect directly to the replica set?

closed as off-topic by RolandoMySQLDBA, Michael Green, Philᵀᴹ, Max Vernon, Erik Apr 4 '16 at 1:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – RolandoMySQLDBA, Michael Green, Philᵀᴹ, Max Vernon, Erik
If this question can be reworded to fit the rules in the help center, please edit the question.

1

It turns out that the host wasn't properly added to /etc/hosts. Adding it seemed to solve the problem.

Not the answer you're looking for? Browse other questions tagged or ask your own question.