mysqlrpladmin 是一个MySQL复制的管理工具。设计的目的为了很容易的恢复计划性对主的维护或者以一个事件让主意外离线。

使主离线和切换控制另一个从。在这种情况下,主上没有任何事务的丢失,所有的从都可以赶上主。一旦从已经从主上读取所有的事件,将主关闭掉并切换为从。

恢复当掉的主是件痛苦的事,无法知道哪些事务是失败的。新的主(候选从)必需是最新最近的从,这取决于服务器的版本。但是,也会存在事务在当掉的主上执行完但没有发给从。该工具从从列表中选择考虑候选从。如果没有从符合要求,该操作将搜索已知的从列表。

该工具还提供了以下的命令来管理复制拓扑,如下:

elect  该命令只提供给支持全局事务标识符(GTIDs)服务器,选举最好的从,在切换或故障转移时报告最好的从。最好从选举第一个满足先决条件的从。GTIDs需要5.6.5或更高的版本。该命令需要 –master 选项,要么 –slaves 或者 –discover-slaves-login 选项。

failover  该命令只提供给支持全局事务标识符(GTIDs)服务器。切换到最佳的从上。该命令将测试列出的先决条件的每个候选从。一旦候选从被选中,将是其他从的主,收集其他不是候选从上执行的事务。这种方式,候选从将是最佳最新的从。该命令需要–slaves选项。不允许–discover-slaves-login选项,因为对于故障转移,主需要脱机或其他原因不可到达,因此没法发现从。对于这个命令–master选项忽略。

gtid  该命令只提供给支持全局事务标识符(GTIDs)服务器。显示GTID变量@@GLOBAL.GTID_EXECUTED, @@GLOBAL.GTID_PURGED, @@GLOBAL.GTID_OWNED内容。还显示所有服务器的全局唯一标识符(UUID)。该命令需要下面的组合之一的选项:–master 和 –slaves 或者  –master 和 –discover-slaves-login。

health  显示复制拓扑的状况。默认情况下,显示主机名、端口、服务器角色(master或者slave)、服务器状态(UP = is connected, WARN = not connected but can ping, DOWN = not connected and cannot ping)、GTID_MODE、健康状态。该命令需要下面的组合之一:

–master 和 –slaves

–master 和 –discover-slaves-login

主的健康状态基于如果GTID_MODE=ON时,服务器必需开启二进制日志,且必需存在具有REPLICATE SLAVE权限的用户。

从的健康状态基于IO_THREAD 和 SQL_THREADS 必需运行,必需连接到主,没有错误,没有启用GTID的落后从不超过–max-position选项指定的阀值,从正在读取正确的主日志文件,从延迟不高于–seconds-behind 选项阀值。

reset     在所有从上执行  STOP SLAVE 和 RESET SLAVE 命令。需要–slaves选项。不允许–discover-slaves-login 选项,因为不能提供除了从IO线程停止的其它预期的结果。可以使用–master选项,在这种情况下,将执行额外的检查来确保从是否关联到给定的主,也就是复制的配置。

start 在所有从上执行START SLAVE命令。需要–slaves选项。不允许–discover-slaves-login 选项,因为不能提供除了从IO线程停止的其它预期的结果。可以使用–master选项,在这种情况下,将执行额外的检查来确保从是否关联到给定的主,也就是复制的配置。

stop  在所有从上执行STOP SLAVE 命令。需要–slaves选项。不允许–discover-slaves-login 选项,因为不能提供除了从IO线程停止的其它预期的结果。可以使用–master选项,在这种情况下,将执行额外的检查来确保从是否关联到给定的主,也就是复制的配置。

switchover   将候选从切换为新主。该命令适用于启用GTID服务器和没启用GTID场景。该命令需要下面的组合之一:

–master 、–new-master和 –slaves

–master 、–new-master和 –discover-slaves-login

判定主失效的依据是:如果连接到主丢失,等待–ping秒数并再次检查。如果主连接丢失和主无法ping通或重新连接,故障转移事件发生。

对于需要指定多个服务器的命令,参数需要以逗号分隔。形式如下:

1

2

