Thursday, October 13, 2016

MongoDB Installation + Replication using CentOS with using config file version 3.2.10

Step 1: vi /etc/yum.repos.d/mongodb-enterprise.repo
[mongodb-enterprise]
name=MongoDB Enterprise Repository
baseurl=https://repo.mongodb.com/yum/redhat/$releasever/mongodb-enterprise/3.2/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

Step 2: Install the MongoDB packages and associated tools
sudo yum install -y mongodb-enterprise

Step 3: connect to mongo
mongo
Check Mongo is working correctly or not.
You can also check by using this command "service mongod status".

Step 4: I have selected /apps/mongodb/appdb to store mongodb data files for database

Step 5 : Create Data Folders for database
mkdir -p /apps/mongodb/appdb/rs0
mkdir -p /apps/mongodb/appdb/rs1
mkdir -p /apps/mongodb/appdb/rs2

chown mongod.mongod /apps/mongodb/appdb/rs0
chown mongod.mongod /apps/mongodb/appdb/rs1
chown mongod.mongod /apps/mongodb/appdb/rs2

Step 6: Generate mongodb key file for Authentication 
cd /apps/mongodb/appdb
openssl rand -base64 741 > mongodb-keyfile
chmod 600 mongodb-keyfile
chown mongod.mongod /apps/mongodb/appdb/mongodb-keyfile

Step 7: Configuration file first node
vi /apps/mongodb/appdb/rs0/mongod.config
*******************************************************************
systemLog:
  destination: file
  logAppend: true
  path: /apps/mongodb/appdb/rs0/mongod.log
storage:
  dbPath: /apps/mongodb/appdb/rs0
  journal:
    enabled: true
  engine: wiredTiger
processManagement:
  fork: true
net:
  port: 30000
  bindIp: 127.0.0.1,cignex.mongodb.com
replication:
  replSetName: rsapp
security:

  keyFile: /apps/mongodb/appdb/mongodb-keyfile

Step 7: Configuration file second node
vi /apps/mongodb/appdb/rs1/mongod.config
*******************************************************************
systemLog:
  destination: file
  logAppend: true
  path: /apps/mongodb/appdb/rs1/mongod.log
storage:
  dbPath: /apps/mongodb/appdb/rs1
  journal:
    enabled: true
  engine: wiredTiger
processManagement:
  fork: true
net:
  port: 30001
  bindIp: 127.0.0.1,cignex.mongodb.com
replication:
  replSetName: rsapp
security:

  keyFile: /apps/mongodb/appdb/mongodb-keyfile

Step 8: Configuration file third node
vi /apps/mongodb/appdb/rs2/mongod.config
*******************************************************************
systemLog:
  destination: file
  logAppend: true
  path: /apps/mongodb/appdb/rs2/mongod.log
storage:
  dbPath: /apps/mongodb/appdb/rs2
  journal:
    enabled: true
  engine: wiredTiger
processManagement:
  fork: true
net:
  port: 30002
  bindIp: 127.0.0.1,cignex.mongodb.com
replication:
  replSetName: rsapp
security:
  keyFile: /apps/mongodb/appdb/mongodb-keyfile

Step 9: start mongodb process for all the nodes
/usr/bin/mongod -f /apps/mongodb/appdb/rs0/mongod.conf
/usr/bin/mongod -f /apps/mongodb/appdb/rs1/mongod.conf
/usr/bin/mongod -f /apps/mongodb/appdb/rs2/mongod.conf

Step 10:connect to mongo port 30000 and switch to admin database
mongo --port 30000
use admin

Step 11: set configuration variable & initiate replication
rsconf = {_id: "rsapp",members: [{_id: 0,host: "cignex.mongodb.com:30000"}]}

rs.initiate(rsconf);

Step 12: Create Super user for your database
db.createUser({user:"admin",pwd:"admin@123",roles:["root"]})

db.auth("admin","admin@123");

Step 13: Add mongodb Servers
rs.add("cignex.mongodb.com:30001");

rs.add("cignex.mongodb.com:30002");

Step 14: Check you replication Status
MongoDB Enterprise rsapp:PRIMARY> rs.status();
{
        "set" : "rsapp",
        "date" : ISODate("2016-10-06T13:05:34.894Z"),
        "myState" : 1,
        "term" : NumberLong(3),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "cignex.mongodb.com:30000",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 14123,
                        "optime" : {
                                "ts" : Timestamp(1475745944, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2016-10-06T09:25:44Z"),
                        "electionTime" : Timestamp(1475745943, 1),
                        "electionDate" : ISODate("2016-10-06T09:25:43Z"),
                        "configVersion" : 4,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "cignex.mongodb.com:30001",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 13772,
                        "optime" : {
                                "ts" : Timestamp(1475745944, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2016-10-06T09:25:44Z"),
                        "lastHeartbeat" : ISODate("2016-10-06T13:05:33.339Z"),
                        "lastHeartbeatRecv" : ISODate("2016-10-06T13:05:33.379Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "cignex.mongodb.com:30000",
                        "configVersion" : 4
                },
                {
                        "_id" : 2,
                        "name" : "cignex.mongodb.com:30002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 13771,
                        "optime" : {
                                "ts" : Timestamp(1475745944, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2016-10-06T09:25:44Z"),
                        "lastHeartbeat" : ISODate("2016-10-06T13:05:33.131Z"),
                        "lastHeartbeatRecv" : ISODate("2016-10-06T13:05:34.204Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "cignex.mongodb.com:30000",
                        "configVersion" : 4
                }
        ],
        "ok" : 1
}




No comments:

Post a Comment