一,配置规划

复制集原理(

基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB)  Paxos(mysql MGR 用的是变种))

如果发生主库宕机,复制集内部会进行投票选举,选择一个新的主库替代原有主库对外提供服务。同时复制集会自动通知

客户端程序,主库已经发生切换了。应用就会连接到新的主库。

多端口化: 28017、28018、28019、28020

  配置多实例

replcation set (复制集)配置过程 --mongodb_复制集replcation set (复制集)配置过程 --mongodb_复制集_02

1 mkdir -p /mongodb/28017/conf /mongodb/28017/data /mongodb/28017/log
2 mkdir -p /mongodb/28018/conf /mongodb/28018/data /mongodb/28018/log
3 mkdir -p /mongodb/28019/conf /mongodb/28019/data /mongodb/28019/log
4 mkdir -p /mongodb/28020/conf /mongodb/28020/data /mongodb/28020/log
5
6
7 多套配置文件
8
9 /mongodb/28017/conf/mongod.conf
10 /mongodb/28018/conf/mongod.conf
11 /mongodb/28019/conf/mongod.conf
12 /mongodb/28020/conf/mongod.conf
13
14
15
16 配置文件内容
17
18
19 vim /mongodb/28017/conf/mongod.conf
20 systemLog:
21 destination: file
22 path: /mongodb/28017/log/mongodb.log
23 logAppend: true
24 storage:
25 journal:
26 enabled: true
27 dbPath: /mongodb/28017/data
28 directoryPerDB: true
29 #engine: wiredTiger
30 wiredTiger:
31 engineConfig:
32 cacheSizeGB: 1
33 directoryForIndexes: true
34 collectionConfig:
35 blockCompressor: zlib
36 indexConfig:
37 prefixCompression: true
38 processManagement:
39 fork: true
40 net:
41 port: 28017
42 replication:
43 oplogSizeMB: 2048
44 replSetName: my_repl
45
46
47
48 cp /mongodb/28017/conf/mongod.conf /mongodb/28018/conf/
49 cp /mongodb/28017/conf/mongod.conf /mongodb/28019/conf/
50 cp /mongodb/28017/conf/mongod.conf /mongodb/28020/conf/
51
52 sed 's#28017#28018#g' /mongodb/28018/conf/mongod.conf -i
53 sed 's#28017#28019#g' /mongodb/28019/conf/mongod.conf -i
54 sed 's#28017#28020#g' /mongodb/28020/conf/mongod.conf -i

View Code

启动所有实例 

1 mongod -f /mongodb/28017/conf/mongod.conf
2 mongod -f /mongodb/28018/conf/mongod.conf
3 mongod -f /mongodb/28019/conf/mongod.conf
4 mongod -f /mongodb/28020/conf/mongod.conf

replcation set (复制集)配置过程 --mongodb_sed_03

replcation set (复制集)配置过程 --mongodb_mongodb_04

二, 复制集配置

一主两从      

mongo --port 28017 admin
1 config = {_id: 'my_repl', members: [
2 {_id: 0, host: '10.0.0.53:28017'},
3 {_id: 1, host: '10.0.0.53:28018'},
4 {_id: 2, host: '10.0.0.53:28019'}]
5 }

replcation set (复制集)配置过程 --mongodb_复制集_05

重置所有

1 rs.initiate(config)  

replcation set (复制集)配置过程 --mongodb_复制集_06

查看复制集状态

 1 rs.status(); 

replcation set (复制集)配置过程 --mongodb_mongodb_07

重新输入命令就能出来谁是主从了

一主一从一arb

搭建前添加节点  

replcation set (复制集)配置过程 --mongodb_复制集replcation set (复制集)配置过程 --mongodb_复制集_02

1 mongo -port 28017 admin
2
3 config = {_id: 'my_repl', members: [
4 {_id: 0, host: '10.0.0.51:28017'},
5 {_id: 1, host: '10.0.0.51:28018'},
6 {_id: 2, host: '10.0.0.51:28019',"arbiterOnly":true}]
7 }
8
9 rs.initiate(config)

View Code 

搭建后添加节点

删除节点

my_repl:PRIMARY> rs.remove("10.0.0.53:28019")

添加ARB节点

s.addArb("10.0.0.53:28019")


         ____ 添加从节点______


my_repl:PRIMARY> rs.add("10.0.0.53:28019");

添加ARB节点后查看

replcation set (复制集)配置过程 --mongodb_mongodb_10

三, 故障查看

故障主节点宕机:

