GS部署mongodb副本集集群

安装MongoDB安装包

rpm -ivh mongodb-org-server-4.4.6-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-4.4.6-1.el7.x86_64.rpm
rpm -ivh mongodb-database-tools-rhel70-x86_64-100.3.1.rpm

修改配置

从primary或secondary上copy mongod.conf配置到新的节点
修改对应的目录和监听地址

echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
vim /etc/mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /home/data/mongo_data
  journal:
    enabled: true
  directoryPerDB: true
  wiredTiger:
    engineConfig:
      cacheSizeGB: 12
      directoryForIndexes: true

# how the process runs
processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
  port: 27017
  bindIp: 192.168.16.183,127.0.0.1
  maxIncomingConnections: 5000


security:
  keyFile: /var/log/mongodb/KeyFile.file
  authorization: enabled

#operationProfiling:

replication:
  replSetName: rs0
  enableMajorityReadConcern: false

启动

systemctl start mongod

#注意mongodb的目录权限

初始化副本集

config = { _id:"rs0", members:[
                     {_id:0,host:"192.168.16.181:27017",priority:90}, 
                     {_id:1,host:"192.168.16.182:27017",priority:90}, 
                    {_id:2,host:"192.168.16.180:27017",arbiterOnly:true}
    ]
}

rs.initiate(config); 

添加新节点到副本集

登录primary节点 mongo mongodb://127.0.0.1:27017/wBird_MainDB -u wbird -p Mcache@2021

rs.status()  #查看当前副本集成员

#执行添加指令
rs0:PRIMARY> rs.add("192.168.16.183:27017")
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1624432093, 300),
                "signature" : {
                        "hash" : BinData(0,"PYtdDrW0+9XOj/6+LvLlbU2j8bQ="),
                        "keyId" : NumberLong("6969143689746579460")
                }
        },
        "operationTime" : Timestamp(1624432090, 2001)


添加仲裁节点

rs0:PRIMARY> rs.addArb('192.168.16.181:27077')
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1626083256, 1),
                "signature" : {
                        "hash" : BinData(0,"HIgC06FF8Hyt9S60GKrMhH8oflo="),
                        "keyId" : NumberLong("6983966635497357315")
                }
        },
        "operationTime" : Timestamp(1626083256, 1)

等待数据同步完成

rs.status()

 

 

 


从副本集删除节点

#在主节点执行
rs0:PRIMARY>  rs.remove("192.168.16.181:27017")
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1624929502, 1),
                "signature" : {
                        "hash" : BinData(0,"+8PQm6CMhau45mF3mXuQsjnL8Mo="),
                        "keyId" : NumberLong("6969143689746579460")
                }
        },
        "operationTime" : Timestamp(1624929502, 1)

rs.remove("192.168.16.183:27017")
rs.remove("192.168.16.180:27017")
.
.
.

主动切换primary

重新选举的条件有

  1. 复制集被reconfig
  2. Secondary节点检测到Primary宕机时,会触发新Primary的选举
  3. 当有Primary节点主动stepDown(主动降级为Secondary)时,也会触发新的Primary选举

这里我们通过第三种方式,让primary降级为secondary,然后再下掉primary

rs0:PRIMARY> rs.stepDown()
{
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1624930220, 1),
                "signature" : {
                        "hash" : BinData(0,"Man+GGO8SvZqI+AmSj/KK8NQAYE="),
                        "keyId" : NumberLong("6969143689746579460")
                }
        },
        "operationTime" : Timestamp(1624930220, 1)
}
rs0:SECONDARY> 

修改同步secondary同步源

rs0:SECONDARY> rs.syncFrom("192.168.17.189:27017");
{
        "syncFromRequested" : "192.168.17.189:27017",
        "prevSyncTarget" : "192.168.17.190:27017",
        "ok" : 1,
        "$clusterTime" : {
                "clusterTime" : Timestamp(1624932969, 1),
                "signature" : {
                        "hash" : BinData(0,"s2UlBNUp54CjdORyFZNqESkuDBg="),
                        "keyId" : NumberLong("6969143689746579460")
                }
        },
        "operationTime" : Timestamp(1624932969, 1)
}
posted @ 2024-10-29 11:33  羊脂玉净瓶  阅读(23)  评论(0)    收藏  举报