Introduction and Construction of MongoDB Cluster--RS

What is a mongodb cluster

Refers to a server cluster composed of multiple mongodb servers, called a mongodb cluster.

How to build a mongodb cluster:

  • Replica Set
  • Sharding
  • Master-Slaver (no longer recommended)

Introduction to Replica Set

To put it simply, the cluster contains multiple copies of data to ensure that the primary node is down and the standby node can continue to provide data services. The premise of the provision is that the data needs to be consistent with the primary node.
By default, the primary node provides all addition, deletion, and modification services, and the standby node does not provide any services. However, the standby node can be set to provide query services, so that the pressure on the primary node can be reduced. When the client performs data query, the request is automatically transferred to the standby node. This setting is called Read Preference Modes.
The arbitration node is a special kind of node. It does not store data itself. Its main function is to determine which standby node will be promoted to the master node after the master node hangs down, so the client does not need to connect to this node. Although there is only one standby node, an arbitration node is still needed to upgrade the level of the standby node. I didn't believe that there must be a arbitration node at first, but I also tried that if there is no arbitration node, the master node is still the standby node, so we still need it.
The official application architecture diagram is as follows:
image
The replication relationship between clusters
Introduction and Construction of MongoDB Cluster--RS
uses the arbitration arbiter node to
Introduction and Construction of MongoDB Cluster--RS
automatically switch and select the master
Introduction and Construction of MongoDB Cluster--RS
Read Preference architecture
Introduction and Construction of MongoDB Cluster--RS

Replica Set replica set construction process

Basic configuration can refer to: https://blog.51cto.com/hbxztc/2637521

planning

More than three mongodb nodes (or multiple instances)

Environmental preparation

Multiple ports: 28017, 28018, 28019, 28020
sets of catalogs:

su - mongod 
mkdir -p /mongodb/28017/conf /mongodb/28017/data /mongodb/28017/log
mkdir -p /mongodb/28018/conf /mongodb/28018/data /mongodb/28018/log
mkdir -p /mongodb/28019/conf /mongodb/28019/data /mongodb/28019/log
mkdir -p /mongodb/28020/conf /mongodb/28020/data /mongodb/28020/log

Multiple sets of configuration files

/mongodb/28017/conf/mongod.conf
/mongodb/28018/conf/mongod.conf
/mongodb/28019/conf/mongod.conf
/mongodb/28020/conf/mongod.conf

Configuration file content:

cat > /mongodb/28017/conf/mongod.conf <<EOF
systemLog:
  destination: file
  path: /mongodb/28017/log/mongodb.log
  logAppend: true
storage:
  journal:
    enabled: true
  dbPath: /mongodb/28017/data
  directoryPerDB: true
  #engine: wiredTiger
  wiredTiger:
    engineConfig:
      cacheSizeGB: 1
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true
processManagement:
  fork: true
net:
  bindIp: 11.111.24.4,127.0.0.1
  port: 28017
replication:
  oplogSizeMB: 2048
  replSetName: my_repl
EOF

cp  /mongodb/28017/conf/mongod.conf  /mongodb/28018/conf/
cp  /mongodb/28017/conf/mongod.conf  /mongodb/28019/conf/
cp  /mongodb/28017/conf/mongod.conf  /mongodb/28020/conf/

sed 's#28017#28018#g' /mongodb/28018/conf/mongod.conf -i
sed 's#28017#28019#g' /mongodb/28019/conf/mongod.conf -i
sed 's#28017#28020#g' /mongodb/28020/conf/mongod.conf -i

Start multiple instances for backup:

mongod -f /mongodb/28017/conf/mongod.conf
mongod -f /mongodb/28018/conf/mongod.conf
mongod -f /mongodb/28019/conf/mongod.conf
mongod -f /mongodb/28020/conf/mongod.conf

netstat -lnp|grep 280

Configure normal replication set:

Configure 1 master and 2 slaves, slave library, ordinary slave library