[:]@[:][:] or

[:][:]

该工具允许用户连接到主自动发现从。为了使用自动发现从功能,所有从必需使用–report-host 和 –report-port 启动参数来指定正确的从的主机名和IP端口。如果这些缺失或报告不正确的信息,从健康可能无法正确报告或从可能不会全部列出。对于无法连接到的从或者IO线程关闭了(未连接到主)从发现功能将被忽略。

该工具允许在切换操作过程中将主降级为从。–demote-master选项一旦新的主被建立了,使旧的主作为新主的从。这使得主的角色在一组服务器建旋转。

该工具允许用户指定外部脚本在切换和故障转移命令之前或之后执行。用户可以通过 –exec-before 和 –exec-after 选项指定。脚本的返回码来确定是否成功执行,0表示成功,非0表示失败并返回错误信息。

该工具还允许用户记录命令期间的所有动作。–log 选项需要指定有效的路径和文件名。只有该选项指定了,该功能才生效。–log-age 选项用于指定日志存放天数,默认是7天。旧的日志自动轮滚掉。日志的格式包括的项目有事件的日期和时间、事件的级别(informational – INFO, warning – WARN, error – ERROR, critical failure – CRITICAL)和消息。

该工具具有一些选项来获取更相信的信息说明。 一些选项用于特定的某些命令。当选项不适用请求的命令发出警告信息。每个命令和选项的简要概述如下:

start, stop, reset 命令要求 –slaves选项列出拓扑中的所有从。该工具也可以指定–master选项在执行命令前检查指定的从是否关联到给定的主,确保该命令只应用于连接到正确主的从。

该选项需要 elect, health 和 gtid 命令包括–master选项指定现有的主,要不就是–slaves选项列出拓扑中的所有从或 –discover-slaves-login 选项提供的用户名和密码来发现拓扑中已注册和连接到主的任何从。

切换需要的选项包括 –master 指定现有的主, –new-master 指定候选从,将成为新的主,要不就是 –slaves 列出拓扑中的可选从或 –discover-slaves-login 选项提供的用户名和密码来发现拓扑中已注册和连接到主的任何从。

故障转移命令只需要–slaves选项明确列出所有拓扑中的从,当主当掉时使用。

使用–verbose选项来查看切换或故障转移时的额外的监控报告和附加信息。

选项

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

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

MySQLUtilitiesmysqlrpladminversion 1.5.3

Licensetype: GPLv2

Usage: mysqlrpladmin —slaves=root@localhost:3306

mysqlrpladmin – administrationutilityfor MySQLreplication

Options:

—versionshowprogram‘s version number and exit

–help                display this help message and exit

–license             display program’s licenseand exit

—candidates=CANDIDATES

connectioninformationfor candidateslaveserversfor

failoverin theform:

[:]@[:][:] or

[:][:] or

path>[] Validonlywithfailovercommand.

Listmultipleslavesin comma–separatedlist.

—discover–slaves–login=DISCOVER

atstartup, querymasterfor allregisteredslavesand

use theusernameand passwordspecifiedto connect.

Supplytheuserand passwordin theform

[:] or . For example,

—discover–slaves–login=joe:secretwilluse ‘joe’ as

theuserand ‘secret’ as thepasswordfor each

discoveredslave.

—exec–after=EXEC_AFTER

nameofscriptto executeafterfailoveror switchover

—exec–before=EXEC_BEFORE

nameofscriptto executebeforefailoveror

switchover

—log=LOG_FILEspecify a logfileto use for loggingmessages

—log–age=LOG_AGEspecifymaximumageoflogentriesin days. Entries

olderthanthis willbepurgedonstartup. Default = 7

days.

—master=MASTERconnectioninformationfor masterserverin theform:

[:]@[:][:] or

[:][:] or

path>[].

—max–position=MAX_POSITION

usedto detectslavedelay. Themaximumdifference

betweenthemaster‘s log position and the slave’s

reportedreadpositionofthemaster. A valuegreater

thanthis meanstheslaveis toofarbehindthe

master. Default is 0.

—ping=PINGNumberofpingattemptsfor detectingdownedserver.

