MongoDB高可用方案之副本集(Replica Set)

Replicat Set比起传统的Master - Slave结构而言,应用场景更加多,也有了自动failover的能力。
在mongo3.2的文档中明确写道:

  1. Replica sets replace master-slave replication for most use cases. 
  2. If possible, use replica sets rather than master-slave replication for all new production deployments.

本文将简单介绍mongodb副本集搭建

〇 副本集结构图


我在此处大致理解为“MySQL中1主2从+mha_manager”的结构。
Replication通过Oplog实现语句复现。




〇 副本集成员的属性:
分别为Primary、Secondary(Secondaries)
Primary负责处理所有的write请求,并记录到oplog(operation log)中。
Secondary负责复制oplog并应用这些write请求到他们自己的数据集中。(有一些类似于MySQL Replication)


所有的副本集都可以处理读操作,但是需要设置。

最小化的副本集配置建议有三个成员:
1个Primary + 2个Secondary
或者
1个Primary + 1个Secondary + 1个Arbiter(仲裁者)

〇 Arbiter和选举机制:
Arbiter可以作为副本集的一部分,但它不是一个数据副本,故它不会成为Primary。
Arbiter在Primary不可用的时候,作为一个选举的角色存在。
如果Arbiter不存在,Primary挂掉的情况下,剩下的两个Secondary则也会进行选举。

1个Primary + 2个Secondary的情况下,failover如下:


〇 搭建

实验结构:
185(Arbiter)(后文用到)
186\187\188(1个Primary、2个Secondary)

在三台机器上分别创建对应目录然后启动mongodb:

  1. mkdir -p /data/mongo_replset
  2. mongod --dbpath=/data/mongo_replset --logpath=/data/mongo_replset/mongo.log --fork --replSet=first_replset

登录任意一台副本集的mongo shell 
此处用的是192.168.1.187

  1. > use admin

输入:

  1. > cnf = {_id:"first_replset", members:[
  2.         {_id:1, host:"192.168.1.186:27017"},
  3.         {_id:2, host:"192.168.1.187:27017"},
  4.         {_id:3, host:"192.168.1.188:27017"},
  5.         ]
  6.     }
  7. 输出结果:
  8. > {
  9.         "_id" : "first_replset",
  10.         "members" : [
  11.                 {
  12.                         "_id" : 1,
  13.                         "host" : "192.168.1.186:27017"
  14.                 },
  15.                 {
  16.                         "_id" : 2,
  17.                         "host" : "192.168.1.187:27017"
  18.                 },
  19.                 {
  20.                         "_id" : 3,
  21.                         "host" : "192.168.1.188:27017"
  22.                 }
  23.         ]
  24. }

初始化配置:

  1. > rs.initiate(cnf);
  2. { "ok" : 1 }
  3. first_replset:OTHER>
  4. first_replset:PRIMARY>
  5. first_replset:PRIMARY>

此时可以发现该mongodb实例已经成为PRIMARY了。

可以看一下这个副本集的各个成员的状态:

  1. first_replset:PRIMARY> rs.status();
  2. {
  3.         "set" : "first_replset",
  4.         "date" : ISODate("2016-11-15T08:22:10.316Z"),
  5.         "myState" : 2,
  6.         "term" : NumberLong(0),
  7.         "heartbeatIntervalMillis" : NumberLong(2000),
  8.         "members" : [
  9.                 {
  10.                         "_id" : 1,
  11.                         "name" : "192.168.1.186:27017",
  12.                         "health" : 1,
  13.                         "state" : 2,
  14.                         "stateStr" : "SECONDARY",
  15.                         ……………………
  16.                 },
  17.                 {
  18.                         "_id" : 2,
  19.                         "name" : "192.168.1.187:27017",
  20.                         "health" : 1,
  21.                         "state" : 2,
  22.                         "stateStr" : "PRIMARY",
  23.                         ……………………
  24.                 },
  25.                 {
  26.                         "_id" : 3,
  27.                         "name" : "192.168.1.188:27017",
  28.                         "health" : 1,
  29.                         "state" : 2,
  30.                         "stateStr" : "SECONDARY",
  31.                         ……………………
  32.                 }
  33.         ],
  34.         "ok" : 1
  35. }

〇 副本集复制状态测试
PRIMARY上insert:

