CentOS6.8下MySQL MHA架构搭建笔记
以下是CentOS6.8下MySQL MHA架构搭建笔记
IP资源规划:
```192.168.206.139 master
192.168.206.140 slave01(备用master)
192.168.206.141 slave02
192.168.206.142 manager
192.168.206.145 VIP
```
一、准备工作:
1.关闭Selinux
[root@localhost ~]# vi /etc/selinux/config [root@localhost ~]# reboot
2.关闭防火墙
[root@localhost ~]# chkconfig iptables off [root@localhost ~]# service iptables stop
3.安装epel yum源
[root@localhost tmp]# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@localhost tmp]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
4.安装MHA node所需的perl模块(DBD:mysql)
[root@localhost tmp]# yum -y install perl-DBD-MySQL ncftp perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes perl-devel
二、Replication搭建
1.建立replication用户(master和slave01)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'u_repl'@'192.168.206.%' IDENTIFIED BY 'replpass' with grant option;
2.建立MHA管理用用户
mysql> GRANT ALL PRIVILEGES ON *.* to 'root'@'192.168.206.%' IDENTIFIED BY 'checkpass';
3.安装半同步复制插件
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
4.修改my.cnf配置文件
[root@localhost tmp]# vi /etc/my.cnf
5.master备份slave还原(略),查找master Binlog位置
6.各slave上执行脚本,建立主从配置。(MySQL5.7可以直接使用master_auto_position=1参数)
mysql> change master to master_host='192.168.206.139',master_user='u_repl',master_password='replpass',master_log_file='mysql-bin.000001' ,master_log_pos=154; mysql> start slave;
三、开始安装配置
1.各节点使用ssh-keygen实现三台机器之间相互免密钥登录
[root@localhost tmp]# ssh-keygen -t rsa [root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] [root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected] [root@localhost .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
2.所有节点(包括manager)下载安装mha4mysql-node-0.56
由于google被墙,我是直接从网上下载的rpm文件,具体下载过程略。
[root@localhost tmp]# yum localinstall mha4mysql-node-0.56-0.el6.noarch.rpm
确认安装内容
[root@localhost tmp]# rpm -ql mha4mysql-node
3.manager节点安装mha4mysql-manager-0.56
[root@localhost tmp]# yum localinstall mha4mysql-manager-0.56-0.el6.noarch.rpm
确认安装内容
[root@localhost tmp]# rpm -ql mha4mysql-manager
4.在manager节点管理MHA配置文件
前面采用yum安装的,没有samples文件夹,所以另外down了一份mha4mysql-manager-0.56.tar.gz包(具体过程略)
复制代码[root@localhost tmp]# tar xf mha4mysql-manager-0.56.tar.gz [root@localhost tmp]# cd mha4mysql-manager-0.56 [root@localhost mha4mysql-manager-0.56]# mkdir -p /etc/mha/{app1,scripts} [root@localhost mha4mysql-manager-0.56]# cp -r samples/conf/* /etc/mha/ [root@localhost mha4mysql-manager-0.56]# cp -r samples/scripts/* /etc/mha/scripts/ [root@localhost mha4mysql-manager-0.56]# mv /etc/mha/app1.cnf /etc/mha/app1/ [root@localhost mha4mysql-manager-0.56]# mv /etc/mha/masterha_default.cnf /etc/masterha_default.cnf
复制代码
5.在manager上设置全局配置
修改master_ip_failover脚本,添加VIP漂移相关三个参数
[root@localhost mha4mysql-manager-0.56]# vi /etc/mha/scripts/master_ip_failover
修改master_ip_online_change脚本,添加VIP漂移相关三个参数
[root@localhost scripts]# vi master_ip_online_change
修改masterha_default.cnf全局配置
[root@localhost mha4mysql-manager-0.56]# vi /etc/masterha_default.cnf
创建日志目录:
[root@localhost mha4mysql-manager-0.56]# mkdir -p /var/log/mha/app1
配置app1.cnf:
[root@localhost mha4mysql-manager-0.56]# vi /etc/mha/app1/app1.cnf
6.验证安装内容
验证ssh信任
[root@localhost mha4mysql-manager-0.56]# masterha_check_ssh --conf=/etc/mha/app1/app1.cnf
验证主从复制
[root@localhost mha4mysql-manager-0.56]# masterha_check_repl --conf=/etc/mha/app1/app1.cnf
四、启动MHA、测试MHA故障转移
启动MHA
[root@localhost mha4mysql-manager-0.56]# masterha_manager --conf=/etc/mha/app1/app1.cnf
或者采用后台启动
[root@localhost mha4mysql-manager-0.56]# nohup masterha_manager --conf=/etc/mha/app1/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/app1/manager.log 2>&1 &
配置master的VIP
[root@localhost mysql]# /sbin/ifconfig eth0:234 192.168.206.145 netmask 255.255.255.0 up
确认VIP
[root@localhost mysql]# ifconfig
确认MHA启动状态:
[root@localhost ~]# masterha_check_status --conf=/etc/mha/app1/app1.cnf
进程确认:
[root@localhost scripts]# ps aux|grep mha
关闭MHA Manager监控:
[root@localhost mha4mysql-manager-0.56]# masterha_stop --conf=/etc/mha/app1/app1.cnf
主动切换之交互模式
[root@localhost scripts]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1/app1.cnf --new_master_host=192.168.206.140
重新将旧的master加入复制,指向新的master
mysql> change master to master_host='192.168.206.140',master_user='u_repl',master_password='replpass',master_auto_position=1; mysql> start slave;
主动切换之非交互模式:
[root@localhost scripts]# masterha_master_switch --master_state=alive --conf=/etc/mha/app1/app1.cnf --new_master_host=192.168.206.139 --interactive=0
Master192.168.206.140宕机模式下切换主从:
masterha_master_switch --master_state=dead --conf=/etc/mha/app1/app1.cnf --dead_master_host=192.168.206.140 --dead_master_ip=192.168.206.140 --dead_master_port=3306 --new_master_host=192.168.206.139