—seconds–behind=MAX_DELAY

usedto detectslavedelay. Themaximumnumberof

secondsbehindthemasterpermittedbeforeslaveis

consideredbehindthemaster. Default is 0.

—slaves=SLAVESconnectioninformationfor slaveserversin theform:

[:]@[:][:] or

[:][:] or

path>[].Listmultipleslavesin comma–

separatedlist.

—timeout=TIMEOUTmaximumtimeoutin secondsto waitfor each

replicationcommandto complete. For example, timeout

for slavewaitingto catch upto master. Default =

300.

—script–threshold=SCRIPT_THRESHOLD

Valuefor externalscriptsto triggerabortingthe

operationif resultis greaterthanor equalto the

threshold. Default = None (nothresholdchecking).

—new–master=NEW_MASTER

connectioninformationfor theslaveto beusedto

replacethemasterfor switchover, in theform:

[:]@[:][:] or

[:][:] or

path>[]. Validonlywithswitchovercommand.

—forceignoreprerequisitecheckresultsor some

inconsistenciesfound (e.g. erranttransactionson

slaves) and executeaction

–f FORMAT, —format=FORMAT

displaytheoutputin eithergrid (default), tab, csv,

or verticalformat

—demote–mastermakemaster a slaveafterswitchover.

—no–healthturnoffhealthreportafterswitchoveror failover.

–v, —verbosecontrolhowmuchinformationis displayed. e.g., –v =

verbose, –vv = moreverbose, –vvv = debug

–q, —quietturnoffallmessagesfor quietexecution.

—rpl–user=RPL_USERtheuserand passwordfor thereplicationuser

requirement, in theform: [:] or

. E.g. rpl:passwd

—ssl–ca=SSL_CAThepathto a filethatcontains a listoftrustedSSL

CAs.

—ssl–cert=SSL_CERTThenameoftheSSLcertificatefileto use for

establishing a secureconnection.

—ssl–key=SSL_KEYThenameoftheSSLkeyfileto use for establishing a

secureconnection.

AvailableCommands:

elect– performbestslaveelectionand reportbestslave

failover– conductfailoverfrommasterto bestslave

gtid– showstatusofglobal transactionidvariables

alsodisplaysuuidsfor allservers

health– displaythereplicationhealth

reset– stopand resetallslaves

start– startallslaves

stop– stopallslaves

switchover– performslavepromotion

Note:

elect, gtidand healthrequire —masterand either

—slavesor —discover–slaves–login;

failoverrequires —slaves;

switchoverrequires —master, —new–masterand either

—slavesor —discover–slaves–login;

start, stopand resetrequire —slaves (and —masteris optional)

注意

用户必须具有相应的权限来执行SHOW SLAVE STATUS,SHOW MASTER STATUS,SHOW VARIABLES 和 REPLICATE SLAVE 权限。在启动该工具前检测主、从、候选者权限。

不推荐混合使用IP地址和主机名。

如果用户没有指定–rpl-user和用户指定切换或故障转移命令,该工具将检查从是否使用–master-info-repository=TABLE。如果没有,将出错并停止。

所有的命令要求使用–slaves或–discover-slaves-login选项,两者不能同时使用。事实上,一些命令只允许使用–slaves选项,更安全的指定从列表。因为–discover-slaves-login 不会提供最近的可用从列表。

MySQL客户端工具的路径需要包含在PATH环境变量中,以便使用login-paths验证机制。允许使用my_print_defaults 来从登陆配置文件(.mylogin.cnf)读取login-path值。

实例

拓扑中所有服务器GTID_MODE=ON (server version 5.6.5 or higher) ,选举最佳从,所有从都指定–slaves选项。命令如下:

1

2

3

4

5

shell> mysqlrpladmin —master=root@localhost:3331 \

—slaves=root@localhost:3332,root@localhost:3333,root@localhost:3334 elect

# Electing candidate slave from known slaves.

# Best slave found is located on localhost:3332.

# …done.

提供候选从名单执行最佳从选举,如下:

1

2

3

4

5

6

shell> mysqlrpladmin —master=root@localhost:3331 \

