MySQL 高可用集群架构 MHA 详解(3)

注:若主MYSQL服务器已经存在,只是后期才搭建从MYSQL服务器,在置配数据同步前应先将主MYSQL服务器的要同步的数据库拷贝到从MYSQL服务器上(如先在主MYSQL上备份数据库,再用备份在从MYSQL服务器上恢复)

master mysql主机:

server-id = 1
log-bin=mysql-bin
binlog_format=mixed

log-bin-index=mysql-bin.index

rpl_semi_sync_master_enabled=1

rpl_semi_sync_master_timeout=10000

rpl_semi_sync_slave_enabled=1

relay_log_purge=0

relay-log= relay-bin

relay-log-index = relay-bin.index

注:

rpl_semi_sync_master_enabled=1  1表是启用,0表示关闭

rpl_semi_sync_master_timeout=10000:毫秒单位,该参数主服务器等待确认消息10秒后,不再等待,变为异步方式。

Candidate 主机:

server-id = 2
log-bin=mysql-bin
binlog_format=mixed

log-bin-index=mysql-bin.index

relay_log_purge=0

relay-log= relay-bin

relay-log-index = slave-relay-bin.index

rpl_semi_sync_master_enabled=1

rpl_semi_sync_master_timeout=10000

rpl_semi_sync_slave_enabled=1

注:relay_log_purge=0,禁止 SQL 线程在执行完一个 relay log 后自动将其删除,对于MHA场景下,对于某些滞后从库的恢复依赖于其他从库的relaylog,因此采取禁用自动删除功能

Slave主机:

Server-id = 3

log-bin = mysql-bin

relay-log = relay-bin

relay-log-index = slave-relay-bin.index
read_only = 1

rpl_semi_sync_slave_enabled = 1

查看半同步相关信息

mysql> show variables like '%rpl_semi_sync%';

查看半同步状态:

mysql> show status like '%rpl_semi_sync%';

| Rpl_semi_sync_master_clients               | 2     |

重点关注的参数:

rpl_semi_sync_master_status :显示主服务是异步复制模式还是半同步复制模式 

rpl_semi_sync_master_clients :显示有多少个从服务器配置为半同步复制模式 

rpl_semi_sync_master_yes_tx :显示从服务器确认成功提交的数量 

rpl_semi_sync_master_no_tx :显示从服务器确认不成功提交的数量 

rpl_semi_sync_master_tx_avg_wait_time :事务因开启 semi_sync ,平均需要额外等待的时间 

rpl_semi_sync_master_net_avg_wait_time :事务进入等待队列后,到网络平均等待时间 

三、配置mysql-mha

所有mysql节点安装 

rpm -ivh perl-DBD-MySQL-4.013-3.el6.i686.rpm  [yum -y install perl-DBD-MySQL]

rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm

2. manage需安装依赖的perl包  

rpm -ivh perl-Config-Tiny-2.12-7.1.el6.noarch.rpm

rpm -ivh perl-DBD-MySQL-4.013-3.el6.i686.rpm  [yum -y install perl-DBD-MySQL]

rpm -ivh compat-db43-4.3.29-15.el6.x86_64.rpm

rpm -ivh perl-Mail-Sender-0.8.16-3.el6.noarch.rpm

rpm -ivh perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm

rpm -ivh perl-TimeDate-1.16-11.1.el6.noarch.rpm

rpm -ivh perl-MIME-Types-1.28-2.el6.noarch.rpm

rpm -ivh perl-MailTools-2.04-4.el6.noarch.rpm

rpm -ivh perl-Email-Date-Format-1.002-5.el6.noarch.rpm

rpm -ivh perl-Params-Validate-0.92-3.el6.x86_64.rpm

rpm -ivh perl-Params-Validate-0.92-3.el6.x86_64.rpm

rpm -ivh perl-MIME-Lite-3.027-2.el6.noarch.rpm

rpm -ivh perl-Mail-Sendmail-0.79-12.el6.noarch.rpm

rpm -ivh perl-Log-Dispatch-2.27-1.el6.noarch.rpm

yum install -y perl-Time-HiRes-1.9721-144.el6.x86_64

rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm

3. 配置mha

