一、总概:
本文介绍了MySQL高可用性的实现方案MHA,MHA由Node和Manager组成,Node运行在每一台MySQL服务器上,不管是MySQL主服务器,还是MySQL从服务器,都要安装Node。主从安装keepalived,实现虚拟ip漂移,程序不用改IP自动切换。
二、环境
1. 软件版本
操作系统版本:CentOS release 6.4
MySQL数据库版本:5.6.14
MHA版本:mha4mysql-node-0.54-0.el6.noarch.rpm、mha4mysql-manager-0.55-0.el6.noarch.rpm
keepalived版本:1.2.7
2. 主机部署
MHA管理机、客户端模拟:192.168.1.100
master:192.168.1.1
slave:192.168.1.2
三、实验步骤
. 安装MySQL
. MySQL配置主重复制
. 建立ssh无密码登录环境
. 安装MHAmha4mysql-node,mha4mysql-manager 软件包
. 管理机manager上配置MHA文件
. masterha_check_ssh工具验证ssh信任登录是否成功
. masterha_check_repl工具验证mysql复制是否成功
. 启动MHA manager,并监控日志文件
. 测试master宕机后,是否会自动切换
. 测试使用MHA的master_ip_failover,实现故障转移
. 主从安装keepalived ,实现虚拟ip漂移
1. 安装MySQL
master的my.cnf
more /etc/my.cnf
[mysqld] basedir = /usr/local/mysql datadir = /usr/local/mysql/data sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_UNSIGNED_SUBTRACTION port = 6603 server_id = 1 lower_case_table_names = 1 character_set_server = utf8 autocommit = off innodb_flush_log_at_trx_commit = 1 skip_name_resolve = ON innodb_strict_mode = ON log_bin = mysql-bin innodb_data_home_dir = /usr/local/mysql/data innodb_log_group_home_dir = /usr/local/mysql/data binlog_format=mixed
slave的my.cnf
more /etc/my.cnf
[mysqld] basedir = /usr/local/mysql datadir = /usr/local/mysql/data sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_UNSIGNED_SUBTRACTION port = 6603 server_id = 2 lower_case_table_names = 1 character_set_server = utf8 autocommit = off innodb_flush_log_at_trx_commit = 1 skip_name_resolve = ON innodb_strict_mode = ON log_bin = mysql-bin innodb_data_home_dir = /usr/local/mysql/data innodb_log_group_home_dir = /usr/local/mysql/data binlog_format=mixed read_only = ON
MySQL安装步骤略
2. MySQL配置主重复制(略)
3. 建立ssh无密码登录环境
在192.168.1.100、192.168.1.1、192.168.1.2上修改hostname:
vi /etc/hosts
192.168.1.100 manage 192.168.1.1 master 192.168.1.2 slave
# 在192.168.1.100: ssh-keygen -t rsa ssh-copy-id -i .ssh/id_rsa.pub [email protected] ssh-copy-id -i .ssh/id_rsa.pub [email protected] # 在192.168.1.1: ssh-keygen -t rsa ssh-copy-id -i .ssh/id_rsa.pub [email protected] ssh-copy-id -i .ssh/id_rsa.pub [email protected] # 在192.168.1.2: ssh-keygen -t rsa ssh-copy-id -i .ssh/id_rsa.pub [email protected] ssh-copy-id -i .ssh/id_rsa.pub [email protected]
测试:
# 在192.168.1.100: ssh 192.168.1.1 ssh 192.168.1.2 # 在192.168.1.1: ssh 192.168.1.100 ssh 192.168.1.2 # 在192.168.1.2: ssh 192.168.1.100 ssh 192.168.1.1
4. 安装MHA mha4mysql-node,mha4mysql-manager 软件包
# 在192.168.1.100: rpm -ivh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm yum install perl-DBD-MySQL yum install perl-Config-Tiny yum install perl-Log-Dispatch yum install perl-Parallel-ForkManager rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm # 在192.168.1.1: rpm -ivh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm yum install perl-DBD-MySQL yum install perl-Config-Tiny yum install perl-Log-Dispatch yum install perl-Parallel-ForkManager rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm # 在192.168.1.2: rpm -ivh rpmforge-release-0.5.2-1.el6.rf.x86_64.rpm yum install perl-DBD-MySQL yum install perl-Config-Tiny yum install perl-Log-Dispatch yum install perl-Parallel-ForkManager rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm
5. 管理机manager上配置MHA文件
在192.168.1.100:
mkdir -p /masterha/app1
mkdir /etc/masterha
vi /etc/masterha/app1.cnf
[server default] user=root password=12345 manager_workdir=/masterha/app1 manager_log=/masterha/app1/manager.log remote_workdir=/masterha/app1 ssh_user=root repl_user=repl repl_password=12345 ping_interval=1 [server1] hostname=192.168.1.1 port=6603 master_binlog_dir=/usr/local/mysql/data candidate_master=1 [server2] hostname=192.168.1.2 port=6603 master_binlog_dir=/usr/local/mysql/data candidate_master=1
6. masterha_check_ssh工具验证ssh信任登录是否成功
在192.168.1.100:
masterha_check_ssh --conf=/etc/masterha/app1.cnf
7. masterha_check_repl工具验证mysql复制是否成功
在192.168.1.100:
masterha_check_repl --conf=/etc/masterha/app1.cnf
8. 启动MHA manager,并监控日志文件
在192.168.1.100:
nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log 2>&1
# 单开一个终端监控日志
tail -f tail -f /masterha/app1/manager.log
9. 测试master宕机后,是否会自动切换
在192.168.1.1:
service mysql stop
在192.168.1.2:
mysql> show slave status\G
mysql> show variables like 'read_only';
10. 测试使用MHA的master_ip_failover,实现故障转移
恢复主从配置(略)
在192.168.1.100:
在文件/etc/masterha/app1.cnf的[server default]段添加下面一行
master_ip_failover_script=/tmp/master_ip_failover
然后在/tmp/master_ip_failover写入如下代码:
#!/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); ?>
赋给这个文件可执行权限:
chmod +x /tmp/master_ip_failover
安装PHP:
yum install php
运行MHA进程:
nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log 2>&1
# 单开一个终端监控日志
tail -f tail -f /masterha/app1/manager.log
测试切换:
停止master上的mysql
在192.168.1.1:
service mysql stop
在192.168.1.2:
mysql> show slave status\G
mysql> show variables like 'read_only';
11. 主从安装keepalived,实现虚拟ip漂移
恢复主从配置(略)
vip:192.168.1.3/192.168.1.4
master:192.168.1.1
slave:192.168.1.2
在主从两个机器上安装keepalived(略)
配置keepalived
在192.168.1.1:
vim /etc/keepalived/keepalived.conf
global_defs { router_id MySQL-ha } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 90 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.3 192.168.1.4 } } virtual_server 192.168.1.3 6603 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.1.1 6603 { weight 1 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 6603 } } }
vim /usr/local/mysql/bin/mysql.sh
#!/bin/bash pkill keepalived
chmod +x /usr/local/mysql/bin/mysql.sh
# 先启动mysql,再启动keepalived
service mysql start
service keepalived start
在192.168.1.2:
vim /etc/keepalived/keepalived.conf
global_defs { router_id MySQL-ha } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 90 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.3 192.168.1.4 } } virtual_server 192.168.1.3 6603 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.1.2 6603 { weight 1 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 6603 } } }
vim /usr/local/mysql/bin/mysql.sh
#!/bin/bash pkill keepalived
chmod +x /usr/local/mysql/bin/mysql.sh
# 先启动mysql,再启动keepalived
service mysql start
service keepalived start
在192.168.1.100:
运行MHA进程:
nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log 2>&1
# 单开一个终端监控日志
tail -f tail -f /masterha/app1/manager.log
测试:
在192.168.1.1:
ip a
在192.168.1.2:
ip a
mysql> show slave status\G
在192.168.1.100:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'read_only'"
停master的mysql,再查看
在192.168.1.1:
service mysql stop
ip a
在192.168.1.2:
ip a
mysql> show slave status\G
在192.168.1.100:
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'server_id'"
mysql -u u1 -p12345 -P 6603 -h 192.168.1.3 -e "show variables like 'read_only'"
四、参考
http://os.51cto.com/art/201307/401702.htm
http://www.tuicool.com/articles/ZFfqua
http://www.tuicool.com/articles/ErU7nu
http://www.tuicool.com/articles/2ENRjaI
http://blog.itpub.net/14594028/viewspace-1073516/