—slaves=root@localhost:3332,root@localhost:3333,root@localhost:3334 \

—candidates=root@localhost:3333,root@localhost:3334 elect

# Electing candidate slave from candidate list then slaves list.

# Best slave found is located on localhost:3332.

# …done.

主失败执行故障切换,如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

shell> mysqlrpladmin\

—slaves=root@localhost:3332,root@localhost:3333,root@localhost:3334 \

—candidates=root@localhost:3333,root@localhost:3334 failover

# Performing failover.

# Candidate slave localhost:3333 will become the new master.

# Preparing candidate for failover.

# Creating replication user if it does not exist.

# Stopping slaves.

# Performing STOP on all slaves.

# Switching slaves to new master.

# Starting slaves.

# Performing START on all slaves.

# Checking slaves for errors.

# Failover complete.

# …done.

要查看GTID_MODE=ON (server version 5.6.5 or higher) 拓扑的复制健康和发现连接到主的所有从,命令如下。使用上述故障转移命令的结果:

1

2

3

4

5

6

7

8

9

10

11

12

13

shell> mysqlrpladmin —master=root@localhost:3333 \

—slaves=root@localhost:3332,root@localhost:3334 health

# Getting health for master: localhost:3333.

#

# Replication Topology Health:

+——————+———–+————–+————+——————+————–+

| host| port| role| state| gtid_mode| health|

+——————+———–+————–+————+——————+————–+

| localhost| 3333| MASTER| UP| ON| OK|

| localhost| 3332| SLAVE| UP| ON| OK|

| localhost| 3334| SLAVE| UP| ON| OK|

+——————+———–+————–+————+——————+————–+

# …done.

要查看详细的复制状况报告,但不需要复制监控检查,使用–verbose选项。如下:

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

72

73

74

75

76

shell> mysqlrpladmin —master=root@localhost:3331 \

—slaves=root@localhost:3332,root@localhost:3333,root@localhost:3334 \

—verbosehealth

# Getting health for master: localhost:3331.

# Attempting to contact localhost … Success

# Attempting to contact localhost … Success

# Attempting to contact localhost … Success

# Attempting to contact localhost … Success

#

# Replication Topology Health:

*************************1. row *************************

host: localhost

port: 3331

role: MASTER

state: UP

gtid_mode: ON

health: OK

version: 5.6.5–m8–debug–log

master_log_file: mysql–bin.000001

master_log_pos: 571

IO_Thread:

SQL_Thread:

Secs_Behind:

Remaining_Delay:

IO_Error_Num:

IO_Error:

*************************2. row *************************

host: localhost

port: 3332

role: SLAVE

state: UP

gtid_mode: ON

health: OK

version: 5.6.5–m8–debug–log

master_log_file: mysql–bin.000001

master_log_pos: 571

IO_Thread: Yes

SQL_Thread: Yes

Secs_Behind: 0

Remaining_Delay: No

IO_Error_Num: 0

IO_Error:

*************************3. row *************************

host: localhost

port: 3333

role: SLAVE

state: UP

gtid_mode: ON

health: OK

version: 5.6.5–m8–debug–log

master_log_file: mysql–bin.000001

master_log_pos: 571

IO_Thread: Yes

SQL_Thread: Yes

Secs_Behind: 0

Remaining_Delay: No

IO_Error_Num: 0

IO_Error:

*************************4. row *************************

host: localhost

port: 3334

role: SLAVE

state: UP

gtid_mode: ON

health: OK

version: 5.6.5–m8–debug–log

master_log_file: mysql–bin.000001

master_log_pos: 571

IO_Thread: Yes

SQL_Thread: Yes

Secs_Behind: 0

Remaining_Delay: No

IO_Error_Num: 0

IO_Error:

4 rows.

# …done.

运行上面相同的故障转移命令,但是指定日志文件。如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

shell> mysqlrpladmin\

—slaves=root@localhost:3332,root@localhost:3333,root@localhost:3334 \

—candidates=root@localhost:3333,root@localhost:3334 \

—log=test_log.txtfailover

# Performing failover.

# Candidate slave localhost:3333 will become the new master.

