前期准备:
mysql先部署好GTID主从,然后才部署MHA
1)环境准备(所有节点)
#安装依赖包
yum install perl-DBD-MySQL -y
#进入安装包存放目录
[root@mysql-db01 ~]# cd /root/tools
#上传mha安装包
[root@mysql-db01 tools]# rz -be
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-manager-0.56.tar.gz
mha4mysql-node-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56.tar.gz
#安装node包
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
#登录数据库
mysql -uroot -p
#添加mha管理账号
grant all privileges on *.* to mha@'192.168.1.%' identified by 'mha';
flush privileges;
#查看是否添加成功
select user,host from mysql.user;
2)命令软连接(所有节点)
#如果不创建命令软连接,检测mha复制情况的时候会报错
ln -s /usr/local/mysql/bin//mysqlbinlog /usr/bin/mysqlbinlog
ln -s /usr/local/mysql/bin//mysql /usr/bin/mysql
3)部署管理节点(mha-manager:mysql-db03)
#使用epel源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache
#安装manager依赖包
yum install -y perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
#安装manager包
rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm
4)编辑配置文件
#创建配置文件目录
mkdir -p /etc/mha
#创建日志目录
mkdir -p /var/log/mha/app1
#编辑mha配置文件
vim /etc/mha/app1.cnf
==================================
[server default]
manager_log=/var/log/mha/app1/manager
manager_workdir=/var/log/mha/app1
master_binlog_dir=/data/mysql/data/
master_ip_failover_script=/usr/local/mha/bin/master_ip_failover
password=mha
ping_interval=2
repl_password=1234
repl_user=replication
ssh_user=root
user=mha
[server1]
hostname=192.168.1.17
port=3306
[server2]
hostname=192.168.1.151
port=3306
[server3]
hostname=192.168.1.219
port=3306
====================================
5)配置ssh信任(所有节点)
#创建秘钥对
ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
#发送公钥,包括自己
ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.1.219
ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.1.17
ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.1.151
测试登入是需要密码
ssh 192.168.1.219
ssh 192.168.1.17
ssh 192.168.1.151
6)解压安装包
mkdir /usr/local/mha
tar -xf mha4mysql-manager-0.56.tar.gz
mv mha4mysql-manager-0.56 /usr/local
cd /usr/local
mv mha4mysql-manager-0.56 mha
7)启动测试
# 在MHA manage节点测试ssh
cd /usr/local/mha/bin
/usr/local/mha/bin/masterha_check_ssh --conf=/etc/mha/app1.cnf
#看到如下字样,则测试成功
Tue Mar 7 01:03:33 2017 - [info] All SSH connection tests passed successfully.
# 在MHA manage节点测试复制
/usr/local/mha/bin/masterha_check_repl --conf=/etc/mha/app1.cnf
#看到如下字样,则测试成功
MySQL Replication Health is OK.
8)启动MHA
#启动
cd /usr/local/mha/bin
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 &
如果在做项目时建议写成脚本,否则在当shell中提示了nohup成功后,还需要按终端上键盘任意键退回到shell输入命令窗口,然后通过在shell中输入exit来退出终端;如果在nohup执行成功后直接点关闭程序按钮关闭终端的话,这时候会断掉该命令所对应的session,导致nohup对应的进程被通知需要一起shutdown,起不到关掉终端后调用程序继续后台运行的作用。
vim start_mha.sh
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 &
9)检查MHA运行状态
/usr/local/mha/bin/masterha_check_status --conf=/etc/mha/app1.cnf
app1 (pid:22623) is running(0:PING_OK), master:192.168.1.17
六切换测试
1)切换master测试
#登录数据库(db02)
mysql -uroot -p
#检查复制情况
show slave status\G
#手动停止主库
/etc/init.d/mysqld stop
Shutting down MySQL..... SUCCESS!
再停止数据的同时查看日志信息的变化
tailf /var/log/mha/app1/manager