作为前提条件,应先配置MySQL复制,并设置SSH公钥免密码登录。MySQL5.5 引入了一种半同步复制功能,该功能可以确保主服务器和访问链中至少一台从服务器之间的数据一致性和冗余。
MHA由Node和Manager组成,Node运行在每一台MySQL服务器上,也就是说,不管是MySQL主服务器,还是MySQL从服务器,都要安装Node,而Manager通常运行在独立的服务器上,但如果硬件资源吃紧,也可以用一台MySQL从服务器来兼职Manager的角色。
安装Node:
shell> yum install perl-DBD-MySQL
shell> rpm -Uvh http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.52-0.noarch.rpm
安装Manager:
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> cat /etc/masterha_default.cnf
[server default]
user=...
password=...
ssh_user=...
配置应用设置:
shell> cat /etc/masterha_application.cnf
[server_1]
hostname=...
[server_2]
hostname=...
检查
检查MySQL复制:
shell> masterha_check_repl --conf=/etc/masterha_application.cnf
检查SSH公钥免密码登录:
shell> masterha_check_ssh --conf=/etc/masterha_application.cnf
实战
首先启动MHA进程:
shell> masterha_manager --conf=/etc/masterha_application.cnf
然后检查MHA状态:
shell> masterha_check_status --conf=/etc/masterha_application.cnf
注:如果正常,会显示『PING_OK』,否则会显示『NOT_RUNNING』。
到此为止,一个基本的MHA例子就能正常运转了,不过一旦当前的MySQL主服务器发生故障,MHA把某台MySQL从服务器提升为新的MySQL主服务器后,如何通知应用呢?这就需要在配置文件里加上如下两个参数:
说到Failover,通常有两种方式:一种是虚拟IP地址,一种是全局配置文件。