需要四台虚拟机进行实验环境,其中一台负责manager,其余三台配置mysql主从复制且必须是一主二从。
具体要求:四台机器必须SSH免密登录,node端安装node相关包和依赖包,manager安装所有依赖包。
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
初始化mysql
rm -rf /data/mysql/*
mysqld --initialize-insecure --user=mysql
/etc/init.d/mysqld start
修改完成后进入mysql中
mysql> grant replication slave on *.* to repl@'%' identified by 'westos';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> alter user root@localhost identified by 'westos';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to root@'%' identified by 'westos';
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
server23 同样修改配置文件并且初始化并且
mysql> change master to master_host='172.25.76.1', master_user='repl', master_password='westos', master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
server4:
将相关安装包导入至 server4
[root@foundation76 images]# scp -r /root/Desktop/westospub/docs/mysql/MHA-7/ [email protected]:/root
并将node文件传到123
scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm server2/3/1:/root/
cd MHA-7
yum install *.rpm -y
mkdir /etc/masterha
cd /etc/masterha/
vim app1.conf
[server default]
manager_workdir=/etc/masterha
manager_log=/etc/masterha/mha.log
master_binlog_dir=/data/mysql
#master_ip_failover_script=/usr/bin/master_ip_failover
#master_ip_online_change_script= /usr/local/bin/master_ip_online_change
user=root
password=westos
ping_interval=3
remote_workdir=/tmp
repl_user=repl
repl_password=westos
secondary_check_script=/usr/bin/masterha_secondary_check -s 172.25.76.2 -s 172.25.76.3
ssh_user=root
[server1]
hostname=172.25.76.1
port=3306
[server2]
hostname=172.25.76.2
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=172.25.76.3
port=3306
no_master=1
配置四台机器互相免密登陆
master端
ssh-keygen
ssh-copy-id server1
ssh-copy-id server2
ssh-copy-id server3
ssh-copy-id server
scp -r .ssh/ server2:
scp -r .ssh/ server3:
scp -r .ssh/ server4:
保证四台机器之间互通
server4:
检测ssh免密登陆是否正常
masterha_check_ssh --conf=/etc/masterha/app1.conf #检测ssh
masterha_check_repl --conf=/etc/masterha/app1.conf #检测授权
若未授权,进server1主机msyql 给root用户远程登陆的权限
grant all on *.* to root@'%' identified by 'westos';
当server1(manager)处于alive模式时,在manager端进行
masterha_master_switch --conf=/etc/masterha/app1.conf --master_state=alive --new_master_host=172.25.76.2 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
进入mysql数据库show slave status\G;查看故障解决结果
当前master(server1)处于dead模式时
[root@server2 .ssh]# /etc/init.d/mysqld stop
masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.conf --dead_master_host=172.25.76.2 --dead_master_port=3306 --new_master_host=172.25.76.1 --new_master_port=3306 --ignore_last_failover
传入脚本
[root@foundation76 images]# scp -r /root/Desktop/westospub/docs/mysql/master_ip_online_change [email protected]:/root
root@172.25.76.4's password:
master_ip_online_change 100% 3813 677.3KB/s 00:00
[root@foundation76 images]# scp -r /root/Desktop/westospub/docs/mysql/master_ip_failover [email protected]:/root
[email protected]'s password:
master_ip_failover 100% 2156 252.5KB/s 00:00
server4:
chmod +x master_ip_*
vim master_ip_failover
vim master_ip_online_change
修改为自己的ip地址
小脚本,自动切:
masterha_manager --conf=/etc/masterha/app1.conf &
执行脚本读取文件 并打入后台,模拟master服务区down掉
将一开始注释的配置文件中的脚本选项打开
vim /etc/masterha/app1.cnf
[server default]
manager_workdir=/etc/masterha
manager_log=/etc/masterha/mha.log
master_binlog_dir=/data/mysql
master_ip_failover_script=/etc/masterha/master_ip_failover
master_ip_online_change_script=/etc/masterha/master_ip_online_change
user=root
password=westos
ping_interval=3
remote_workdir=/tmp
repl_user=repl
repl_password=westos
secondary_check_script=/usr/bin/masterha_secondary_check -s 172.25.76.2 -s 172.25.76.3
ssh_user=root
[server1]
hostname=172.25.76.1
port=3306
[server2]
hostname=172.25.76.2
port=3306
candidate_master=1
check_repl_delay=0
[server3]
hostname=172.25.76.3
port=3306
no_master=1
修改其中的VIP为自己的172.25.1.100
添加到server上 然后直接停掉server上的数据库
ip addr add 172.25.76.100/24 dev eth0
ip addr show
/etc/init.d/mysqld stop
然后可以在server4上看到刚后台执行的命令开始动作
注意这个时候刚才添加在server1上的ip172.25.76.100不在了,它飘到了server2上:
然后重新将server1启动
/etc/init.d/mysqld start
mysql -pwestos
mysql> change master to master_host='172.25.76.2', master_user='repl', master_password='westos',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
server4:
masterha_master_switch --conf=/etc/masterha/app1.conf --master_state=alive --new_master_host=172.25.76.1 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000