Mongo Cluster Setup
Mongodb Introduction:
Mongo is a open source database which uses nosql mechanism to store the data. The advantage of mongodb is simple and no structure is required.
Mongodb Installation:
- Create a file mongodb-org-3.2.repo with the following content on /etc/yum.repos.d/mongodb-org-3.2.repo
[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc
2. Install MongoDB server.
yum install -y mongodb-org
3. Modify the DB path if you want(/etc/mongodb.conf)
dbPath: /opt/mongodata
4. Start the mongo Service
sudo service mongod start
5. Add the service on reboot safe.
chkconfig mongod on
Optional:
If the mongo is accessed locally, we can leave the bind IP address. If mongodb is called by remote server then we need to bind the port to interface or all interface.
1. Comment out bindIp: on /etc/mongodb.conf
More information:https://docs.mongodb.com/manual/installation/
Settingup Mongo Cluster:
1. Make sure that there is a connection between hosts to the port 27017.
2. Modify /etc/mongodb.conf with replication configuration on all the hosts.
replication:
replSetName: <replicationname>
3. Restart Mongod service on all Hosts.
4. On primary hosts, login to mongo terminal and execute the following command.
>conf = {
_id: “<replicationName>”,
members:
[
{_id : 0, host : “Host1IPAddress:27017”},
{_id : 1, host : “Host2IPAddress:27017”},
{_id : 2, host : “Host3IPAddress:27017”}
]
}
>rs.initiate(conf)
5. Check the replication status.
>rs.status()
More Information:https://docs.mongodb.com/manual/tutorial/deploy-shard-cluster/
Additional Information:
Mongo has a feature arbiter.An arbiter does not have a copy of data set and cannot become a primary.Replica sets may have arbiters to add a vote in elections of for primary.
To add a host which will act as arbiter, follow below information.
1. Remove the host from existing cluster incase if the host already added in the cluster.
>rs.remove(“HOSTIPADDRESS:27017”);
2. On Primary replication cluster,
>rs.addArb(“HOSTIPAddress:27017”);
3. Check the replication configuration. You might have seen “arbiterOnly” : true,
>rs.conf()
Bonus Information:
To get current running queries on Mongodb
db.currentOp();
To make the primary to secondary, execute below mentioned command
rs.stepDown(300)
To check Replication Status.
rs.status()
To check whether the host is master or not.
db.isMaster()