扫描二维码关注公众号,回复: 1959479 查看本文章
  1. [root@192.168.1.187]# mongo 127.0.0.1/test
  2. MongoDB shell version: 3.2.10
  3. connecting to: 127.0.0.1/test
  4. first_replset:PRIMARY> db.test_table.insert({"id": 1})
  5. WriteResult({ "nInserted" : 1 })
  6. first_replset:PRIMARY> db.test_table.find()
  7. { "_id" : ObjectId("582abba066fdf9fae28a1ba7"), "id" : 1 }

在186、188任意SECONDARY上查询:
同master - slave结构一样,默认SECONDARY是不可读的,需要执行rs.slaveOk()。

  1. first_replset:SECONDARY> rs.slaveOk()
  2. first_replset:SECONDARY> db.test_table.find()
  3. { "_id" : ObjectId("582abba066fdf9fae28a1ba7"), "id" : 1 }

〇 failover测试

此时情况:
187:PRIAMRY
186、188:SECONDARY


停掉PRIAMRY:

  1. first_replset:PRIMARY> use admin
  2. switched to db admin
  3. first_replset:PRIMARY> db.shutdownServer()
  4. server should be down...
  5. 2016-11-15T16:31:25.921+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
  6. 2016-11-15T16:31:27.299+0800 I NETWORK [thread1] Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
  7. 2016-11-15T16:31:27.299+0800 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017]
  8. 2016-11-15T16:31:27.299+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
  9. 2016-11-15T16:31:27.302+0800 I NETWORK [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
  10. 2016-11-15T16:31:27.302+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
  11. 2016-11-15T16:31:27.302+0800 I NETWORK [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) failed failed
  12. >

(原SECONDARY)188上查到,此时188已经为PRIMARY了。

  1. first_replset:PRIMARY> rs.status()
  2. {
  3.         "set" : "first_replset",
  4.         "date" : ISODate("2016-11-15T16:31:45.773Z"),
  5.         "myState" : 1,
  6.         "term" : NumberLong(2),
  7.         "heartbeatIntervalMillis" : NumberLong(2000),
  8.         "members" : [
  9.                 {
  10.                         "_id" : 1,
  11.                         "name" : "192.168.1.186:27017",
  12.                         "health" : 1,
  13.                         "state" : 2,
  14.                         "stateStr" : "SECONDARY",
  15.                         ……………………
  16.                 },
  17.                 {
  18.                         "_id" : 2,
  19.                         "name" : "192.168.1.187:27017",
  20.                         "health" : 0,
  21.                         "state" : 8,
  22.                         "stateStr" : "(not reachable/healthy)",
  23.                         ……………………
  24.                 },
  25.                 {
  26.                         "_id" : 3,
  27.                         "name" : "192.168.1.188:27017",
  28.                         "health" : 1,
  29.                         "state" : 1,
  30.                         "stateStr" : "PRIMARY",
  31.                         ……………………
  32.                 }
  33.         ],
  34.         "ok" : 1
  35. }

〇 在副本集中添加一个属性为Arbiter的成员
然此处只做添加实践,实际上并不建议在Secondary-Primary-Secondary的结构上再多一个Arbiter成员形成偶数个节点。
文档写到:

  1. Only add an arbiter to sets with even numbers of voting members. 
  2. If you add an arbiter to a set with an odd number of voting members, the set may suffer from tied elections.

192.168.1.185(另一台)上启动一个mongod实例:

  1. mkdir -p /data/arb
  2. mongod --dbpath=/data/arb/ --logpath=/data/arb/mongo.log --fork --replSet=first_replset

在failover后的PRIMARY节点添加Arbiter

  1. first_replset:PRIMARY> rs.addArb("192.168.1.185:27017")
  2. { "ok" : 1 }
  3. first_replset:PRIMARY> rs.status()
  4.                 …………………
  5.                 {
  6.                         "_id" : 4,
  7.                         "name" : "192.168.1.185:27017",
  8.                         "health" : 1,
  9.                         "state" : 7,
  10.                         "stateStr" : "ARBITER",
  11.                         …………………
  12.                 }
  13.                 ……………………

此时回到arbiter的mongo shell,发现正如文档所说,arbiter是不会存有副本集中数据的。

  1. first_replset:ARBITER> rs.slaveOk()
  2. first_replset:ARBITER> use test;
  3. switched to db test
  4. first_replset:ARBITER> show tables;
  5. first_replset:ARBITER> db.test_table.find();
  6. Error: error: { "ok" : 0, "errmsg" : "node is recovering", "code" : 13436 }

〇 文档:
Replication > Replica Set Tutorials > Replica Set Deployment Tutorials 
Reference > mongo Shell Methods > Replication Methods

猜你喜欢

转载自www.linuxidc.com/Linux/2017-03/142379.htm