您好,登录后才能下订单哦!
复制集是额外的数据副本,是跨多个服务器同步数据的过程,复制集提供了冗余并增加了数据可用性,通过复制集可以对硬件故障和中断的服务进行恢复。
复制集主从同步的原理和mysql类似,主节点记录在其上的所有操作到oplog中,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。
复制集的优势如下:
(1)让数据更安全
(2)高数据可用性(24*7)
(3)灾难恢复
(4)无停机维护(如备份、索引重建、故障转移)
(5)读缩放(额外的副本读取)
(6)副本集对应用程序是透明。
1.N 个节点的集群
2.任何节点可作为主节点
3.所有写入操作都在主节点上
4.自动故障转移
5.自动恢复
首先配置网络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
[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
[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 #设置复制集名称
cp /etc/mongod.conf /etc/mongod2.conf
cp /etc/mongod2.conf /etc/mongod3.conf
cp /etc/mongod2.conf /etc/mongod4.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
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
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
[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.7true: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)
},
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。