配置文件位于管理节点,通常包括每一个mysql server的主机名,mysql用户名,密码,工作目录等等。

mkdir /etc/masterha/

vim /etc/masterha/app1.cnf

[server default]

user=mhauser

password=123456

manager_workdir=/data/masterha/app1 

manager_log=/data/masterha/app1/manager.log

remote_workdir=/data/masterha/app1

ssh_user=root

repl_user=repl

repl_password=123456

ping_interval=1

[server1]

hostname=192.168.137.134

port=3306

master_binlog_dir=/usr/local/mysql/data

candidate_master=1

[server2]

hostname=192.168.137.130

port=3306

master_binlog_dir=/usr/local/mysql/data

candidate_master=1

[server3]

hostname=192.168.137.146

port=3306

master_binlog_dir=/usr/local/mysql/data

no_master=1

配关配置项的解释

manager_workdir=/masterha/app1//设置manager的工作目录

manager_log=/masterha/app1/manager.log//设置manager的日志

user=manager//设置监控用户manager

password=123456  //监控用户manager的密码

ssh_user=root  //ssh连接用户

repl_user=mharep  //主从复制用户

repl_password=123.abc//主从复制用户密码

ping_interval=1  //设置监控主库,发送ping包的时间间隔,默认是3秒,尝试三次没有回应的时候自动进行failover

master_binlog_dir=/usr/local/mysql/data  //设置master 保存binlog的位置,以便MHA可以找到master的日志,我这里的也就是mysql的数据目录

candidate_master=1//设置为候选master,如果设置该参数以后,发生主从切换以后将会将此从库提升为主库。

检测各节点间ssh互信通信配置是否ok

masterha_check_ssh --conf=/etc/masterha/app1.cnf

结果:All SSH connection tests passed successfully.

检测各节点间主从复制是否ok

masterha_check_repl --conf=/etc/masterha/app1.cnf

结果:MySQL Replication Health is OK.

在验证时,若遇到这个错误:Can't exec "mysqlbinlog" ......

解决方法是在所有服务器上执行:

ln -s /usr/local/mysql/bin/* /usr/local/bin/

启动manager:

nohup /usr/bin/masterha_manager --conf=/etc/masterha/app1.cnf  --remove_dead_master_conf --ignore_last_failover > /etc/masterha/manager.log 2>&1 &

--remove_dead_master_conf 为主从切换后,老的主库IP将会从配置文件中移除

--ignore_last_failover 忽略生成的切换完成文件,若不忽略,则8小时内无法再次切换

--ignore_fail_on_start

##当有slave 节点宕掉时,MHA默认是启动不了的,加上此参数即使有节点宕掉也能启动MHA,

关闭MHA:

masterha_stop  --conf=/etc/masterha/app1.cnf

查看MHA状态:

masterha_check_status --conf=/etc/masterha/app1.cnf

app1 (pid:45128) is running(0:PING_OK), master:192.168.137.134

4.模拟故障转移

停掉master,

/etc/init.d/mysqld stop

查看 MHA 日志  /data/masterha/app1/manager.log

----- Failover Report -----

app1: MySQL Master failover 192.168.137.134(192.168.137.134:3306) to 192.168.137.1

30(192.168.137.130:3306) succeeded

Master 192.168.137.134(192.168.137.134:3306) is down!

Check MHA Manager logs at zifuji:/data/masterha/app1/manager.log for details.

Started automated(non-interactive) failover.

The latest slave 192.168.137.130(192.168.137.130:3306) has all relay logs for reco

very.

Selected 192.168.137.130(192.168.137.130:3306) as a new master.

192.168.137.130(192.168.137.130:3306): OK: Applying all logs succeeded.

192.168.137.146(192.168.137.146:3306): This host has the latest relay log events.

Generating relay diff files from the latest slave succeeded.

192.168.137.146(192.168.137.146:3306): OK: Applying all logs succeeded. Slave star

ted, replicating from 192.168.137.130(192.168.137.130:3306)

192.168.137.130(192.168.137.130:3306): Resetting slave info succeeded.

Master failover to 192.168.137.130(192.168.137.130:3306) completed successfully.

3.  查看slave复制状态

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.137.130

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/e77e4e4e63e9f717e19b57a0f5bc94bc.html