replcation set (复制集)配置过程 --mongodb_mongodb_11

查看状态:

replcation set (复制集)配置过程 --mongodb_复制集replcation set (复制集)配置过程 --mongodb_复制集_02

1 [mongod@db03 ~]$ mongo --port 28018 admin
2 MongoDB shell version: 3.2.16
3 connecting to: 127.0.0.1:28018/admin
4 Server has startup warnings:
5 2018-12-03T15:15:33.679+0800 I CONTROL [initandlisten]
6 2018-12-03T15:15:33.679+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
7 2018-12-03T15:15:33.679+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
8 2018-12-03T15:15:33.679+0800 I CONTROL [initandlisten]
9 2018-12-03T15:15:33.679+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
10 2018-12-03T15:15:33.679+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
11 2018-12-03T15:15:33.679+0800 I CONTROL [initandlisten]
12 my_repl:PRIMARY> db.stats()
13 {
14 "db" : "admin",
15 "collections" : 0,
16 "objects" : 0,
17 "avgObjSize" : 0,
18 "dataSize" : 0,
19 "storageSize" : 0,
20 "numExtents" : 0,
21 "indexes" : 0,
22 "indexSize" : 0,
23 "fileSize" : 0,
24 "ok" : 1
25 }
26 my_repl:PRIMARY> rs.status()
27 {
28 "set" : "my_repl",
29 "date" : ISODate("2018-12-03T07:36:43.228Z"),
30 "myState" : 1,
31 "term" : NumberLong(2),
32 "heartbeatIntervalMillis" : NumberLong(2000),
33 "members" : [
34 {
35 "_id" : 0,
36 "name" : "10.0.0.53:28017",
37 "health" : 0,
38 "state" : 8,
39 "stateStr" : "(not reachable/healthy)",
40 "uptime" : 0,
41 "optime" : {
42 "ts" : Timestamp(0, 0),
43 "t" : NumberLong(-1)
44 },
45 "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
46 "lastHeartbeat" : ISODate("2018-12-03T07:36:42.063Z"),
47 "lastHeartbeatRecv" : ISODate("2018-12-03T07:35:31.656Z"),
48 "pingMs" : NumberLong(0),
49 "lastHeartbeatMessage" : "Connection refused",
50 "configVersion" : -1
51 },
52 {
53 "_id" : 1,
54 "name" : "10.0.0.53:28018",
55 "health" : 1,
56 "state" : 1,
57 "stateStr" : "PRIMARY",
58 "uptime" : 1270,
59 "optime" : {
60 "ts" : Timestamp(1543822542, 2),
61 "t" : NumberLong(2)
62 },
63 "optimeDate" : ISODate("2018-12-03T07:35:42Z"),
64 "infoMessage" : "could not find member to sync from",
65 "electionTime" : Timestamp(1543822542, 1),
66 "electionDate" : ISODate("2018-12-03T07:35:42Z"),
67 "configVersion" : 3,
68 "self" : true
69 },
70 {
71 "_id" : 2,
72 "name" : "10.0.0.53:28019",
73 "health" : 1,
74 "state" : 7,
75 "stateStr" : "ARBITER",
76 "uptime" : 267,
77 "lastHeartbeat" : ISODate("2018-12-03T07:36:42.057Z"),
78 "lastHeartbeatRecv" : ISODate("2018-12-03T07:36:42.619Z"),
79 "pingMs" : NumberLong(0),
80 "syncingTo" : "10.0.0.53:28018",
81 "configVersion" : 3
82 }
83 ],
84 "ok" : 1
85 }
86 my_repl:PRIMARY>

View Code

replcation set (复制集)配置过程 --mongodb_复制集_14

启动故障节点

replcation set (复制集)配置过程 --mongodb_复制集replcation set (复制集)配置过程 --mongodb_复制集_02

1 [mongod@db03 ~]$ mongod -f /mongodb/28017/conf/mongod.conf
2 about to fork child process, waiting until server is ready for connections.
3 forked process: 3669
4 child process started successfully, parent exiting

View Code

replcation set (复制集)配置过程 --mongodb_复制集_17

进入主节点再次查看状态

replcation set (复制集)配置过程 --mongodb_sed_18

检测到存活自动生成为从节点

四, 特殊节点配置

添加隐藏节点

rs.conf()

"_id" : 0,   ---------成员号
"host" : "10.0.0.53:28017", ----节点信息
"arbiterOnly" : false, ---- 是否为仲裁节点
"buildIndexes" : true,
"hidden" : false, --------是否为隐蔽节点
"priority" : 1, ----------------选主权重
"tags" : {

},
"slaveDelay" : NumberLong(0), ------延时的时间
"votes" : 1


 首先添加一个新的节点  


