MHA优点:
1.MHA能够在短的时实现故障检测与故障转移
2.在10-30秒内,MHA能够很好的解决复制过程中数据不一致的问题
3.由于不需要在现有的replication中添加额外的服务器,仅需要一个manager节点,所以能大大节约服务器数量
4.安装简单,无性能损耗,以及不需要修改现有的复制部署
5.当Master出现故障时,它可以自动将最新数据的Slave提升为新的Master,然后将所有其他的Slave重新指向新的Master
MHA工作流程
1.将宕机的master二进制日志保存下来
2.找到binlog位置点最新的slave
3.在binlog位置最新的slave上用relay log 修复其他slave
4.将宕机的master上保存下来的二进制日志恢复到含有最新位置点的slave上
5.将含有最新位置点binlog所在的slave提升为master
6.将其他slave重新指向新提升的master,并开启主从复制
MHA框架图
MHA manager工具包主要工具
masterha_check_ssh #检查MHA的ssh-key
masterha_check_repl #检查主从复制情况
masterha_manger #启动MHA
masterha_check_status #检测MHA的运行状态
masterha_master_monitor #检测master是否宕机
masterha_master_switch #手动故障转移
masterha_conf_host #建立TCP连接从远程服务器
masterha_stop #停止MHA
Node工具包主要工具
save_binary_logs #保存宕机的master的binlog
apply_diff_relay_logs #识别relay log的差异
filter_mysqlbinlog #防止回滚事件
purge_relay_logs #清除中继日志
实验环境
角色 | 主机名 | IP地址 | 服务 |
Moniotor host | mha-manager | 200.200.2.10 | mha-manager.mha-node |
Master | mysql-db01 | 200.200.2.11 | mha-node |
Slave01 | mysql-db02 | 200.200.2.12 | mha-node |
Slave02 | mysql-db03 | 200.200.2.13 | mha-node |
环境准备
四台设备都需要操作
hostname 改名
systemctl stop firewalld
setenforce 0
yum -y install lrzsz
安装数据库
除mha-manager不需要操作剩下三台都需要 rz tar xf mysql-5.6.30-linux-glibc2.5-x86_64.tar.gz mv mysql-5.6.30-linux-glibc2.5-x86_64/ /usr/local/mysql 创建数据库用户 useradd -M -s /sbin/nologin mysql 安装依赖包(四台都需要安装) yum -y install autoconf 初始化数据user cd /usr/local/mysql/scripts/ ./mysql_install_db --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/ 复制配置文件 cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf 复制启动脚本 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld 配置环境变量 echo 'export PATH="/usr/local/mysql/bin:$PATH"' >> /etc/profile.d/mysql.sh source /etc/profile 启动MySQL服务 systemctl daemon-reload /etc/init.d/mysqld start 设置为开机自启 chkconfig --add mysqld chkconfig mysqld on 设置用户与密码 mysqladmin -uroot password 123456;history -c 修改数据库db01: vim /etc/my.con [mysqld] server_id=1 #主库server-id为1,从库不等于1; log_bin=mysql-bin #开启binlog日志; user=mysql basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock log_error=/usr/local/mysql/data/error.log 数据库02: vim /etc/my.cnf
[mysqld]
server_id =2 数据库03: vim /etc/my.cnf [mysqld]
server_id =3 三台设备重启服务: systemctl restart mysqld
主从备份对db01登陆数据库
mysql -uroot -p123456
对用户进行授权
grant replication slave on *.* to rep@'200.200.2.%' identified by '123456';
查看节点
show master status
对两台从库进行配置
登陆数据库
mysql -uroot -p123456
授权
change master to master_host='200.200.2.11',master_user='rep',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=120;
启动从库
start slave
查看从库状态
show slave statusG
注
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
必须全是Yes
禁止relay_log_purge自动清理功能
set global relay_log_purge=0
设置只读
set global read_only=1
退出数据库
设置禁用自动删除relay log永久生效
vim /etc/my.cmf
[mysqld]
server_id=10
log_bin=mysql-bin
relay_log_purge=0
重启服务
/etc/init.d/mysqld restart
部署MHA
为所有节点进行配置
vim /etc/hosts
200.200.2.10 mha-manager
200.200.2.11 mysql-db01
200.200.2.12 mysql-db02
200.200.2.13 mysql-db03
安装依赖包
yum -y install perl-DBD-MySQL
上传数据包
rpm -ivh perl-Config-Tiny-2.23-2.3.noarch.rpm
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
yum -y localinstall perl-*
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
在三台mysql数据库上部署
mysql -uroot -p123123
grant all privileges on *.* to mha@'192.168.2.%' identified by 'mha';
select user,host from mysql.user;
编辑配置文件
在MHA服务器上
mkdir -p /etc/mha
mkdir -p /var/log/mha/app1
mkdir -p /var/data/binlog
vim /etc/mha/app1.cnf
编辑mha配置文件
[server default]
#设置manager日志
manager_log=/var/log/mha/app1/manager.log
#设置manager工作目录
manager_workdir=/var/log/mha/app1
#设置master保存binlog的位置,以便MHA可以找到master日志
master_binlog_dir=/usr/local/mysql/data
#设置监控的用户
user=mha
#设置mha用户的密码
password=mha
#设置监控主库,发送ping包的时间间隔,尝试三次没有回应的时候自动进行failover
ping_interval=2
#设置复制用户的密码
repl_password=123456
#指定复制用户
repl_user=rep
#设置ssh登录名
ssh_user=root
[server1]
hostname=200.200.2.11
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=200.200.2.12
port=3306
[server3]
hostname=200.200.2.13
port=3306
在db02与db03上添加rep用户
mysql -uroot -p123456
grant replication slave on *.* to rep@'200.200.2.%' identified by '123456';
对所有节点配置ssh信任
创建密钥对
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa >/dev/null 2>&1
ssh-copy-id -i /root/.ssh/id_rsa.pub root@200.200.2.10
ssh-copy-id -i /root/.ssh/id_rsa.pub root@200.200.2.12
ssh-copy-id -i /root/.ssh/id_rsa.pub root@200.200.2.13
在ssh服务支持密钥认证
[root@mysql-db01 ~]# vim /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
重启ssh服务
systemctl restart sshd
my.cnf配置
[mysqld]
server_id=2
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log_error=/usr/local/mysql/data/error.log
log_bin=mysql-bin
relay_log_purge=0
skip-name-resolve
启动测试
测试ssh
masterha_check_ssh --conf=/etc/mha/app1.cnf
测试复制
masterha_check_repl --conf=/etc/mha/app1.cnf
启动MHA
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
masterha_check_status --conf=/etc/mha/app1.cnf
测试
准备工作
查看mysql-db2.mysql-db3的主从状态
测试
停掉主库
/etc/init.d/mysqld stop
此时登陆mysql-db2
发现slave状态为空
登陆mysql-db3
主库指向mysql-db2
配置VIP漂移
MHA脚本管理VIP
修改MHA配置文件
vim /etc/app1.cnf master_ip_failover_script=/usr/local/bin/master_ip_failover 在/usr/local/bin下创建脚本 vim /usr/local/bin/master_ip_failover #修改一下内容 my $vip = '200.200.2.199/24'; my $key = '0'; my $ssh_start_vip = "/sbin/ifconfig eth:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig eth:$key down"; [root@mha-manager ~]#chmod +x /usr/local/bin/master_ip_failover
脚本
#!/usr/bin/env perl # Copyright (C) 2011 DeNA Co.,Ltd. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## Note: This is a sample script and is not complete. Modify the script based on your environment. use strict; use warnings FATAL => 'all'; use Getopt::Long; use MHA::DBHelper; my ( $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 ); my $vip = '192.168.2.199/24'; my $key = '0'; my $ssh_start_vip = "/sbin/ifconfig eth:$key $vip"; my $ssh_stop_vip = "/sbin/ifconfig eth:$key down"; GetOptions( 'command=s' => $command, 'ssh_user=s' => $ssh_user, 'orig_master_host=s' => $orig_master_host, 'orig_master_ip=s' => $orig_master_ip, 'orig_master_port=i' => $orig_master_port, 'new_master_host=s' => $new_master_host, 'new_master_ip=s' => $new_master_ip, 'new_master_port=i' => $new_master_port, 'new_master_user=s' => $new_master_user, 'new_master_password=s' => $new_master_password, ); exit &main(); sub main { print " IN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip=== "; if ( $command eq "stop" || $command eq "stopssh" ) { my $exit_code = 1; eval { print "Disabling the VIP on old master: $orig_master_host "; &stop_vip(); $exit_code = 0; }; if ($@) { warn "Got Error: $@ "; exit $exit_code; } exit $exit_code; } elsif ( $command eq "start" ) { my $exit_code = 10; eval { print "Enabling the VIP - $vip on the new master - $new_master_host "; &start_vip(); $exit_code = 0; }; if ($@) { warn $@; exit $exit_code; } exit $exit_code; } elsif ( $command eq "status" ) { print "Checking the Status of the script.. OK "; exit 0; } else { &usage(); exit 1; } } sub start_vip() { `ssh $ssh_user@$new_master_host " $ssh_start_vip "`; } sub stop_vip() { return 0 unless ($ssh_user); `ssh $ssh_user@$orig_master_host " $ssh_stop_vip "`; } sub usage { print "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port "; }
在db01上手动绑定IP
ifconfig ens32:0 200.200.2.199
测试VIP
启动MHA
nohup masterha_manager --conf=/etc/mha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
宕掉db01
/etc/init.d/mysqld stop
此事vip漂移到db02
ip a | grep ens32
ens32:0漂移到db02上
配置binlog-server
修改mha配置文件
vim /etc/mha/app1.cnf
添加内容
[binlog]
no_master=1
hostname=200.200.2.10
master_binlog_dir=/var/data/binlog
安装5.7数据库调用命令
请参照主从复制的安装数据库步骤
备份binlog
MHA
cd /var/data/binlog/
mysqlbinlog -R --host=200.200.2.11 --user=mha --password=mha --raw --stop-never mysql-bin.000001 &
查看备份过来的日志
db-01
cd /var/data/binlog
ls
在mysql-slave01上刷新日志
mysql -uroot -p123456
flush logs
刷新完manager备份日志,新增一条日志
ls