#登录客户端
mongo --port 28017 admin
#创建配置
config = {_id: 'my_repl', members: [
                          {_id: 0, host: '11.111.24.4:28017'},
                          {_id: 1, host: '11.111.24.4:28018'},
                          {_id: 2, host: '11.111.24.4:28019'}]
          }
#初始化
rs.initiate(config) 
#查询复制集状态
rs.status();

From the query results, you can see which node primary node (PRIMARY) and standby node (SECONDARY) are
Introduction and Construction of MongoDB Cluster--RS
configured with 1 master, 1 slave, and 1 arbiter

mongo -port 28017 admin
config = {_id: 'my_repl', members: [
                          {_id: 0, host: '11.111.24.4:28017'},
                          {_id: 1, host: '11.111.24.4:28018'},
                          {_id: 2, host: '11.111.24.4:28019',"arbiterOnly":true}]
          }                
rs.initiate(config) 

Replication set management operations

View replica set status

rs.status();    //查看整体复制集状态
rs.isMaster(); // 查看当前是否是主节点
rs.conf();   //查看复制集配置信息

Add and delete nodes

rs.remove("ip:port"); // 删除一个节点
rs.add("ip:port"); // 新增从节点
rs.addArb("ip:port"); // 新增仲裁节点

example:

//添加 arbiter节点
1、连接到主节点
[mongod@db03 ~]$ mongo --port 28018 admin
2、添加仲裁节点
my_repl:PRIMARY> rs.addArb("11.111.24.4:28020")
3、查看节点状态
my_repl:PRIMARY> rs.isMaster()
{
    "hosts" : [
        "11.111.24.4:28017",
        "11.111.24.4:28018",
        "11.111.24.4:28019"
    ],
    "arbiters" : [
        "11.111.24.4:28020"
    ],

//删除一个节点
my_repl:PRIMARY> rs.remove("11.111.24.4:28019");
{ "ok" : 1 }
my_repl:PRIMARY> rs.isMaster()

// 新增从节点
rs.add("ip:port"); 
例子:
my_repl:PRIMARY> rs.add("11.111.24.4:28019")
{ "ok" : 1 }
my_repl:PRIMARY> rs.isMaster()

Special slave node

  • Arbiter node: mainly responsible for voting in the process of electing the master, but does not store any data or provide any services
  • hidden node: hidden node, does not participate in the election of the master, and does not provide external services.
  • Delay node: Delay node. The data is behind the main library for a period of time. Because the data is delayed, it should not provide services or participate in the selection of the master, so it usually cooperates with hidden (hidden)

Under normal circumstances, delay+hidden will be configured and used together

//配置延时节点(一般延时节点也配置成hidden)
cfg=rs.conf() 
cfg.members[2].priority=0
cfg.members[2].hidden=true
cfg.members[2].slaveDelay=120
rs.reconfig(cfg)    
//取消以上配置
cfg=rs.conf() 
cfg.members[2].priority=1
cfg.members[2].hidden=false
cfg.members[2].slaveDelay=0
rs.reconfig(cfg)    
//配置成功后,通过以下命令查询配置后的属性
rs.conf(); 

Other operation commands of replica set

##查看副本集的配置信息
admin> rs.conf()
##查看副本集各成员的状态
admin> rs.status()
++++++++++++++++++++++++++++++++++++++++++++++++
##副本集角色切换(不要人为随便操作)
admin> rs.stepDown()
注:
admin> rs.freeze(300)   ##锁定从,使其不会转变成主库
freeze()和stepDown单位都是秒。
+++++++++++++++++++++++++++++++++++++++++++++
##设置副本节点可读:在副本节点执行
admin> rs.slaveOk()
##查看副本节点(监控主从延时)
admin> rs.printSlaveReplicationInfo()
source: 192.168.1.22:27017
    syncedTo: Thu May 26 2016 10:28:56 GMT+0800 (CST)
    0 secs (0 hrs) behind the primary

Official document: https://docs.mongodb.com/manual/replication/

Guess you like

Origin blog.51cto.com/hbxztc/2638843