千家信息网

详解MongoDB复制集

发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,何为复制集?复制集是额外的数据副本,是跨多个服务器同步数据的过程,复制集提供了冗余并增加了数据可用性,通过复制集可以对硬件故障和中断的服务进行恢复。复制集主从同步的原理和mysql类似,主节点记录在其
千家信息网最后更新 2025年01月31日详解MongoDB复制集

何为复制集?

复制集是额外的数据副本,是跨多个服务器同步数据的过程,复制集提供了冗余并增加了数据可用性,通过复制集可以对硬件故障和中断的服务进行恢复。

复制集主从同步的原理和mysql类似,主节点记录在其上的所有操作到oplog中,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。


复制集的优势如下:


(1)让数据更安全


(2)高数据可用性(24*7)


(3)灾难恢复


(4)无停机维护(如备份、索引重建、故障转移)


(5)读缩放(额外的副本读取)


(6)副本集对应用程序是透明。

MongoDB复制集结构原理图分析

  • .mongodb的复制集至少需要两个节点。其中一个是主节点(Primary),负责处理客户端请求,其余的都是从节点(Secondary),负责复制主节点上的数据。

  • mongodb各个节点常见的搭配方式为:一主一从或一主多从。

  • 客户端在主节点写入数据,在从节点读取数据,主节点与从节点进行数据交互,保障数据的一致性。如果其中一个节点出现故障,其他节点马上会将业务接过来而无需停机操作

复制集的特点:

1.N 个节点的集群


2.任何节点可作为主节点


3.所有写入操作都在主节点上



4.自动故障转移


5.自动恢复

MongoDB复制集部署

  • 在一台CentOS7主机上使用yum在线安装Mongodb,并创建多实例,进行部署MongoDB复制集

首先配置网络YUM源,baseurl(下载路径)指定为mongodb官网提供的yum仓库

vim /etc/yum.repos.d/mongodb.repo

[mongodb-org]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/ #指定获得下载的路径

gpgcheck=1 #表示对从这个源下载的rpm包进行校验

enabled=1 #表示启用这个源。

gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc

重新加载yum源,并使用yum命令下载安装mongodb

yum list

yum -y install mongodb-org

准备4个实例,暂时开启3个, 1主2从 再额外添加群集(追加实例)也可以撤销实例

  • 创建数据文件和日志文件存储路径,并赋予权限


    [root@localhost ~]# mkdir -p /data/mongodb{2,3,4}
    [root@localhost ~]# mkdir /data/logs
    [root@localhost ~]# touch /data/logs/mongodb{2,3,4}.log
    [root@localhost ~]# chmod 777 /data/logs/mongodb*
    [root@localhost ~]# ll /data/logs/
    总用量 0
    -rwxrwxrwx. 1 root root 0 9月 15 22:31 mongodb2.log
    -rwxrwxrwx. 1 root root 0 9月 15 22:31 mongodb3.log
    -rwxrwxrwx. 1 root root 0 9月 15 22:31 mongodb4.log

编辑4个MongoDB实例的配置文件

  • 先编辑yum安装的默认实例的配置文件/etc/mongod.conf,指定监听IP,端口默认为27017,开启replication参数配置,replSetName:true(自定义)

[root@localhost ~]# vim /etc/mongod.conf

# mongod.conf

# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:

# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:

port: 27017 #默认端口
bindIp: 0.0.0.0 #监听任意地址

#security:

#operationProfiling:

replication: #去掉前面的"#"注释,开启该参数设置
replSetName: true #设置复制集名称

  • 复制配置文件给其他实例,并将mongodb2.conf 中的port参数配置为27018,mongod3.conf中的port参数配置为27019,mongod4.conf中的port参数配置为27020。 同样也将dbpath和logpath参数修改为对应的路径值

cp /etc/mongod.conf /etc/mongod2.conf

cp /etc/mongod2.conf /etc/mongod3.conf

cp /etc/mongod2.conf /etc/mongod4.conf

  • 实例2的配置文件mongodb2.conf 修改

vim /etc/mongod2.conf

systemLog:

destination: file

logAppend: true

path: /data/logs/mongodb2.log

storage:

dbPath: /data/mongodb/mongodb2

journal:

enabled: true

port: 27018

bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

  • 实例3的配置文件mongodb3.conf 修改

vim /etc/mongod3.conf

systemLog:

destination: file

logAppend: true

path: /data/logs/mongodb3.log

storage:

dbPath: /data/mongodb/mongodb3

journal:

enabled: true

port: 27019

bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

  • 实例4的配置文件mongodb4.conf 修改

vim /etc/mongod4.conf

systemLog:

destination: file

logAppend: true

path: /data/logs/mongodb4.log

storage:

dbPath: /data/mongodb/mongodb4

journal:

enabled: true

port: 27020

bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.

