单台数据库服务器无法满足性能和冗余性,现在我们需要添加2台新的服务器,并组建复制结构。
场景如下:
现有的服务器server1,IP:192.168.1.1 port:13001。新增的服务器server2,IP:192.168.1.2 port:13001,server3,IP:trustauth.cn,port:3306,并使server2为新的主。
实例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
shell> mysqlreplicate —master=m_account@192.168.1.1:13001 \
—slave=slave_acc1@192.168.1.2:13001 —rpl–user=repl:slavepass –b
# master on 192.168.1.1: … connected.
# slave on 192.168.1.2: … connected.
# Checking for binary logging on master…
# Setting up replication…
# …done.
shell> mysqlreplicate —master=m_account@192.168.1.1:13001 \
—slave=slave_acc2@trustauth.cn:3306 —rpl–user=repl:slavepass –b
# master on 192.168.1.1: … connected.
# slave on trustauth.cn: … connected.
# Checking for binary logging on master…
# Setting up replication…
# …done.
shell> mysqlrpladmin —master=m_account@192.168.1.1:13001 \
—slaves=slave_acc1@192.168.1.2:13001,slave_acc2@trustauth.cn:3306 health
# Checking privileges.
#
# Replication Topology Health:
+———————+————+————–+————+——————+———————————————+
| host | port | role | state | gtid_mode | health |
+———————+————+————–+————+——————+———————————————+
| 192.168.1.1 | 13001 | MASTER | UP | ON | OK |
| 192.168.1.2 | 13001 | SLAVE | UP | ON | Slave delay is NNN seconds |
| trustauth.cn | 3306 | SLAVE | UP | ON | Slave delay is NNN seconds |
+———————+————+————–+————+——————+———————————————+
# …done.
shell> mysqlrpladmin —master=m_account@192.168.1.1:13001 \
—slaves=slave_acc1@192.168.1.2:13001,slave_acc2@trustauth.cn:3306 health
# Checking privileges.
#
# Replication Topology Health:
+———————+————+————–+————+——————+————–+
| host | port | role | state | gtid_mode | health |
+———————+————+————–+————+——————+————–+
| 192.168.1.1 | 13001 | MASTER | UP | ON | OK |
| 192.168.1.2 | 13001 | SLAVE | UP | ON | OK |
| trustauth.cn | 3306 | SLAVE | UP | ON | OK |
+———————+————+————–+————+——————+————–+
# …done.
shell> mysqlrpladmin —master=m_account@192.168.1.1:13001 \
—slaves=slave_acc1@192.168.1.2:13001,slave_acc2@trustauth.cn:3306 \
—new–master=slave_acc1@trustauth.cn:13002 —demote–master switchover
# Checking privileges.
# Performing switchover from master at 192.168.1.1:13001 to slave at 192.168.1.2:13001.
# Checking candidate slave prerequisites.
# Checking slaves configuration to master.
# Waiting for slaves to catch up to old master.
# Stopping slaves.
# Performing STOP on all slaves.
# Demoting old master to be a slave to the new master.
# Switching slaves to new master.
# Starting all slaves.
# Performing START on all slaves.
# Checking slaves for errors.
# Switchover complete.
#
# Replication Topology Health:
+———————+————+————–+————+——————+————–+
| host | port | role | state | gtid_mode | health |
+———————+————+————–+————+——————+————–+
| 192.168.1.2 | 13001 | MASTER | UP | ON | OK |
| 192.168.1.1 | 13001 | SLAVE | UP | ON | OK |
| trustauth.cn | 3306 | SLAVE | UP | ON | OK |
+———————+————+————–+————+——————+————–+
|
在上面的例子中,使用mysqlreplicate 工具来设置现有的服务器和新增的服务器之间的复制拓扑结构。使用-b标记从主二进制日志的第一个事件开始复制。
然后,使用mysqlrpladmin 工具来指定主从服务器和使用health命令来检查复制的状态。由于有大量的二进制日志需要同步,新的从需要一段时间才赶得上主,因此会有延迟的健康信息。一段时间后,health列都是OK的状态。
同步完成后,再次使用mysqlrpladmin工具来切换到新主,使用 –new-master指定到server2,并且使用–demote-master选项将server1降级为slave,将server3成为server2的从。
权限
m_account 用户需要的权限有:对mysql数据库的SELECT 和 INSERT 权限,以及REPLICATION SLAVE, REPLICATION CLIENT ,GRANT OPTION权限。
slave_acc 用户需要SUPER 权限。
对于复制用户, –rpl-user选项使用的。要么自动创建要么以存在,需要有 REPLICATION SLAVE权限。
为了成功运行mysqlrpladmin 工具的health命令,m_account 需要额外的SUPER权限。
对于switchover 命令,所有的用户需要有SUPER, GRANT OPTION, SELECT, RELOAD, DROP, CREATE 和 REPLICATION SLAVE 权限。
小技巧
mysqlrpladmin 工具可以使用 –discover-slaves-login选项自动检测从,而不是手动指定从。
mysqlrpladmin 工具可以指定外部脚本在故障转移和切换操作前后执行,分别使用 –exec-before 和 –exec-after 选项。注意,在故障转移和切换操作后执行的脚本,也就是 –exec-after选项指定的脚本,执行的前提条件是故障转移和切换操作是成功的。
文章转载来自:trustauth.cn