Install PHP driver 1.1.8:
pecl install -f mongodb-1.1.8
Download server.pem from:
https://github.com/mongodb/mongo-php-driver/tree/master/scripts/ssl
Create necessary database paths:
mkdir -p /tmp/cluster/a
mkdir -p /tmp/cluster/b
mkdir -p /tmp/cluster/c
Create a 3-node replica set with SSL:
mongod --replSet foo --sslMode requireSSL --sslPEMKeyFile /path/to/server.pem --port 3001 --dbpath /tmp/cluster/a
mongod --replSet foo --sslMode requireSSL --sslPEMKeyFile /path/to/server.pem --port 3002 --dbpath /tmp/cluster/b
mongod --replSet foo --sslMode requireSSL --sslPEMKeyFile /path/to/server.pem --port 3003 --dbpath /tmp/cluster/c
Connect to port 3001 and configure the replica set:
mongo --port 3001 --ssl --sslAllowInvalidCertificates
Configure the replica set:
rs.initiate({
_id: "foo",
members: [
{ _id: 1, host: "127.0.0.1:3001" },
{ _id: 2, host: "127.0.0.1:3002" },
{ _id: 3, host: "127.0.0.1:3003" }
]
});
Run this script. It should report the selected primary server and then dump the replica set topology:
Selected primary server: 127.0.0.1:3001
Dumping server topology:
- 127.0.0.1:3001 is primary
- 127.0.0.1:3002 is secondary
- 127.0.0.1:3003 is secondary
Use iptables to drop traffic to port 3003, which appears last in the URI:
sudo iptables -A INPUT -p tcp --destination-port 3003 -j DROP
Wait for a new primary to be elected. Run the script again the driver will fail to connect to all servers and report an empty topology:
No suitable servers found (`serverselectiontryonce` set):
[connection error calling ismaster on '127.0.0.1:3001']
[connection error calling ismaster on '127.0.0.1:3002']
[Failed connecting to '127.0.0.1:3003': Connection timed out]
Dumping server topology:
Move port 3003 first in the URI. Run the script again and the driver will connect to the remaining hosts and select the primary:
Selected primary server: 127.0.0.1:3001
Dumping server topology:
- 127.0.0.1:3001 is primary
- 127.0.0.1:3002 is secondary
Move port 3003 to the middle of the seed list and the primary to the start of the seed list. Run the script again and the driver will fail to select the primary; however, the primary will be listed in the topology as "unknown":
No suitable servers found (`serverselectiontryonce` set):
[connection error calling ismaster on '127.0.0.1:3001']
[Failed connecting to '127.0.0.1:3003': Connection timed out]
Dumping server topology:
- 127.0.0.1:3001 is unknown
- 127.0.0.1:3002 is secondary
Leave port 3003 to the middle of the seed list and swap the primary and secondary ports so that the primary appears last in the seed list. Run the script again and the driver will select the primary but list no other nodes in the topology:
Selected primary server: 127.0.0.1:3001
Dumping server topology:
- 127.0.0.1:3001 is primary
Clean up the iptables rule to restore traffic:
sudo iptables -D INPUT -p tcp --destination-port 3003 -j DROP