#security:

#operationProfiling:

replication:
replSetName: true

启动mongodb各实例

[root@localhost ~]# mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93576
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod2.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93608
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod3.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93636
child process started successfully, parent exiting
[root@localhost ~]# mongod -f /etc/mongod4.conf
about to fork child process, waiting until server is ready for connections.
forked process: 93664
child process started successfully, parent exiting
[root@localhost ~]# netstat -antp | grep mongod //查看mongodb进程状态
tcp 0 0 0.0.0.0:27019 0.0.0.0:* LISTEN 93636/mongod
tcp 0 0 0.0.0.0:27020 0.0.0.0:* LISTEN 93664/mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 93576/mongod
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 93608/mongod

配置三个节点的复制集

[root@localhost ~]# mongo //进入其中一个实例
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.7

> rs.status() //查看复制集状态,提示复制集还未配置
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"operationTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}

> cfg={"_id":"true","members":[{"_id":0,"host":"192.168.195.137:27017"},{"_id":1,"host":"192.168.195.137:27018"},{"_id":2,"host":"192.168.195.137:27019"}]} // 定义cfg初始化参数
{
"_id" : "true",
"members" : [
{
"_id" : 0,
"host" : "192.168.195.137:27017"
},
{
"_id" : 1,
"host" : "192.168.195.137:27018"
},
{
"_id" : 2,
"host" : "192.168.195.137:27019"
}
]
}

> rs.initiate(cfg) //初始化并启动复制集

{"ok" : 1}

true:PRIMARY> rs.status() //再次查看复制集的状态信息
{
"set" : "true",
"date" : ISODate("2018-09-15T15:39:48.426Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.195.137:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //此节点成为主节点
"uptime" : 1371,
"optime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},

....................


{
"_id" : 1,
"name" : "192.168.195.137:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //此节点为从节点
"uptime" : 18,
"optime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},

...................


{
"_id" : 2,
"name" : "192.168.195.137:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", //此节点为从节点
"uptime" : 18,
"optime" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1537025984, 1),
"t" : NumberLong(1)
},

  • 添加节点

true:PRIMARY> rs.add("192.168.195.137:27020")

true:PRIMARY> rs.status()

..............

{
"_id" : 3,
"name" : "192.168.195.137:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 8,
"optime" : {
"ts" : Timestamp(1537026818, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1537026818, 1),
"t" : NumberLong(1)
},

.................

  • 删除节点

true:PRIMARY> rs.remove("192.168.195.137:27020")

模拟故障自动转移切换

[root@localhost ~]# mongod -f /etc/mongod.conf --shutdown //关闭主节点服务
killing process with pid: 93576
[root@localhost ~]# mongo --port 27018 //进入其中一个从节点
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7

true:PRIMARY> rs.status() //查看复制集信息

..............


},
"members" : [
{
"_id" : 0,
"name" : "192.168.195.137:27017", //原来的主节点健康值为0
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},

.................


{
"_id" : 1,
"name" : "192.168.195.137:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", //此节点切换成主节点
"uptime" : 2657,
"optime" : {
"ts" : Timestamp(1537027275, 1),
"t" : NumberLong(2)
},

................


{
"_id" : 2,
"name" : "192.168.195.137:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1312,
"optime" : {
"ts" : Timestamp(1537027275, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1537027275, 1),
"t" : NumberLong(2)
},

手动进行主从切换

[root@localhost ~]# mongod -f /etc/mongod.conf #开启刚才关闭的端口为27017的节点实例

about to fork child process, waiting until server is ready for connections.
forked process: 94723
child process started successfully, parent exiting
[root@localhost ~]# mongo --port 27018 #进入主节点服务器实例
MongoDB shell version v3.6.7
connecting to: mongodb://127.0.0.1:27018/
MongoDB server version: 3.6.7

true:PRIMARY> rs.freeze(30) //暂停30s不参与选举

true:PRIMARY> rs.stepDown(60.30) //交出主节点位置,维持从节点状态不少于60秒,等待30秒使主节点和从节点日志同步

true:SECONDARY> rs.status() //可以看到本实例已经切换成从节点

................


"members" : [
{
"_id" : 0,
"name" : "192.168.195.137:27017", // 端口27017的节点变成了主节点
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 167,
"optime" : {
"ts" : Timestamp(1537027620, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1537027620, 1),
"t" : NumberLong(3)
},

..................

{
"_id" : 1,
"name" : "192.168.195.137:27018", // 端口27018的节点变成了从节点

"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2997,
"optime" : {
"ts" : Timestamp(1537027620, 1),
"t" : NumberLong(3)
},

...................

{
"_id" : 2,
"name" : "192.168.195.137:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1651,
"optime" : {
"ts" : Timestamp(1537027620, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1537027620, 1),
"t" : NumberLong(3)
},

0