Setup Mongo Replica Set with Authentication

Replica set involves at least 3 instances of Mongo running with respect to voting so that any one server can take over as master in case of failure. So first thing is to install mongo in 3 servers in your network which is a good idea for redundancy. You can easily install mongo by following instructions at mongodb documentation site. In this blog we will be deploying mongo db replica set in linux environment.

Once you have completed installation of mongo, it’s time for some configuration tweaks before you start the databases. But first generate a key file in your master instance for authentication so that the 3 mongo instance can talk to each other securely. 

openssl rand -base64 741 > /etc/mongo.key

Copy the key file in your other instances in same location.

Now edit the following in all of the 3 mongo DB /etc/mongo.conf

replSet=rs0 #[unique name for your set]
rest=true # if you want rest api to view status of your mongodb

Note: auth=true only works for single mongodb instance authentication, keyFile is required for replica or sharding authentication setup.

Now start your mongo db process

service mongod start

start interactive mongo to initiate the replication and setup replication configuration

# will return no configuration found
> config = {_id: rs0, members:[{_id:0, host: 'ip:port'},{_id:1, host: 'ip:port'},{_id:2, host: 'ip:port'}]}
# will return success status
> exit

that’s it! you have setup mongo DB replica set with authentication.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: