About MongoDB cluster mode - Replica Set

A, Replica Set cluster is divided into two architectures:

  • An odd number of nodes constituting Replica Set, all the data sets owned by the node. Minimum architecture: a Primary node, two Secondary nodes
  • Replica Set + even number of nodes consisting of an arbitration node, the node that owns the data set, only nodes involved in arbitration arbitration elected Primary node. Minimum architecture: a Primary node, a Secondary node, a node Arbiter

Take the next three servers, for example, deployment framework with arbitration.

IP addresses Operating system version MongoDB version port Features
10.10.18.10 7.5  4.0  27017  Primary 
10.10.18.11 Centos7.5  4.0  27017  Secondary
10.10.18.12 Centos7.5  4.0  27017  Arbiter

Two, MongoDB installation and deployment

Installation Environment: CentOS 7.5

Mounting base dependencies

yum install cyrus-sasl cyrus-sasl-gssapi cyrus-sasl-plain krb5-libs libcurl libpcap lm_sensors-libs net-snmp net-snmp-agent-libs openldap openssl rpm-libs tcp_wrappers-libs

Download the installation package

https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.10.tgz

Resolve installation package

tar -xzvf mongodb-linux-x86_64-4.0.10.tgz -C /data/
ln -s /data/mongodb-linux-x86_64-4.0.10 /data/mongodb

Adding Environment Variables

echo "export PATH=\$PATH:/data/mongodb/bin" >> ~/.bashrc
source ~/.bashrc
mongo --version

Primary configuration file:

systemLog:
  destination: file
  path: "/data/mongodb/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/data/mongodb/data"
  journal:
      enabled: true
  wiredTiger:
      engineConfig:
        cacheSizeGB: 2
processManagement:
  fork: true
  pidFilePath: "/data/mongodb/pid/m.pid"
net:
  bindIp: 10.10.18.10
  port: 27017
replication:
  replSetName: "rs0"

Secondary profile

systemLog:
  destination: file
  path: "/data/mongodb/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/data/mongodb/data"
  journal:
      enabled: true
  wiredTiger:
      engineConfig:
        cacheSizeGB: 2
processManagement:
  fork: true
  pidFilePath: "/data/mongodb/pid/m.pid"
net:
  bindIp: 10.10.18.11
  port: 27017
replication:
  replSetName: "rs0"

Arbiter's profile

systemLog:
  destination: file
  path: "/data/mongodb/log/mongod.log"
  logAppend: true
storage:
  dbPath: "/data/mongodb/data"
  journal:
      enabled: true
  wiredTiger:
      engineConfig:
        cacheSizeGB: 2
processManagement:
  fork: true
  pidFilePath: "/data/mongodb/pid/m.pid"
net:
  bindIp: 10.10.18.12
  port: 27017
replication:
  replSetName: "rs0"

Start mongdb on all three servers

mongod -f /data/mongodb/mongod.conf

Third, configure the Replica Set

Log in Renyiyitai three servers, log mongo

mongo --host 10.10.18.10

>cfg={ _id:"rs0",members:[{_id:0,host:'10.10.18.10:27017',priority:1},{_id:1,host:'10.10.18.11:27017',priority:1},{_id:2,host:'10.10.18.12:27017',arbiterOnly:true}] };

>rs.initiate(cfg)

View Replica Set Configuration

> rs.conf()

{
 "_id" : "rs0",
 "version" : 1,
 "protocolVersion" : NumberLong(1),
 "writeConcernMajorityJournalDefault" : true,
 "members" : [
 {
 "_id" : 0,
 "host" : "10.10.18.10:27017",
 "arbiterOnly" : false,
 "buildIndexes" : true,
 "hidden" : false,
 "priority" : 1,
 "tags" : {

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

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

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

},
 "getLastErrorDefaults" : {
 "w" : 1,
 "wtimeout" : 0
 },
 "replicaSetId" : ObjectId("5cff76e5e57e23a5bc7054e2")
 }
}

 

Fourth, verification Replica Set

Primary data is inserted in the

 

rs0:PRIMARY>  show dbs
admin  0.000GB
config  0.000GB
local  0.000GB
rs0:PRIMARY> db.users.insertOne(
... {
... name:"sue",
... age: 26,
... status:"pending"
... })
{
        "acknowledged" : true,
        "insertedId" : ObjectId("5cff79e8993e70290a081d04")
}


rs0:PRIMARY> db.users.find()
{ "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }

In Secondary, the default is not allowed to read

rs0:SECONDARY> db.users.find()
Error: error: {
        "operationTime" : Timestamp(1560247181, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1560247181, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}

Five, fault simulation

Stop mongo process on the Primary, Secondary login mongo viewed on

mongo --host 10.10.18.11


rs0:PRIMARY> db.users.find()
{ "_id" : ObjectId("5cff79e8993e70290a081d04"), "name" : "sue", "age" : 26, "status" : "pending" }

I found the original Secondary becomes Primary, and can be queried.

Mongo open process on the original Primary server, which becomes Secondary.

Guess you like

Origin www.linuxidc.com/Linux/2019-06/159049.htm