Creating mongodb replica set

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).

Setup

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:

var conf=rs.conf();
conf.members[0].name="192.168.56.101:27017"
rs.reconfig(conf)

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:

rs.add("192.168.56.102:27017")
rs.add("192.168.56.103:27017")

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:

  1. Edit /etc/hostname, to e.g. yourhost.dyndns.org
  2. Run: hostname -F /etc/hostname
  3. IP addresses are acceptable
  4. 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.

Mongo users

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: db.addUser({user:'user',pwd:'password',roles:["root","clusterAdmin","userAdmin","userAdminAnyDatabase","readWriteAnyDatabase"]})

depends on the version of the server, root might be fine. Also, mongo 2.63 state that the createUser() function should be used now. If you are looking for other roles, you can find them here.

Security

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.

How to create a key

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).

Testing

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()

Other links:

  1. changing user roles

Dealing with ubuntu 14.04

Dealing with the new version from Canonical seems to have it’s own share of issues.

Here, I’m trying to get some of them clear, as these questions keep popping up in forums.

  1. Key combinations stop working. Most known are the language input combo (defaulted to super+ctrl) and the Ctrl+space for the auto completion if you are programming in any current IDE. There are two tools you can use for these problems:  the first is dconf-editor.
    sudo apt install dconf-editor

    This will enable you to get rid of the Ctrl+space binding, which you match to the image below:Screenshot from 2014-05-08 14:06:38If your triggers are not empty – please make them so. and do not use root to run this, this is a user config editor. This will return Ctrl+space to all the IDE’s.

  2. For the Super+space issue, we have yet another tool. This one goes by the name of gnome-tweak-tool and you can set the typing option there. usually just set these up (to anything) solves this issue:Screenshot from 2014-05-08 14:14:59Another great tool is the unity-tweak-tool which allows you to change system fonts, themes and more important, the focus – which many find to be too large in Ubuntu.
  3. Scrollbars: these horrible Ubuntu scrollbars can be removed with
    gsettings set com.canonical.desktop.interface scrollbar-mode normal

    this is also a user (non root) command. It will effect immediately and no need to repeat it. If some windows does not update, you can log out and in again.