For a matter of convenience, I’ll assume mongo is installed on Ubuntu 14.04 server.
Setting mongo replica set is quite easy. most of mongo’s settings are in the configuration file (/etc/mongodb.conf).
To setup replica, make sure that bind_ip IS NOT set to local host. unless you are creating replica on the same machine (which is a decent learning practice, but worth absolutely nothing in the real world). you need to add one more thing, which is the replSet instruction (note the exact syntax, it’s important). IF you are using 2.61, it’s already there. You can pick any legal name, e.g rs0.
This line should read then,
replSet = rs0.
This same name should be set in every server you wish to connect. There are many types of servers that can be connected to a replica set.
Make sure all your servers are up and running.
Login to mongo and run rs.status. you will see an error stating that the replica set is not yet initialized. run rs.initiate() to create a new empty one.
Next, you’ll need to setup server name. The server that you are setting up, is most likely to become primary, unless you’ll set a priority. Server name is usually the host name, and many times (using vpn, host name not set correctly) it’s not correct. For a test setup I do recommend virtual box host only, and setting up a static ip. so lets say we have 3 servers, 192.168.56.101 to 103. If we are setting the first one, we need to set it’s name property. Then we’ll add the other servers, which will adjust their name based on our settings, so their host name is less of an issue. To set this up, we’ll get into the first mongo instance, with mongo, and configure the replica set like so:
What We are doing here is getting an object, modifying it and then re-setting it to the replica set. Now, we need to add the other members, like so:
Note that in 2.6+ you might get an error even starting the replica set when the host information is not correct. To fix it, set a server FQDN:
/etc/hostname, to e.g. yourhost.dyndns.org
hostname -F /etc/hostname
- IP addresses are acceptable
- you might also want to edit /etc/hosts
This will take few second or many minutes, all depends on the amount of data and network connectivity you have. at the any time you can issue an
rs.status() command to view the current status. At the end you should see N-1 set as SECONDARY and one set as PRIMARY, usually, it will be the one that you’ve created the replica from.
when managing a cluster, you should setup at least one user for it. to be able to set all the aspects of a database, you need to set the following:
In order to secure the sessions between the replica set members, we need to set auth=true (this parameter is there, but disabled by default) also, if we to enable this under a replicate set, we also need to setup a key file, with the
keyFile=[path] parameter. Special note about key files: they need to have a special permission, or mongo will not start. They must be fully accessible by mongo, but not by other users. The best way to achieve that is by issuing both chown and chmod commands, the first to mongodb:mongodb and the second to 700. The : in the chown command changes the group as well. Note that these settings and the same key need to be present in all the other databases and also represented in their configuration file.
You must have the same key across all of your instances, also, you must have auth set to true in all of them. You will not be able to login now without your username and password.
setting replica set priority (must be done on the primary).
Well, this is pretty easy. You can shutdown one of the hosts (it’s best to test the primary) and wait. in about 10 or 15 seconds, you can issue an rs.status to any of the other instances, and see that one is now master. if you’ll boot the once was primary, and run rs.status on it, you’ll see that it’s now taking it’s place as a secondary. Magic.
If you want to run a query on one of the secondaries, use rs.slaveOk()