提到MySQL高可用性,不少人会想到MySQL Cluster,亦或者Heartbeat+DRBD,不过这些方案的复杂性经常让人望而却步,与之相对,利用MySQL复制实现高可用性则显得容易不少,目前大体有MMM,MHA等方案可供选择:MMM是最多见的方案,惋惜它问题太多(What’s wrong with MMM,Problems with MMM for MySQL);相比之下,MHA是个更好的选择,通过DeNA大规模的实践应用证实它是个靠谱的工具。php
做为前提条件,应先配置MySQL复制,并设置SSH公钥免密码登陆。下面以CentOS为例来讲明,最好先安装EPEL,否则YUM可能找不到某些软件包。node
MHA由Node和Manager组成,Node运行在每一台MySQL服务器上,也就是说,不论是MySQL主服务器,仍是MySQL从服务器,都要安装Node,而Manager一般运行在独立的服务器上,但若是硬件资源吃紧,也能够用一台MySQL从服务器来兼职Manager的角色。mysql
安装Node:linux
shell> yum install perl-DBD-MySQL shell> rpm -Uvh http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.52-0.noarch.rpm
安装Manager:sql
shell> yum install perl-DBD-MySQL shell> yum install perl-Config-Tiny shell> yum install perl-Log-Dispatch shell> yum install perl-Parallel-ForkManager shell> rpm -Uvh http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.52-0.noarch.rpm shell> rpm -Uvh http://mysql-master-ha.googlecode.com/files/mha4mysql-manager-0.52-0.noarch.rpm
配置全局设置:shell
shell> cat /etc/masterha_default.cnf [server default] user=... password=... ssh_user=...
配置应用设置:ubuntu
shell> cat /etc/masterha_application.cnf [server_1] hostname=... [server_2] hostname=...
注:MHA配置文件中参数的详细介绍请参考官方文档。服务器
检查MySQL复制:app
shell> masterha_check_repl --conf=/etc/masterha_application.cnf
检查SSH公钥免密码登陆:ssh
shell> masterha_check_ssh --conf=/etc/masterha_application.cnf
首先启动MHA进程:
shell> masterha_manager --conf=/etc/masterha_application.cnf
注:视配置状况而定,可能会提示read_only,relay_log_purge等警告信息。
而后检查MHA状态:
shell> masterha_check_status --conf=/etc/masterha_application.cnf
注:若是正常,会显示『PING_OK』,不然会显示『NOT_RUNNING』。
到此为止,一个基本的MHA例子就能正常运转了,不过一旦当前的MySQL主服务器发生故障,MHA把某台MySQL从服务器提高为新的MySQL主服务器后,如何通知应用呢?这就须要在配置文件里加上以下两个参数:
说到Failover,一般有两种方式:一种是虚拟IP地址,一种是全局配置文件。MHA并无限定使用哪种方式,而是让用户本身选择,虚拟IP地址的方式会牵扯到其它的软件,这里就不赘述了,如下简单说说全局配置文件,以PHP为实现语言,代码以下:
#!/usr/bin/env php <?php $longopts = array( 'command:', 'ssh_user:', 'orig_master_host:', 'orig_master_ip:', 'orig_master_port:', 'new_master_host::', 'new_master_ip::', 'new_master_port::', ); $options = getopt(null, $longopts); if ($options['command'] == 'start') { $params = array( 'ip' => $options['new_master_ip'], 'port' => $options['new_master_port'], ); $string = '<?php return ' . var_export($params, true) . '; ?>'; file_put_contents('config.php', $string, LOCK_EX); } exit(0); ?>
注:用其它语言实现这个脚本也是OK的,最后别忘了给脚本加上可执行属性。
若是要测试效果的话,能够kill掉当前的MySQL主服务器,稍等片刻,MHA就会把某台MySQL从服务器提高为新的MySQL主服务器,并调用master_ip_failover_script脚本,如上所示,咱们在master_ip_failover_script脚本里能够把新的MySQL主服务器的ip和port信息持久化到配置文件里,这样应用就可使用新的配置了。
有时候须要手动切换MySQL主服务器,http://www.gwdang.com可使用masterha_master_switch命令,不过它调用的不是master_ip_failover_script脚本,而是master_ip_online_change_script脚本,但调用参数相似,脚本能够互用。
shell> masterha_master_switch --conf=/etc/masterha_application.cnf --master_state=dead --dead_master_host=... shell> masterha_master_switch --conf=/etc/masterha_application.cnf --master_state=alive --new_master_host=...
注:针对原来的MySQL主服务器是否已经宕机,执行命令所需的参数有所不一样。
须要说明的是,缺省状况http://www.gwdang.com/app/extension下,若是MHA检测到连续发生宕机,且两次宕机时间间隔不足八小时的话,则不会进行Failover,之因此这样限制是为了不ping-pong效应。不过为了自动化,咱们每每但愿能取消这种限制,此时能够用以下方式启动Manager:
shell> nohup masterha_manager --conf=/etc/masterha_application.cnf --ignore_last_failover --remove_dead_master_conf &
注:请确保Manager的运行用户对masterha_application.cnf有写权限。
…
本文只是MHA的一个简要介绍,至于详细说明,建议你们阅读官方文档。