Qiang Blog

Just another zhangjingqiang's blog.

How to create MongoDB replica set on CentOS 7

Nodes

  • node1
  • node2

/etc/hosts

192.168.0.1              node1
192.168.0.2              node2

Install

On both nodes

vi /etc/yum.repos.d/mongodb-org.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

yum install mongodb-org

Create keyfile

On both nodes

openssl rand -base64 756 > /root/keyfile
chmod 400 /root/keyfile

Edit config file

On both nodes

service mongodb stop
mkdir /mongo-metadata

vi /etc/mongod.conf
storage:
  dbPath: /mongo-metadata
  ...
net:
  port: 27017
security:
  keyFile: /root/keyfile
replication:
  replSetName: rs0

mongod --config /etc/mongod.conf

Start replica set and add node

On node1

mongo
rs.initiate()
use admin
db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
);
db.auth('admin', 'password')
1
rs.add("node2")
{ "ok" : 1 }
rs.conf()
{
        "_id" : "rs0",
        "version" : 2,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "node1:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "node2:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("596ee39369296db40861b82f")
        }
}

Official

centos mongodb

MongoDB Deployment Checklist

Hardware

  • RAM
  • Disk space
  • Disk speed
  • CPU
  • Network

Security

  • Protection of network traffic
  • Access control

Monitoring

  • Hardware usage
  • Health checks
  • MMS Monitoring
  • Client performance monitoring

Disaster recovery

  • Evaluate risk
  • Have a plan
  • Test your plan
  • Have a backup plan

Performance

  • Load testing

MongoDB in Action, 2nd Edition

deployment mongodb