1、简介mongoDB复制是将数据同步在多个服务器的过程,类似Mysql Replication是一种异步复制;复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的
1、简介
mongoDB复制是将数据同步在多个服务器的过程,类似Mysql Replication是一种异步复制;
复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。
2、MonGoDB复制特点
(1) mongodb的节点有三种角色:primary,seconfary,arbiter,
primary负责处理客户端请求,读写数据;
secondary通过oplog复制主节点上的数据,也可读数据;
arbiter节点不存储数据,仅投票功能,所以不需要单独的硬件配置;
(2)如果primary不可以用,secondary会提升为新的primary; 由参数electionTimeoutMillis=10s控制
3、实验环境
MongoDB-3.0.6版本
一台物理机器上三个MongoDB实例,分别占用三个节点
172.16.101.54 27017
172.16.101.54 27018
172.16.101.54 27019
4、创建数据和日志目录
[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/data/{db27017,db27018,db27019}
[root@sht-sgmhadoopcm-01 mongodb]# mkdir -p /usr/local/mongodb/log
5、配置文件/etc/mongod.conf
三个节点分别创建配置文件,文件内容区别是节点不同,其他都相同,参数replSetname用来指定副本集名称为rs0
[root@sht-sgmhadoopcm-01 mongodb]# ll /etc/mongod2701*.conf
-rw-r--r-- 1 root root 405 Nov 2 20:21 /etc/mongod27017.conf
-rw-r--r-- 1 root root 405 Nov 2 20:13 /etc/mongod27018.conf
-rw-r--r-- 1 root root 405 Nov 2 20:13 /etc/mongod27019.conf
mongodb配置文件每个冒号后面要有一个空格,否则会报错
[root@sht-sgmhadoopcm-01 mongodb]# cat /etc/mongod27017.conf
systemLog:
destination: file
path: "/usr/local/mongodb/log/mongod27017.log"
logAppend: true
storage:
dbPath: /usr/local/mongodb/data/db27017
journal:
enabled: true
proceSSManagement:
fork: true
pidFilePath: /usr/local/mongodb/data/db27017/mongod27017.pid
net:
port: 27017
bindIp: 0.0.0.0
setParameter:
enableLocalhostAuthBypass: false
replication:
replSetName: rs0
6、启动MongoDB
分别启动三个节点的MongoDB服务,并指定配置文件
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27018.conf
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27019.conf
7、初始化副本集
登录27017节点
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27017/admin
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "sht-sgmhadoopcm-01:27017",
"ok" : 1
}
添加成员到副本集,priority大的节点提成为主的可能性就大
rs0:PRIMARY> rs.add('172.16.101.54:27018')
{ "ok" : 1 }
rs0:PRIMARY> rs.add('172.16.101.54:27019',priority:5)
{ "ok" : 1 }
查看副本集配置
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 3,
"members" : [
{
"_id" : 0,
"host" : "sht-sgmhadoopcm-01:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "172.16.101.54:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 2,
"host" : "172.16.101.54:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrORModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
查看各个角色状态
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2018-11-02T12:27:44.570Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "sht-sgmhadoopcm-01:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 626,
"optime" : Timestamp(1541161589, 1),
"optimeDate" : ISODate("2018-11-02T12:26:29Z"),
"electionTime" : Timestamp(1541161465, 2),
"electionDate" : ISODate("2018-11-02T12:24:25Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "172.16.101.54:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 161,
"optime" : Timestamp(1541161589, 1),
"optimeDate" : ISODate("2018-11-02T12:26:29Z"),
"lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"),
"lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.311Z"),
"pingMs" : 0,
"syncingTo" : "sht-sgmhadoopcm-01:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "172.16.101.54:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 75,
"optime" : Timestamp(1541161589, 1),
"optimeDate" : ISODate("2018-11-02T12:26:29Z"),
"lastHeartbeat" : ISODate("2018-11-02T12:27:43.430Z"),
"lastHeartbeatRecv" : ISODate("2018-11-02T12:27:43.430Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 3
}
],
"ok" : 1
}
8、数据同步测试
向27017节点插入数据
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27017/admin
rs0:PRIMARY> db.user.insert({"username":"test","age":12,"sex":"m"})
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313Da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
数据成功的同步到27018和27019节点
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27018/admin
rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')
rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27019/admin
rs0:SECONDARY> db.getMongo().setReadPref('secondaryPreferred')
rs0:SECONDARY> db.user.find()
{ "_id" : ObjectId("5bdc65ff048de313da6c425a"), "username" : "test", "age" : 12, "sex" : "m" }
9、AutoFailover测试
kill当前的primary27017节点
[root@sht-sgmhadoopcm-01 mongodb]# ps -ef|grep mongod
root 5703 1 0 20:17 ? 00:00:52 bin/mongod --config /etc/mongod27017.conf
root 5747 1 0 20:17 ? 00:00:51 bin/mongod --config /etc/mongod27018.conf
root 5979 1 0 20:20 ? 00:00:52 bin/mongod --config /etc/mongod27019.conf
[root@sht-sgmhadoopcm-01 mongodb]# kill -9 5703
可以看到27019变成primary,此时27017不可用
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongo 172.16.101.54:27018/admin
rs0:SECONDARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2018-11-02T15:10:18.746Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "sht-sgmhadoopcm-01:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2018-11-02T15:10:18.631Z"),
"lastHeartbeatRecv" : ISODate("2018-11-02T15:09:28.583Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to sht-sgmhadoopcm-01:27017; couldn't connect to server sht-sgmhadoopcm-01:27017 (172.16.101.54), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "172.16.101.54:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10349,
"optime" : Timestamp(1541170688, 2),
"optimeDate" : ISODate("2018-11-02T14:58:08Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 2,
"name" : "172.16.101.54:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 9827,
"optime" : Timestamp(1541170688, 2),
"optimeDate" : ISODate("2018-11-02T14:58:08Z"),
"lastHeartbeat" : ISODate("2018-11-02T15:10:18.611Z"),
"lastHeartbeatRecv" : ISODate("2018-11-02T15:10:18.592Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1541171370, 1),
"electionDate" : ISODate("2018-11-02T15:09:30Z"),
"configVersion" : 3
}
],
"ok" : 1
}
重新启动27017节点,会自动变成了27019的备库,说明auto failover成功了
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2018-11-02T15:13:27.035Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "sht-sgmhadoopcm-01:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 42,
"optime" : Timestamp(1541170688, 2),
"optimeDate" : ISODate("2018-11-02T14:58:08Z"),
"lastHeartbeat" : ISODate("2018-11-02T15:13:26.764Z"),
"lastHeartbeatRecv" : ISODate("2018-11-02T15:13:25.055Z"),
"pingMs" : 0,
"configVersion" : 3
},
{
"_id" : 1,
"name" : "172.16.101.54:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 10017,
"optime" : Timestamp(1541170688, 2),
"optimeDate" : ISODate("2018-11-02T14:58:08Z"),
"lastHeartbeat" : ISODate("2018-11-02T15:13:26.662Z"),
"lastHeartbeatRecv" : ISODate("2018-11-02T15:13:26.678Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "could not find member to sync from",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "172.16.101.54:27019",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 10349,
"optime" : Timestamp(1541170688, 2),
"optimeDate" : ISODate("2018-11-02T14:58:08Z"),
"electionTime" : Timestamp(1541171370, 1),
"electionDate" : ISODate("2018-11-02T15:09:30Z"),
"configVersion" : 3,
"self" : true
}
],
"ok" : 1
}
FAQ
[root@sht-sgmhadoopcm-01 mongodb]# bin/mongod --config /etc/mongod27017.conf &
[1] 4885
[root@sht-sgmhadoopcm-01 mongodb]# Error parsing YAML config file: yaml-cpp: error at line 8, column 11: illegal map value
try 'bin/mongod --help' for more information
[1]+ Exit 2 bin/mongod --config /etc/mongod27017.conf
原因是配置文件/etc/mongod.conf冒号后面没有跟空格。
参考链接
MongoDB Replication
--结束END--
本文标题: MongoDB Replica Set 副本集
本文链接: https://lsjlt.com/news/39208.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0