Setup MongoDB 3.2 Geographical Replica Set in Ubuntu 15.10

Interestingly, i needed to setup a Replica Set on Ubuntu 15.10 for MongoDB 3.2 which is the latest Ubuntu and MongoDB version. This also serve as a tutorial for anyone who is interested in setting up a MongoDB Replica Set as well.

Import the public key used by the package management system.

Login to your server as root, we will need to import the public key use by the package manager for mongodb, just fire the following command,

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

And we are good here.

Create a Source list file for MongoDB and Installation

Next, we need to add the source list for MongoDB. However, since MongoDB did not support 15.10 at this time, we will use debian ones

echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.0 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

Now, we will need to update the server and install mongodb

sudo apt-get update
sudo apt-get install -y mongodb-org

And after everything finished running, you should have your mongodb running.

sudo service mongod start

if no error is given, meaning your MongoDB has successfully installed.

Setup Replica Set

Now, assuming you did the above on 3 machines, you will need to setup each replica with the following steps,

head over to /etc/mongod.conf and replace your config with the one show below,

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

storage:
    dbPath: "/data/mongo"
    directoryPerDB: false
    journal:
        enabled: true
    engine: "wiredTiger"
    wiredTiger:
        engineConfig:
            cacheSizeGB: 1
        collectionConfig:
            blockCompressor: snappy
systemLog:
    destination: file
    path: "/var/log/mongodb.log"
    logAppend: true
    logRotate: reopen
    timeStampFormat: iso8601-utc
net:
  port: 27017
  bindIp: 0.0.0.0

replication:
   oplogSizeMB: 500
   replSetName: dstTest

Next, create the folder for MongoDB data,

mkdir -p /data/mongo
chown -R mongodb:mongodb /data

Once you have done that, restart MongoDB and make sure there is no error.

sudo service mongod restart

Next we need to setup each replica in MongoDB.

Configure the servers to include in replica set

Assuming you have 3 machines, with the following hostname

sg.db.hungred.com
us.db.hungred.com
tw.db.hungred.com

Now, head over to the primary MongoDB server that you would like it to be primary (in my case, us.db.hungred.com) and enter to mongodb using the command below,

mongo
rs.initiate

then paste the following

rs.reconfig({ _id : "testDB", members : [ {_id : 0, host : "sg.db.hungred.com:27017", priority: 5}, {_id : 1, host : "us.db.hungred.com:27017", priority: 4}, {_id : 2, host : "tw.db.hungred.com:27017", priority: 3 } ] })

take note of the priority i have given it and make sure this is one liner (yeah its messy but that's how i copy and paste it in one piece), then check your conf

rs.conf()

and status at

rs.status()

and you got yourself a 3 location replica set of MongoDB!

***** UPDATE *****

Adding Security Authentication

If you want to add authentication into your setup, you will need to visit /etc/mongod.conf and add the following

security:
  keyFile: /data/mongodb-keyfile

on all of your primary and secondary Mongodb server. The file will need to generate this way,

openssl rand -base64 741 > /data/mongodb-keyfile
chmod 600 mongodb-keyfile

This is to ensure all replica set can communicate with each other. Once you have generated the file above on the primary MongoDB server, copy the same file to other secondary server and update the /etc/mongod.conf on each secondary server along with it.