MongoDB复制是将数据同步到多个服务器的过程;
复制集提供了数据的冗余备份并提高了数据的可用性,通常可以保证数据的安全性;
复制集还允许您从硬件故障和服务中断中恢复数据。
- mongodb的复制集至少需要两个节点。其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据。
- mongodb各个节点常见的搭配方式为:一主一从、一主多从。
- 主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。
(以上图片引用网址:http://www.runoob.com/mongodb/mongodb-replication.html )
以上结构图中,客户端从主节点读取数据,在客户端写入数据到主节点时,主节点与从节点进行数据交互保障数据的一致性。
- N 个节点的集群
- 任何节点可作为主节点
- 所有写入操作都在主节点上
- 自动故障转移
- 自动恢复
在同一台服务器上创建MongoDB的多实例(4个实例)来做MongoDB主从的实验。
至于如何安装MongoDB?请参考之前博文 Linux 平台安装MongoDB 4.0(最新版)
#创建各实例的数据目录
mkdir -p /data/mongodb/mongodb{1,2,3,4}
#创建实例配置目录
mkdir -p /data/conf/
#创建实例的日志目录
mkdir -p /data/logs/
#创建各实例的日志文件
touch /data/logs/mongodb{1,2,3,4}.log
#赋予日志文件权限777
chmod 777 /data/logs/*.log
vim /data/mongodb/mongodb1.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: /data/logs/mongodb1.log //mongodb1的日志文件路径
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb1/ //mongodb1的数据文件路径
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27017 //mongodb1的进程号
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //删除“#”,开启复制集功能
replSetName: test-rc //名称为test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
#复制默认实例的配置文件
cp -p /data/mongodb/mongodb1.conf /data/conf/mongodb2.conf
cp -p /data/mongodb/mongodb1.conf /data/conf/mongodb3.conf
cp -p /data/mongodb/mongodb1.conf /data/conf/mongodb4.conf
cat /data/conf/mongodb2.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: /data/logs/mongodb2.log //mongodb2的日志文件路径
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb2/ //mongodb2的数据文件路径
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27018 //mongodb2的进程号
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //删除“#”,开启复制集功能
replSetName: test-rc #名称为test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
cat /data/conf/mongodb3.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: /data/logs/mongodb3.log //mongodb3的日志文件路径
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb3/ //mongodb3的数据文件路径
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27019 //mongodb3的进程号
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //删除“#”,开启复制集功能
replSetName: test-rc #名称为test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
cat /data/conf/mongodb4.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: /data/logs/mongodb4.log //mongodb4的日志文件路径
#Where and how to store data.
storage:
dbPath: /data/mongodb/mongodb4/ //mongodb4的数据文件路径
journal:
enabled: true
#engine:
#mmapv1:
#wiredTiger:
#how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/mongodb1/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
#network interfaces
net:
port: 27020 //mongodb4的进程号
bindIp: 0.0.0.0 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication: //删除“#”,开启复制集功能
replSetName: test-rc //名称为test-rc
#sharding:
##Enterprise-Only Options
#auditLog:
#snmp:
for i in 1 2 3 4
do
mongod -f /data/conf/mongodb$i.conf
done
[root@localhost conf]# netstat -tunlp | grep mongod
mongo
> rs.status()
> cfg={"_id":"test-rc","members":[{"_id":0,"host":"192.168.100.100:27017"},{"_id":1,"host":"192.168.100.100:27018"},{"_id":2,"host":"192.168.100.100:27019"}]}
> rs.initiate(cfg)
test-rc:PRIMARY> rs.status()
{
"set" : "test-rc",
"date" : ISODate("2018-07-14T04:46:58.710Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1531543608, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.100.100:27017",
"health" : 1, //健康状态
"state" : 1, //1:为主节点 ; 2:为从节点
"stateStr" : "PRIMARY", //主节点
"uptime" : 2886,
"optime" : {
"ts" : Timestamp(1531543618, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-14T04:46:58Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1531543426, 1),
"electionDate" : ISODate("2018-07-14T04:43:46Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.100.100:27018",
"health" : 1, //健康状态
"state" : 2, //1:为主节点 ; 2:为从节点
"stateStr" : "SECONDARY", //从节点
"uptime" : 202,
"optime" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-14T04:46:48Z"),
"optimeDurableDate" : ISODate("2018-07-14T04:46:48Z"),
"lastHeartbeat" : ISODate("2018-07-14T04:46:56.765Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T04:46:57.395Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27017",
"syncSourceHost" : "192.168.100.100:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.100.100:27019",
"health" : 1, //健康状态
"state" : 2, //1:为主节点 ; 2:为从节点
"stateStr" : "SECONDARY", //从节点
"uptime" : 202,
"optime" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1531543608, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-07-14T04:46:48Z"),
"optimeDurableDate" : ISODate("2018-07-14T04:46:48Z"),
"lastHeartbeat" : ISODate("2018-07-14T04:46:56.769Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T04:46:57.441Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27017",
"syncSourceHost" : "192.168.100.100:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1531543618, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531543618, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
特别提醒:如以上可知道主节点在192.168.100.100:27017节点上
注意:严禁在从库做任何修改操作
test-rc:PRIMARY> rs.add("192.168.100.100:27020")
test-rc:PRIMARY> rs.status()
test-rc:PRIMARY> rs.remove("192.168.100.100:27018")
test-rc:PRIMARY> rs.status()
发现192.168.100.100:27018节点已经没有相关信息了
test-rc:PRIMARY> exit
netstat -tunlp | grep mongod
可以查看到共有4个实例的进程信息
kill -9 48211
mongo --port 27019
test-rc:PRIMARY> rs.status()
{
"_id" : 2,
"name" : "192.168.100.100:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1547,
"optime" : {
"ts" : Timestamp(1531544567, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-07-14T05:02:47Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1531544345, 1),
"electionDate" : ISODate("2018-07-14T04:59:05Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 3,
"name" : "192.168.100.100:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 700,
"optime" : {
"ts" : Timestamp(1531544567, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1531544567, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2018-07-14T05:02:47Z"),
"optimeDurableDate" : ISODate("2018-07-14T05:02:47Z"),
"lastHeartbeat" : ISODate("2018-07-14T05:02:56.150Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T05:02:56.289Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27019",
"syncSourceHost" : "192.168.100.100:27019",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 3
}
特别提醒:主节点已经到192.168.100.100:27019节点上,说明主节点已经自动切换了
test-rc:PRIMARY> rs.freeze(30)
{
"operationTime" : Timestamp(1531544867, 1),
"ok" : 0,
"errmsg" : "cannot freeze node when primary or running for election. state: Primary",
"code" : 95,
"codeName" : "NotSecondary",
"$clusterTime" : {
"clusterTime" : Timestamp(1531544867, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
test-rc:PRIMARY> rs.stepDown(60,30)
> test-rc:PRIMARY> rs.stepDown(60,30)
2018-07-14T01:08:07.326-0400 E QUERY [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27019' :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1398:12
@(shell):1:1
2018-07-14T01:08:07.328-0400 I NETWORK [js] trying reconnect to 127.0.0.1:27019 failed
2018-07-14T01:08:07.329-0400 I NETWORK [js] reconnect 127.0.0.1:27019 ok
> test-rc:PRIMARY> rs.status()
{
"set" : "test-rc",
"date" : ISODate("2018-07-14T05:10:31.161Z"),
"myState" : 2,
"term" : NumberLong(3),
"syncingTo" : "192.168.100.100:27020",
"syncSourceHost" : "192.168.100.100:27020",
"syncSourceId" : 3,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"appliedOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1531545018, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.100.100:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 70,
"optime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-07-14T05:10:28Z"),
"syncingTo" : "192.168.100.100:27020",
"syncSourceHost" : "192.168.100.100:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "192.168.100.100:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 68,
"optime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-07-14T05:10:28Z"),
"optimeDurableDate" : ISODate("2018-07-14T05:10:28Z"),
"lastHeartbeat" : ISODate("2018-07-14T05:10:30.079Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T05:10:31.094Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.100.100:27020",
"syncSourceHost" : "192.168.100.100:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 3,
"name" : "192.168.100.100:27020",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 68,
"optime" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1531545028, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-07-14T05:10:28Z"),
"optimeDurableDate" : ISODate("2018-07-14T05:10:28Z"),
"lastHeartbeat" : ISODate("2018-07-14T05:10:30.079Z"),
"lastHeartbeatRecv" : ISODate("2018-07-14T05:10:29.561Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1531544897, 1),
"electionDate" : ISODate("2018-07-14T05:08:17Z"),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1531545028, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1531545028, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
特别提醒:此刻主节点已经在192.168.100.100:27020节点上
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。