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、健康状态。该命令需要下面的组合之一:
主的健康状态基于如果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场景。该命令需要下面的组合之一:
判定主失效的依据是:如果连接到主丢失,等待–ping秒数并再次检查。如果主连接丢失和主无法ping通或重新连接,故障转移事件发生。
对于需要指定多个服务器的命令,参数需要以逗号分隔。形式如下:
1 2 | < * user* > [ : < * passwd* > ] @ < * host* > [ : < * port* > ] [ : < * socket* > ] or < * login – path* > [ : < * port* > ] [ : < * socket* > ] |
该工具允许用户连接到主自动发现从。为了使用自动发现从功能,所有从必需使用–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 | MySQL Utilities mysqlrpladmin version 1.5.3 License type : GPLv2 Usage : mysqlrpladmin — slaves = root @ trustauth.cn : 3306 < command > mysqlrpladmin – administration utility for MySQL replication Options : — version show program ‘s version number and exit –help display this help message and exit –license display program’ s license and exit — candidates = CANDIDATES connection information for candidate slave servers for failover in the form : < user > [ : < password > ] @ < host > [ : < port > ] [ : < socket > ] or < login – path > [ : < port > ] [ : < socket > ] or < config – path > [ < [ group ] > ] Valid only with failover command . List multiple slaves in comma – separated list . — discover – slaves – login = DISCOVER at startup , query master for all registered slaves and use the user name and password specified to connect . Supply the user and password in the form < user > [ : < password > ] or < login – path > . For example , — discover – slaves – login = joe : secret will use ‘joe’ as the user and ‘secret’ as the password for each discovered slave . — exec – after = EXEC_AFTER name of script to execute after failover or switchover — exec – before = EXEC_BEFORE name of script to execute before failover or switchover — log = LOG_FILE specify a log file to use for logging messages — log – age = LOG_AGE specify maximum age of log entries in days . Entries older than this will be purged on startup . Default = 7 days . — master = MASTER connection information for master server in the form : < user > [ : < password > ] @ < host > [ : < port > ] [ : < socket > ] or < login – path > [ : < port > ] [ : < socket > ] or < config – path > [ < [ group ] > ] . — max – position = MAX_POSITION used to detect slave delay . The maximum difference between the master ‘s log position and the slave’ s reported read position of the master . A value greater than this means the slave is too far behind the master . Default is 0. — ping = PING Number of ping attempts for detecting downed server . — seconds – behind = MAX_DELAY used to detect slave delay . The maximum number of seconds behind the master permitted before slave is considered behind the master . Default is 0. — slaves = SLAVES connection information for slave servers in the form : < user > [ : < password > ] @ < host > [ : < port > ] [ : < socket > ] or < login – path > [ : < port > ] [ : < socket > ] or < config – path > [ < [ group ] > ] . List multiple slaves in comma – separated list . — timeout = TIMEOUT maximum timeout in seconds to wait for each replication command to complete . For example , timeout for slave waiting to catch up to master . Default = 300. — script – threshold = SCRIPT_THRESHOLD Value for external scripts to trigger aborting the operation if result is greater than or equal to the threshold . Default = None ( no threshold checking ) . — new – master = NEW_MASTER connection information for the slave to be used to replace the master for switchover , in the form : < user > [ : < password > ] @ < host > [ : < port > ] [ : < socket > ] or < login – path > [ : < port > ] [ : < socket > ] or < config – path > [ < [ group ] > ] . Valid only with switchover command . — force ignore prerequisite check results or some inconsistencies found ( e . g . errant transactions on slaves ) and execute action – f FORMAT , — format = FORMAT display the output in either grid ( default ) , tab , csv , or vertical format — demote – master make master a slave after switchover . — no – health turn off health report after switchover or failover . – v , — verbose control how much information is displayed . e . g . , – v = verbose , – vv = more verbose , – vvv = debug – q , — quiet turn off all messages for quiet execution . — rpl – user = RPL_USER the user and password for the replication user requirement , in the form : < user > [ : < password > ] or < login – path > . E . g . rpl : passwd — ssl – ca = SSL_CA The path to a file that contains a list of trusted SSL CAs . — ssl – cert = SSL_CERT The name of the SSL certificate file to use for establishing a secure connection . — ssl – key = SSL_KEY The name of the SSL key file to use for establishing a secure connection . Available Commands : elect – perform best slave election and report best slave failover – conduct failover from master to best slave gtid – show status of global transaction id variables also displays uuids for all servers health – display the replication health reset – stop and reset all slaves start – start all slaves stop – stop all slaves switchover – perform slave promotion Note : elect , gtid and health require — master and either — slaves or — discover – slaves – login ; failover requires — slaves ; switchover requires — master , — new – master and either — slaves or — discover – slaves – login ; start , stop and reset require — slaves ( and — master is 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 @ trustauth.cn : 3331 \ — slaves = root @ trustauth.cn : 3332 , root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 elect # Electing candidate slave from known slaves. # Best slave found is located on trustauth.cn:3332. # …done. |
提供候选从名单执行最佳从选举,如下:
1 2 3 4 5 6 | shell > mysqlrpladmin — master = root @ trustauth.cn : 3331 \ — slaves = root @ trustauth.cn : 3332 , root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 \ — candidates = root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 elect # Electing candidate slave from candidate list then slaves list. # Best slave found is located on trustauth.cn:3332. # …done. |
主失败执行故障切换,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | shell > mysqlrpladmin \ — slaves = root @ trustauth.cn : 3332 , root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 \ — candidates = root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 failover # Performing failover. # Candidate slave trustauth.cn: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 @ trustauth.cn : 3333 \ — slaves = root @ trustauth.cn : 3332 , root @ trustauth.cn : 3334 health # Getting health for master: trustauth.cn:3333. # # Replication Topology Health: + — — — — — — + — — — – + — — — — – + — — — — + — — — — — — + — — — — – + | host | port | role | state | gtid_mode | health | + — — — — — — + — — — – + — — — — – + — — — — + — — — — — — + — — — — – + | trustauth.cn | 3333 | MASTER | UP | ON | OK | | trustauth.cn | 3332 | SLAVE | UP | ON | OK | | trustauth.cn | 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 @ trustauth.cn : 3331 \ — slaves = root @ trustauth.cn : 3332 , root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 \ — verbose health # Getting health for master: trustauth.cn:3331. # Attempting to contact trustauth.cn … Success # Attempting to contact trustauth.cn … Success # Attempting to contact trustauth.cn … Success # Attempting to contact trustauth.cn … Success # # Replication Topology Health: * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * host : trustauth.cn 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 : trustauth.cn 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 : trustauth.cn 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 : trustauth.cn 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 @ trustauth.cn : 3332 , root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 \ — candidates = root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 \ — log = test_log . txt failover # Performing failover. # Candidate slave trustauth.cn: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 PM INFO Executing failover command . . . 2012 – 03 – 19 14 : 44 : 17 PM INFO Performing failover . 2012 – 03 – 19 14 : 44 : 17 PM INFO Candidate slave trustauth.cn : 3333 will become the new master . 2012 – 03 – 19 14 : 44 : 17 PM INFO Preparing candidate for failover . 2012 – 03 – 19 14 : 44 : 19 PM INFO Creating replication user if it does not exist . 2012 – 03 – 19 14 : 44 : 19 PM INFO Stopping slaves . 2012 – 03 – 19 14 : 44 : 19 PM INFO Performing STOP on all slaves . 2012 – 03 – 19 14 : 44 : 19 PM INFO Switching slaves to new master . 2012 – 03 – 19 14 : 44 : 20 PM INFO Starting slaves . 2012 – 03 – 19 14 : 44 : 20 PM INFO Performing START on all slaves . 2012 – 03 – 19 14 : 44 : 20 PM INFO Checking slaves for errors . 2012 – 03 – 19 14 : 44 : 21 PM INFO Failover complete . 2012 – 03 – 19 14 : 44 : 21 PM INFO . . . done . |
执行切换和降级当前的主为从。如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | shell > mysqlrpladmin — master = root @ trustauth.cn : 3331 \ — slaves = root @ trustauth.cn : 3332 , root @ trustauth.cn : 3333 , root @ trustauth.cn : 3334 \ — new – master = root @ trustauth.cn : 3332 — demote – master switchover # Performing switchover from master at trustauth.cn:3331 to slave at trustauth.cn: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 | + — — — — — — + — — — – + — — — — – + — — — — + — — — — — — + — — — — – + | trustauth.cn | 3332 | MASTER | UP | ON | OK | | trustauth.cn | 3331 | SLAVE | UP | ON | OK | | trustauth.cn | 3333 | SLAVE | UP | ON | OK | | trustauth.cn | 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 @ trustauth.cn : 3332 — discover – slaves – login = root health # Discovering slaves for master at trustauth.cn:3332 # Discovering slave at trustauth.cn:3331 # Found slave: trustauth.cn:3331 # Discovering slave at trustauth.cn:3333 # Found slave: trustauth.cn:3333 # Discovering slave at trustauth.cn:3334 # Found slave: trustauth.cn:3334 # Checking privileges. # # Replication Topology Health: + — — — — — — + — — — – + — — — — – + — — — — + — — — — — — + — — — — – + | host | port | role | state | gtid_mode | health | + — — — — — — + — — — – + — — — — – + — — — — + — — — — — — + — — — — – + | trustauth.cn | 3332 | MASTER | UP | ON | OK | | trustauth.cn | 3331 | SLAVE | UP | ON | OK | | trustauth.cn | 3333 | SLAVE | UP | ON | OK | | trustauth.cn | 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 权限。
文章转载来自:trustauth.cn