This is our second blog in the ProxySQL Series ( Blog I MySQL Replication Read-write Split up ). Will cover how to integrate ProxySQL with MHA to handle failover of Database servers.
We already have Master – Slave replication setup behind ProxySQL from previous blog [ProxySQL On MySQL Replication]
For this setup we have added one more node for MHA Manager , Which will keep eye on Master and Slave status.
- node5 (172.17.0.5) , MHA Manager
ProxySQL can be greatly configured with MHA for Highly available setup with zero downtime.
MHA role in failover :
MHA tool is used for failover.During failover, MHA promotes most updated slave (slave with most recent transactions) as new master and apply CHANGE MASTER
command on new slave and change read_only
flag on new master and slave.
ProxySQL role in failover :
When failover happened (due to crash or manual for any maintenance activity) ProxySQL will detect the change (checking read_only flag) and promotes new master server’s IP into writers hostgroup and start sending traffic on new master.
Each row in mysql_replication_hostgroups table in proxysql represent a pair of writer_hostgroup and reader_hostgroup .
ProxySQL will monitor the value of read_only
from mysql_server_read_only_log
for all the servers.
If read_only=1 the host is copied/moved to the reader_hostgroup, while if read_only=0 the host is copied/moved to the writer_hostgroup .
Installing MHA
If replication is classic binlog/pos format based then install MHA node on all hosts involving (manager,master,slaves), for GTID based replication it has to be installed only on the manager node.
Install MHA node :
Install MHA on all DB nodes and MHA manager server. More information
Install MHA manager :
Only install on MHA manager server.
Configuration changes :
Changes only on node5 (172.17.0.5) , MHA Manager
:
Create directories :
Config file :
master_ip_failover : Script used to switch virtual IP address.
master_ip_online_change : Script used in switchover when master is online or dead.
NOTE: Don’t forget to comment out the “FIX ME” lines in the above scripts.
Custom scripts : Below scripts are optional
secondary_check_script
It is always good to double check the availability of master. More info
report_script
: With this script we can configure alerts or email when failover completes. In Detail
Now run test against the cluster using below two scripts :
– masterha_check_repl
– masterha_check_ssh
Please note If this check fails then MHA will refuse to run any kind of failover.
To run an Manual failover :
- To Execute manual failover , Make sure your MHA manager is stooped.
new_master_host
– Its optional parameter if you want to select new master. If we don’s specify any value then most updated slave considered as new Master.
Automatic failover :
We need to run masterha_manager
is background to monitor cluster status :
When Auto failover happen , In case of Master Crash , Logs look like
we can also check the status of masterha_manager
:
Remember masterha_manager
script stops working in two situation :
- After automatic fail-over , for that we need to manually start above script to continue monitoring clusters nodes
- If any replication is not running on any slaves defined in cluster or any slave server is down.
Check backend status at ProxySQL :
Below table of ProxySQL shows , what is the current master and its slaves after failover with their ONLINE status.
Preserve relay logs and purge regularly :
As we have two slaves in this setup , MHA is keeping relay logs for recovering other slaves. [To ensure that it disables relay_log_purge
]
We need to periodically purges old relay logs like binary logs. MHA Node has a command line tool purge_relay_logs
to do that
purge_relay_logs removes relay logs without blocking SQL threads. Relay logs need to be purged regularly (i.e. once per day, once per 6 hours, etc), so purge_relay_logs should be regularly invoked on each slave server at different time. It can be scheduled as a cron too.
[root@mysql-slave1]$
Above script will purge relay logs and set relay_log_purge = 0 [OFF] to avoid automatic relay purge .
More Details : https://github.com/yoshinorim/mha4mysql-manager/wiki/Requirements
We can also have MySQL Utilities to perform the failover in ProxySQL too.The main advantage of using MHA-ProxySQL integration is, it avoids need for VIP or re-defining DNS after MHA failover , they are taken care by ProxySQL.