Understanding and Testing the MHA masterha_secondary_check Script for MySQL Network Fault Tolerance
This article explains the purpose, usage, and return codes of the MHA masterha_secondary_check Perl script, demonstrates how to simulate network failures with iptables, and presents a series of tests that verify the script’s ability to detect master reachability and handle various error conditions.
The author, a DBA from 爱可生交付服务部, introduces the masterha_secondary_check script used by MHA (MySQL High Availability) to check the reachability of a MySQL master from monitoring servers. The section begins with a brief description of the script’s role and then shows the full Perl source of /usr/bin/masterha_secondary_check :
#!/usr/bin/env perl
$| = 1;
GetOptions(
'help' => \$help,
'version' => \$version,
'secondary_host=s' => \@monitoring_servers,
'user=s' => \$ssh_user,
'port=s' => \$ssh_port,
'options=s' => \$ssh_options,
'master_host=s' => \$master_host,
'master_ip=s' => \$master_ip,
'master_port=i' => \$master_port,
'master_user=s' => \$master_user,
'master_password=s' => \$master_password,
'ping_type=s' => \$ping_type,
'timeout=i' => \$timeout,
);
... (rest of script omitted for brevity) ...
exit $exit_code;The script returns four possible exit codes, documented in the article:
# 0: master is not reachable from all monitoring servers
# 1: unknown errors
# 2: at least one monitoring server is not reachable from this script
# 3: master is reachable from at least one monitoring serverTo illustrate the behavior, the author simulates network faults using iptables and runs a series of eight tests. Each test shows the command used (e.g., masterha_secondary_check -s remote_host1 -s remote_host2 --user=root --master_host=master_ip --master_port=3306 --master_user=mha --master_password=*** --ping_type=SELECT ) and the resulting output, such as “Master is reachable from remote_host1!” or error messages indicating unreachable monitoring servers.
The tests demonstrate that the script can correctly distinguish between reachable and unreachable masters, handle unreachable monitoring hosts, and report unknown errors (exit code 1). A note at the end mentions a priority issue: when multiple monitoring hosts are provided, the script prefers the first one (remote_host1) unless it cannot reach the master.
In conclusion, the author finds that MHA’s secondary‑check script provides a degree of network‑fault tolerance, though documentation is sparse; continuous validation is recommended to ensure reliable operation.
Aikesheng Open Source Community
The Aikesheng Open Source Community provides stable, enterprise‑grade MySQL open‑source tools and services, releases a premium open‑source component each year (1024), and continuously operates and maintains them.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.