实现MySQL高可用的MHA安装及配置
MHA项目:http://code.google.com/p/mysql-master-ha/wiki/TableOfContents?tm=6
Perl的相关模块下载:http://search.cpan.org/
一、安装masterha-node
客户端——4台MySQL服务器、服务端——2台Lvs服务器均需安装masterha-node结点
1 安装相关的perl模块
1.1 DBI
[root@MySQL-15.11 xxm 18:26:04]#tar xf DBI-1.633.tar.gz
[root@MySQL-15.11 xxm 18:26:19]#cd DBI-1.633
[root@MySQL-15.11 DBI-1.633 18:26:22]#perl Makefile.PL
[root@MySQL-15.11 DBI-1.633 18:27:07]#make
[root@MySQL-15.11 DBI-1.633 18:27:42]#make install
[root@MySQL-15.11 xxm 18:32:22]#perl -e "use DBI" ->无返回值,说明正确安装
1.2 DBD-mysql
[root@MySQL-15.11 xxm 18:34:53]#tar xf DBD-mysql-4.029.tar.gz
[root@MySQL-15.11 DBD-mysql-4.029 18:35:10]#perl Makefile.PL
[root@MySQL-15.11 DBD-mysql-4.029 18:35:40]#make
[root@MySQL-15.11 DBD-mysql-4.029 18:35:56]#make install
[root@MySQL-15.11 DBD-mysql-4.029 18:36:15]#perl -e "use DBD::mysql"
2 安装masterha-node
[root@MySQL-15.11 xxm 19:15:04]#tar xf mha4mysql-node-0.56.tar.gz
[root@MySQL-15.11 xxm 19:15:34]#cd mha4mysql-node-0.56
[root@MySQL-15.11 mha4mysql-node-0.56 19:15:37]#perl Makefile.PL
[root@MySQL-15.11 mha4mysql-node-0.56 19:16:04]#make
[root@MySQL-15.11 mha4mysql-node-0.56 19:17:18]#make install
二、安装masterha-manager
仅在服务端——2台Lvs服务器上安装
1 安装相关的perl模块
1.1 Config::Tiny
[root@Lvs-15.23 xxm 19:48:57]#gunzip Config-Tiny-2.20.tgz
[root@Lvs-15.23 xxm 19:49:01]#tar xf Config-Tiny-2.20.tar
[root@Lvs-15.23 xxm 19:49:05]#cd Config-Tiny-2.20
[root@Lvs-15.23 Config-Tiny-2.20 19:49:48]#perl Makefile.PL
1.2 Log::Dispatch
这个模块单独编译安装的话,所需要的依赖包太多,还是使用CPAN方式简易些。使用CPAN方式,需网络正常。下列方式,一路yes即可。
[root@Lvs-15.23 xxm 20:09:36]# perl -MCPAN -e "install Log::Dispatch"
1.3 Parallel::ForkManager
[root@Lvs-15.23 xxm 20:58:33]#perl -MCPAN -e "install Parallel::ForkManager"
2 安装masterha-manager
[root@Lvs-15.23 xxm 21:07:56]#tar xf mha4mysql-manager-0.56.tar.gz
[root@Lvs-15.23 xxm 21:08:15]#cd mha4mysql-manager-0.56
[root@Lvs-15.23 mha4mysql-manager-0.56 21:08:17]#perl Makefile.PL
[root@Lvs-15.23 mha4mysql-manager-0.56 21:08:21]#make
[root@Lvs-15.23 mha4mysql-manager-0.56 21:08:40]#make install
三、配置及测试
1 masterha-manager的配置文件
1.1 在给MHA创建配置文件
cat << EOF >> /etc/app1.cnf
[server default]
#mysql user and password
user=mha_manager
password=123456
port=36677
#replication user and password
repl_user=repl
repl_password=123456
#ssh user and port
ssh_user=root
ssh_port=2777
#binlog directory
master_binlog_dir=/opt/data/log
#working directory on the manager
manager_workdir=/var/log/masterha/app1
#manager log file
manager_log=/var/log/masterha/app1/app1.log
#working directory on MySQL servers
remote_workdir=/var/log/masterha/app1
#If MySQL command line utilities are installed under a non-standard directory, use this option to set the directory.
client_bindir=/usr/local/mysql/bin
#If MySQL libraries are installed under a non-standard directory, use this option to set the directory.
client_libdir=/usr/local/mysql/lib
#To check by connecting/disconnecting every time, because it's more strict and it can detect TCP connection level failure more quickly.
ping_type=CONNECT
#To fully control the order of priority (i.e. host2->host3->host4..)
latest_priority=0
# IP address failover solution
master_ip_failover_script=/usr/local/scripts/master_ip_failover
#Send a report (i.e. e-mail) when failover has completed or ended with errors
report_script=/usr/local/scripts/send_report
[server-Master1]
hostname=192.168.15.11
candidate_master=1
[server-Master2]
hostname=192.168.15.12
candidate_master=1
[server-Slave1]
hostname=192.168.15.13
[server-Slave2]
hostname=192.168.15.14
EOF
1.2 master_ip_failover脚本
cat /usr/local/scripts/master_ip_failover
#!/bin/bash
#--------------第一部分:变量及函数定义-----------------#
###Begin Variables define###
ssh_port=2777
cmd=/sbin/ifconfig
vip=192.168.15.31
device=eth0:0
netmk=255.255.255.0
start_vip="${cmd} ${device} ${vip} netmask ${netmk} up"
stop_vip="${cmd} ${device} ${vip} netmask ${netmk} down"
###End Variables define###
###Begin Status Funciont###
status()
{
exit 0
}
###End Status Funciont###
###Begin Stop Or Stopssh Funciont###
stop()
{
exit 0
}
###End Stop Or Stopssh Funciont###
###Begin Start Funciont###
start()
{
/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${orig_master_host} ""${stop_vip}""
/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${new_master_host} ""${start_vip}""
/usr/bin/ssh -p ${ssh_port} ${ssh_user}@${new_master_host} "/etc/init.d/lvsrs stop"
exit 0
}
###End Start Funciont###
#--------------第一部分:变量及函数定义-----------------#
#--------------第二部分:命令行参数-----------------#
###Begin Get The Command-Line Parameters###
###eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,orig_master_port:,new_master_host:,new_master_ip:,new_master_port:,new_master_user:,new_master_password: -- "$@"`"
eval set -- "`getopt -a -q -o n -l command::,ssh_user:,orig_master_host:,orig_master_ip:,new_master_host:,new_master_ip: -- "$@"`"
if [ $? != 0 ] ; then echo "Terminating..." >&2 ;exit 1;fi
while true
do
case "$1" in
--command)
command="${2}";
shift;;
--ssh_user)
ssh_user="${2}";
shift;;
--orig_master_host)
orig_master_host="${2}";
shift;;
--orig_master_ip)
orig_master_ip="${2}";
shift;;
--new_master_host)
new_master_host="${2}";
shift;;
--new_master_ip)
new_master_ip="${2}";
shift;;
--)
shift;
break;;
esac
shift
done
###End Get The Command-Line Parameters###
#--------------第二部分:命令行参数-----------------#
#--------------第三部分:函数调用-----------------#
if [ "${command}" == "status" ];
then
status;
fi
if [ "${command}" == "stop" ] || [ "${command}" == "stopssh" ] ;
then
stop;
fi