# Preparing candidate for failover.

# Creating replication user if it does not exist.

# Stopping slaves.

# Performing STOP on all slaves.

# Switching slaves to new master.

# Starting slaves.

# Performing START on all slaves.

# Checking slaves for errors.

# Failover complete.

# …done.

执行完后,日志文件包含下面的信息:

1

2

3

4

5

6

7

8

9

10

11

12

13

2012–03–19 14:44:17 PMINFOExecutingfailovercommand...

2012–03–19 14:44:17 PMINFOPerformingfailover.

2012–03–19 14:44:17 PMINFOCandidateslavelocalhost:3333 willbecomethenew master.

2012–03–19 14:44:17 PMINFOPreparingcandidatefor failover.

2012–03–19 14:44:19 PMINFOCreatingreplicationuserif itdoesnot exist.

2012–03–19 14:44:19 PMINFOStoppingslaves.

2012–03–19 14:44:19 PMINFOPerformingSTOPonallslaves.

2012–03–19 14:44:19 PMINFOSwitchingslavesto new master.

2012–03–19 14:44:20 PMINFOStartingslaves.

2012–03–19 14:44:20 PMINFOPerformingSTARTonallslaves.

2012–03–19 14:44:20 PMINFOCheckingslavesfor errors.

2012–03–19 14:44:21 PMINFOFailovercomplete.

2012–03–19 14:44:21 PMINFO ...done.

执行切换和降级当前的主为从。如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

shell> mysqlrpladmin —master=root@localhost:3331 \

—slaves=root@localhost:3332,root@localhost:3333,root@localhost:3334 \

—new–master=root@localhost:3332 —demote–masterswitchover

# Performing switchover from master at localhost:3331 to slave at localhost:3332.

# Checking candidate slave prerequisites.

# 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.

# …done.

如果按照上面的命令,新的拓扑复制健康报告如下:

1

2

3

4

5

6

7

8

9

# Replication Topology Health:

+——————+———–+————–+————+——————+————–+

| host| port| role| state| gtid_mode| health|

+——————+———–+————–+————+——————+————–+

| localhost| 3332| MASTER| UP| ON| OK|

| localhost| 3331| SLAVE| UP| ON| OK|

| localhost| 3333| SLAVE| UP| ON| OK|

| localhost| 3334| SLAVE| UP| ON| OK|

+——————+———–+————–+————+——————+————–+

可以使用发现从功能,当且仅当所有从向主报告他们各自的主机和端口。使用发现生成的复制健康报告命令如下。注意–discover-slaves-login不能如–slaves结合使用。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

shell> mysqlrpladmin —master=root@localhost:3332 —discover–slaves–login=roothealth

# Discovering slaves for master at localhost:3332

# Discovering slave at localhost:3331

# Found slave: localhost:3331

# Discovering slave at localhost:3333

# Found slave: localhost:3333

# Discovering slave at localhost:3334

# Found slave: localhost:3334

# Checking privileges.

#

# Replication Topology Health:

+——————+———–+————–+————+——————+————–+

| host| port| role| state| gtid_mode| health|

+——————+———–+————–+————+——————+————–+

| localhost| 3332| MASTER| UP| ON| OK|

| localhost| 3331| SLAVE| UP| ON| OK|

| localhost| 3333| SLAVE| UP| ON| OK|

| localhost| 3334| SLAVE| UP| ON| OK|

+——————+———–+————–+————+——————+————–+

# …done.

权限

主上的用户需要有对mysql数据库的SELECT 和 INSERT权限,以及REPLICATION SLAVE, REPLICATION CLIENT 和 GRANT OPTION权限。

从上的用户需要有SUPER 权限。

–rpl-user选项指定的用户,要么自动创建要么以存在,需要有REPLICATION SLAVE权限。

要运行mysqlrpladmin 的health命令,对主上的用户需要有额外的SUPER 权限。

对于切换命令,所有的用户需要有SUPER, GRANT OPTION, SELECT, RELOAD, DROP, CREATE 和 REPLICATION SLAVE 权限。

文章转载来自:ttlsa.com

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