MHA on Mac

本文将详细介绍在Mac上编译配置MHA的过程

MHA下载及编译

下载

shell> git clone https://github.com/yoshinorim/mha4mysql-node.git
shell> git clone https://github.com/yoshinorim/mha4mysql-manager.git

Perl依赖包安装

当然MHA对于Perl的模块依赖不只是Module::Install。但是只需要安装这一个单独模块后,其他模块都会在make时自动安装。

shell> sudo cpan Module::Install

其他依赖包安装

MHA依赖coreutils中提供的md5sum程序

shell> brew install coreutils

MHA编译安装

# node部分安装
shell> cd mha4mysql-node/  
shell> perl Makefile.PL
shell> sudo make && sudo make install
# manager部分安装
shell> cd mha4mysql-manager/  
shell> perl Makefile.PL
shell> sudo make && sudo make install

打开Mac的SSHD服务

MHA使用ssh在master和slave上执行指令。Mac OS默认不启用SSHD服务,需要手动开启。在System PreferrenceSharing中打开SSH配置,如下图: sshd

配置Mac下的SSHD PATH

MHA在进行主从切换时会需要通过ssh调用node节点上预先通过mha4mysql-node安装的代码。例如:ssh ssh_user@127.0.0.1 apply_diff_relay_logs。因此我们需要保证$PATH的目录中能够找到apply_diff_relay_logs这样的程序。而由于Mac下对于non-interactive的bash是不export PATH变量的。具体原因可以见 How do I set $PATH such that ssh user@host command works?

Tue Feb 24 17:54:39 2015 - [error][/Library/Perl/5.16/MHA/ManagerUtil.pm, ln122] Got error when getting node version. Error:
Tue Feb 24 17:54:39 2015 - [error][/Library/Perl/5.16/MHA/ManagerUtil.pm, ln123]
bash: apply_diff_relay_logs: command not found
Tue Feb 24 17:54:39 2015 - [error][/Library/Perl/5.16/MHA/ManagerUtil.pm, ln150] node version on 127.0.0.1 not found! Is MHA Node package installed ?
 at /Library/Perl/5.16/MHA/MasterMonitor.pm line 377.
Tue Feb 24 17:54:39 2015 - [error][/Library/Perl/5.16/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations. Died at /Library/Perl/5.16/MHA/ManagerUtil.pm line 151.
Tue Feb 24 17:54:39 2015 - [error][/Library/Perl/5.16/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.

解决这个问题的方法是执行以下命令:

shell> echo "PATH=$PATH:/usr/local/bin:/usr/local/Cellar/coreutils/8.23_1/libexec/gnubin" > ~/.ssh/environment

同时sudo vi /etc/sshd_configPermitUserEnvironment yes这一行解除注释。在依照上一小节重复一次关闭/打开SSHD的操作,使其生效。 下面是sshd_config的部分截取

PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
UsePrivilegeSeparation sandbox      # Default for new installations.
PermitUserEnvironment yes
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3

MySQL Sandbox安装及编译

MySQL Sandbox 是一个用Perl编写的快速搭建MySQL多实例的工具。下面我们会用他在Mac OS上搭建一个一主三从的主从架构。如果还没有安装过这个软件的用户可以简单的通过sudo perl -MCPAN -e "install MySQL::Sandbox"进行安装。

在Mac搭建一个一主三从的环境

完成以下命令后,在/data/mysql/rsandbox_mysql-5_6_23目录下就生成了一个一主三从的MySQL环境。

shell> mkdir -p /data/mysql
shell> wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.22-osx10.9-x86_64.tar.gz
shell> make_replication_sandbox --how_many_slaves=3 --sandbox_base_port=21288 --upper_directory=/data/mysql /data/mysql-5.6.23-osx10.9-x86_64.tar.gz
installing and starting master
installing slave 1
installing slave 2
installing slave 3
starting slave 1
.. sandbox server started
starting slave 2
... sandbox server started
starting slave 3
.. sandbox server started
initializing slave 1
initializing slave 2
initializing slave 3
replication directory installed in /data/mysql//rsandbox_mysql-5_6_23

为这个环境配置MHA

shell> sudo mkdir -p /etc/mha
shell> cat <<EOF >/tmp/app1.cnf
[server default]
user=msandbox
password=msandbox
ssh_user=michellezhou
repl_user=rsandbox
repl_password=rsandbox
client_bindir=/data/5.6.23/bin

[server1]
hostname=127.0.0.1
port=21288
master_binlog_dir=/data/mysql/rsandbox_mysql-5_6_23/master/data
client_bindir=/data/5.6.23/bin

[server2]
hostname=127.0.0.1
port=21289
master_binlog_dir=/data/mysql/rsandbox_mysql-5_6_23/node1/data
client_bindir=/data/5.6.23/bin

[server3]
hostname=127.0.0.1
port=21290
master_binlog_dir=/data/mysql/rsandbox_mysql-5_6_23/node2/data
client_bindir=/data/5.6.23/bin

[server4]
hostname=127.0.0.1
port=21291
master_binlog_dir=/data/mysql/rsandbox_mysql-5_6_23/node3/data
client_bindir=/data/5.6.23/bin
EOF
shell> sudo mv /tmp/app1.cnf /etc/mha

验证

如果之前的操作都正确的话,现在就可以启动MHA做一次有效性检查了

shell> masterha_check_repl --conf=/etc/mha/app1.cnf

如果配置正确会有大量日志信息输出,最后会有OK提示。截取部分输出如下:

Tue Feb 24 23:04:04 2015 - [info] Slaves settings check done.
Tue Feb 24 23:04:04 2015 - [info]
127.0.0.1(127.0.0.1:21288) (current master)
 +--127.0.0.1(127.0.0.1:21289)
 +--127.0.0.1(127.0.0.1:21290)
 +--127.0.0.1(127.0.0.1:21291)

Tue Feb 24 23:04:04 2015 - [info] Checking replication health on 127.0.0.1..
Tue Feb 24 23:04:04 2015 - [info]  ok.
Tue Feb 24 23:04:04 2015 - [info] Checking replication health on 127.0.0.1..
Tue Feb 24 23:04:04 2015 - [info]  ok.
Tue Feb 24 23:04:04 2015 - [info] Checking replication health on 127.0.0.1..
Tue Feb 24 23:04:04 2015 - [info]  ok.
Tue Feb 24 23:04:04 2015 - [warning] master_ip_failover_script is not defined.
Tue Feb 24 23:04:04 2015 - [warning] shutdown_script is not defined.
Tue Feb 24 23:04:04 2015 - [debug]  Disconnected from 127.0.0.1(127.0.0.1:21288)
Tue Feb 24 23:04:04 2015 - [debug]  Disconnected from 127.0.0.1(127.0.0.1:21289)
Tue Feb 24 23:04:04 2015 - [debug]  Disconnected from 127.0.0.1(127.0.0.1:21290)
Tue Feb 24 23:04:04 2015 - [debug]  Disconnected from 127.0.0.1(127.0.0.1:21291)
Tue Feb 24 23:04:04 2015 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

至此,我们已经顺利的在自己的Mac OS上搭建完了一个一主三从由MHA负责切换的实验环境了!用下面的命令进行一次主从切换吧!

shell> masterha_master_switch --master_state=alive --conf=/etc/mha/app1.cnf