my_repl:PRIMARY> rs.add("10.0.0.53:28020")
{ "ok" : 1 }

配置隐藏节点   

replcation set (复制集)配置过程 --mongodb_复制集replcation set (复制集)配置过程 --mongodb_复制集_02

1 my_repl:PRIMARY> cfg=rs.conf()
2 {
3 "_id" : "my_repl",
4 "version" : 4,
5 "protocolVersion" : NumberLong(1),
6 "members" : [
7 {
8 "_id" : 0,
9 "host" : "10.0.0.53:28017",
10 "arbiterOnly" : false,
11 "buildIndexes" : true,
12 "hidden" : false,
13 "priority" : 1,
14 "tags" : {
15
16 },
17 "slaveDelay" : NumberLong(0),
18 "votes" : 1
19 },
20 {
21 "_id" : 1,
22 "host" : "10.0.0.53:28018",
23 "arbiterOnly" : false,
24 "buildIndexes" : true,
25 "hidden" : false,
26 "priority" : 1,
27 "tags" : {
28
29 },
30 "slaveDelay" : NumberLong(0),
31 "votes" : 1
32 },
33 {
34 "_id" : 2,
35 "host" : "10.0.0.53:28019",
36 "arbiterOnly" : true,
37 "buildIndexes" : true,
38 "hidden" : false,
39 "priority" : 1,
40 "tags" : {
41
42 },
43 "slaveDelay" : NumberLong(0),
44 "votes" : 1
45 },
46 {
47 "_id" : 3,
48 "host" : "10.0.0.53:28020",
49 "arbiterOnly" : false,
50 "buildIndexes" : true,
51 "hidden" : false,
52 "priority" : 1,
53 "tags" : {
54
55 },
56 "slaveDelay" : NumberLong(0),
57 "votes" : 1
58 }
59 ],
60 "settings" : {
61 "chainingAllowed" : true,
62 "heartbeatIntervalMillis" : 2000,
63 "heartbeatTimeoutSecs" : 10,
64 "electionTimeoutMillis" : 10000,
65 "getLastErrorModes" : {
66
67 },
68 "getLastErrorDefaults" : {
69 "w" : 1,
70 "wtimeout" : 0
71 },
72 "replicaSetId" : ObjectId("5c04d9f3892649e9da38101f")
73 }
74 }
75 my_repl:PRIMARY> cfg.members[3].priority=0
76 0
77 my_repl:PRIMARY> cfg.members[3].hidden=true
78 true
79 my_repl:PRIMARY> cfg.members[3].slaveDelay=120
80 120
81 my_repl:PRIMARY> rs.reconfig(cfg)
82 { "ok" : 1 }
83 my_repl:PRIMARY>

View Code


 配置取消出错后恢复

replcation set (复制集)配置过程 --mongodb_复制集replcation set (复制集)配置过程 --mongodb_复制集_02

1 cfg=rs.conf() 
2 cfg.members[3].priority=1
3 cfg.members[3].hidden=false
4 cfg.members[3].slaveDelay=0
5 rs.reconfig(cfg)

View Code

五, 其他

replcation set (复制集)配置过程 --mongodb_复制集replcation set (复制集)配置过程 --mongodb_复制集_02

1 --查看副本集的配置信息
2 admin> rs.config()
3 或者
4 admin> rs.conf()
5
6 --查看副本集各成员的状态
7 admin> rs.status()
8 ++++++++++++++++++++++++++++++++++++++++++++++++
9 --副本集角色切换(不要人为随便操作)
10 admin> rs.stepDown()
11 注:
12 admin> rs.freeze(300) //锁定从,使其不会转变成主库
13 freeze()和stepDown单位都是秒。
14 +++++++++++++++++++++++++++++++++++++++++++++
15 --设置副本节点可读:在副本节点执行
16 admin> rs.slaveOk()
17
18 eg:
19 admin> use app
20 switched to db app
21 app> db.createCollection('a')
22 { "ok" : 0, "errmsg" : "not master", "code" : 10107 }
23
24 --查看副本节点(监控主从延时)
25 admin> rs.printSlaveReplicationInfo()
26 source: 192.168.1.22:27017
27 syncedTo: Thu May 26 2016 10:28:56 GMT+0800 (CST)
28 0 secs (0 hrs) behind the primary
29

View Code