MongoDB Replica Set Can't be connected To 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&gt; 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-&gt;__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-&gt;__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 &amp; tricks
 * below). Specify the replica set name to avoid connection errors.
 */ 
$options = array("connectTimeoutMS" =&gt; 30000, "replicaSet" =&gt; "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 &amp; 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?
It turns out that the host wasn't properly added to /etc/hosts. Adding it seemed to solve the problem.