@zhangsiming65965
2019-02-23T12:54:04.000000Z
字数 22592
阅读 158
云计算
如果有梦想,就放开的去追;
因为只有奋斗,才能改变命运;
主机名 | IP地址 | 描述 |
---|---|---|
MHA1 | 192.168.17.109 | CentOS6.5,MySQL5.6 |
MHA2 | 192.168.17.110 | CentOS6.5,MySQL5.6 |
MHA3 | 192.168.17.111 | CentOS6.5,MySQL5.6 |
[root@MHA1 ~]# vim /etc/hosts
[root@MHA1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.17.109 MHA1
192.168.17.110 MHA2
192.168.17.111 MHA3
[root@MHA1 ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@MHA1 ~]# uname -r
2.6.32-431.el6.x86_64
[root@MHA1 ~]# service iptables stop
[root@MHA1 ~]# chkconfig iptables off
[root@MHA1 ~]# vim /etc/sysconfig/selinux
[root@MHA1 ~]# getenforce 0
Disabled
MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件;
在MySQL故障切换过程中,MHA能做到0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换过程中,MHA能最大程度上保证数据库的一致性,以达到真正意义上的高可用;
MHA由两部分组成:MHA Manager(管理节点)和MHA Node(数据节点)。MHA Manager可以独立部署在一台独立的机器上管理多个Master-Slave集群,也可以部署在一台Slave上。当Master出现故障时,它可以自动将最新数据的Slave提升为新的Master,然后将所有其他的Slave重新指向新的Master。整个故障转移过程对应程序是完全透明的。
[root@Zhangsiming ~]# vim /etc/ansible/hosts
[root@Zhangsiming ~]# cat /etc/ansible/hosts
[Nginx]
web01 ansible_ssh_host=192.168.17.109 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666
web02 ansible_ssh_host=192.168.17.110 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666
web03 ansible_ssh_host=192.168.17.111 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=666666
#ansible添加Nginx主机组,包括三台MHA节点
[root@Zhangsiming ~]# cat mysql.sh
#!/bin/bash
#designed by ZhangSiming
id mysql
if [ $? -ne 0 ];then
useradd -M -s /sbin/nologin mysql
fi
yum -y install ncurses-devel
yum -y install libaio
cd ~
tar xf mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local
ln -s /usr/local/mysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql
/bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
/bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
ln -s /usr/local/mysql/bin/* /usr/local/bin
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
/etc/init.d/mysqld start
sleep 5
mysqladmin -uroot password "666666"
chown -R mysql.mysql /usr/local/mysql
#MySQL安装脚本
[root@Zhangsiming ~]# ansible Nginx -m script -a "/root/mysql.sh"
#测试结果
[root@MHA1 ~]# netstat -antup | grep mysql
tcp 0 0 :::3306 :::* LISTEN 1332/mysqld
[root@MHA1 ~]# mysql -uroot -p666666 -e "show databases;"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
#脚本一键部署成功
GTID(Global Transaction)全局事务标识符:是一个唯一的标识符,它创建并与源服务器(主)上提交的每个事务相关联。此标识符不仅对其发起的服务器是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有1对1的映射。
GTID实际上是由UUID+TID组成的。其中UUID是一个MySQL实例的唯一标识。TID代表了该实例上已经提交的事务数量,并且随着事务提交单调递增。
对比 | 基于GTID的主从复制 | 传统主从复制 |
---|---|---|
基于什么复制 | GTID | 二进制日志的POS |
slave端是否需要开启binlog日志 | 需要 | 不需要 |
1.当一个事务在主库端执行并提交时,产生GTID,一同记录到主库binlog日志中;
2.binlog传输到slave,并存储到slave的relaylog后,读取这个GTID的这个值设置gtid_next变量,即告诉Slave,下一个要执行的GTID值;
3.sql线程从relay log中获取GTID,然后对比slave端的binlog是否有该GTID;
4.如果有记录,说明该GTID的事务已经执行,slave会忽略;
5.如果没有记录,slave就会执行该GTID事务,并记录该GTID到自身的binlog,
在读取执行事务前会先检查其他session持有该GTID,确保不被重复执行;
6.在解析过程中会判断是否有主键,如果没有就用二级索引,如果没有就用全部扫描。
[root@MHA1 ~]# cat /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock
#MySQL连接实例
[mysqld]
gtid_mode = ON
log_slave_updates
enforce_gtid_consistency
#上三句话开启GTID功能,MySQL5.6才有的新功能
lower_case_table_names = 1
default-storage-engine = InnoDB
#InnoDB存储引擎
port = 3306
#3306端口
datadir = /usr/local/mysql/data
#数据目录
character-set-server = utf8
#支持中文
socket = /usr/local/mysql/data/mysql.sock
log_bin = mysql-bin #开启binlog日志
binlog_format = row
#强烈建议二进制日志用row格式,虽然占用空间大,但是严谨,不会产生数据错误
server_id = 1 #设置server_id
innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
#一些优化配置
[root@MHA3 ~]# cat /etc/my.cnf
[client]
socket = /usr/local/mysql/data/mysql.sock
[mysqld]
gtid_mode = ON
log_slave_updates
enforce_gtid_consistency
lower_case_table_names = 1
default-storage-engine = InnoDB
port = 3306
datadir = /usr/local/mysql/data
character-set-server = utf8
socket = /usr/local/mysql/data/mysql.sock
log_bin = mysql-bin
#从库也必须开启binlog日志,目的是记录同步过的GTID
binlog_format = row
#强烈建议二进制日志用row格式,虽然占用空间大,但是严谨,不会产生数据错误
relay-log = /usr/local/mysql/data/relay-bin
#从库开启relay-log
relay_log_purge = 0
#因为从库SQL线程需要从relay-log获取GTID和自身二进制文件GTID对比,所以做好关闭自动清除relay-log的功能
server_id = 10 #设置不同的server_id
innodb_buffer_pool_size = 200M
slave-parallel-workers = 8
thread_cache_size = 600
back_log = 600
slave_net_timeout = 60
max_binlog_size = 512M
key_buffer_size = 8M
query_cache_size = 64M
join_buffer_size = 2M
sort_buffer_size = 2M
query_cache_type = 1
thread_stack = 192K
read-only = 1
#从库设置只读
两个从库和主库配置文件完全一致,需要打开二进制日志功能,GTID功能,只是三者的server ID需要两两不同,这里我设置的主为1,从为5和10。
在以往如果是基于binlog日志的主从复制,则必须要记住主库的master状态信息。
但是在MySQL5.6版本里多了一个Gtid的功能,可以自动记录主从复制位置点的信息,并在日志中输出出来。
[root@MHA1 ~]# /etc/init.d/mysqld restart
Starting MySQL. SUCCESS!
[root@MHA1 ~]# mysql -uroot -p666666 -e "grant replication slave on *.* to rep@'192.168.17.%' identified by '666666';"
Warning: Using a password on the command line interface can be insecure.
[root@MHA1 ~]# mysql -uroot -p666666 -e "show grants for rep@'192.168.17.%';"
Warning: Using a password on the command line interface can be insecure.
+---------------------------------------------------------------------------------------------------------------------------+
| Grants for rep@192.168.17.% |
+---------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.17.%' IDENTIFIED BY PASSWORD '*B2B366CA5C4697F31D4C55D61F0B17E70E5664EC' |
+---------------------------------------------------------------------------------------------------------------------------+
#创建成功
#确定主库从库都开启了GTID,查看GTID状态
#主库
[root@MHA1 ~]# mysql -uroot -p666666 -e "show global variables like '%gtid%';"
Warning: Using a password on the command line interface can be insecure.
+--------------------------+----------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| enforce_gtid_consistency | ON |
| gtid_executed | d21eed35-2eeb-11e9-94d2-000c2987f310:1 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
+--------------------------+----------------------------------------+
#从库
[root@MHA2 ~]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!
[root@MHA2 ~]# mysql -uroot -p666666 -e "show global variables like '%gtid%';"
Warning: Using a password on the command line interface can be insecure.
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
+--------------------------+-------+
[root@MHA2 ~]# mysql -uroot -p666666 -e "change master to master_host='192.168.17.109',master_user='rep',master_password='666666',master_auto_position=1;"
#master_auto_position=1代表开启GTID自动追踪主从复制需要同步的position
Warning: Using a password on the command line interface can be insecure.
[root@MHA2 ~]# mysql -uroot -p666666 -e "start slave;"
Warning: Using a password on the command line interface can be insecure.
#两个从库都这样配置,并开启主从复制
[root@MHA3 ~]# mysql -uroot -p666666 -e "show slave status\G" | egrep "Slave_IO_Running|Slave_SQL_Running|Seconds_Behind_Master"
Warning: Using a password on the command line interface can be insecure.
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
#从库IO线程、SQL线程工作正常,主从复制延迟为0
[root@MHA1 ~]# yum -y install perl-DBD-MySQL
#安装依赖包
[root@MHA1 ~]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
#安装MHA节点rpm包
Preparing... ########################################### [100%]
1:mha4mysql-node ########################################### [100%]
[root@MHA1 ~]# mysql -uroot -p666666 -e "grant all privileges on *.* to mha@'192.168.17.%' identified by '666666';"
Warning: Using a password on the command line interface can be insecure.
#因为开启主从复制,主库创建的账号自动同步到从库
#MHA管理节点可以是独立的,也可以在MySQL主从架构的从库上,这里部署在MHA3上
#使用epel源安装依赖包
[root@MHA3 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
[root@MHA3 ~]# yum -y install perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes
#自定义MHA管理rpm包的yum依赖源,之后用localinstall方式安装MHA管理rpm包
[root@MHA3 ~]# ls /rpm
Atlas-2.2.1.el6.x86_64.rpm
epel-release-6-8.noarch.rpm
mha4mysql-manager-0.56-0.el6.noarch.rpm
mha4mysql-node-0.56-0.el6.noarch.rpm
perl-5.10.1-144.el6.x86_64.rpm
perl-Config-Tiny-2.12-7.1.el6.noarch.rpm
perl-Email-Date-Format-1.002-5.el6.noarch.rpm
perl-libs-5.10.1-144.el6.x86_64.rpm
perl-Log-Dispatch-2.27-1.el6.noarch.rpm
perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
perl-MailTools-2.04-4.el6.noarch.rpm
perl-MIME-Lite-3.027-2.el6.noarch.rpm
perl-MIME-Types-1.28-2.el6.noarch.rpm
perl-Module-Pluggable-3.90-144.el6.x86_64.rpm
perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
perl-Params-Validate-0.92-3.el6.x86_64.rpm
perl-Pod-Escapes-1.04-144.el6.x86_64.rpm
perl-Pod-Simple-3.13-144.el6.x86_64.rpm
perl-TimeDate-1.16-13.el6.noarch.rpm
perl-Time-HiRes-1.9721-144.el6.x86_64.rpm
perl-version-0.77-144.el6.x86_64.rpm
repodata
[root@MHA3 ~]# cat /etc/yum.repos.d/CentOS-Media.repo
[c6-media]
name=CentOS-$releasever - Media
baseurl=file:///media/CentOS/
file:///media/cdrom/
file:///rpm
自定义/rpm的yum源
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
[root@MHA3 ~]# yum -y localinstall mha4mysql-manager-0.56-0.el6.noarch.rpm
[root@MHA3 ~]# mkdir -p /etc/mha
#创建MHA的文件目录
[root@MHA3 ~]# mkdir -p /var/log/mha/mha1
#创建MHA的日志目录
[root@MHA3 ~]# vim /etc/mha/mha1.cnf
[root@MHA3 ~]# cat /etc/mha/mha1.cnf
[server default]
manager_log=/var/log/mha/mha1/manager
#MHA管理日志的位置
manager_workdir=/var/log/mha/mha1
#MHA管理日志的目录路径
master_binlog_dir=/usr/local/mysql/data
#MySQL二进制文件位置
user=mha
password=666666
#MHA用户密码
ping_interval=2
#健康检测监控时间
repl_user=rep
repl_password=666666
#MySQL主从复制用户密码
ssh_user=root
#ssh连接主机用户
#下面是三个主从复制架构节点
[server1]
hostname=192.168.17.109
port=3306
[server2]
hostname=192.168.17.110
port=3306
candidate_master=1
#候选MySQL主
check_repl_delay=0
#忽略延迟,MHA默认不会选择主从复制延迟大的从提升为主,这两句一般情况下一起用
[server3]
hostname=192.168.17.111
port=3306
#注意一定是两两配置信任(自己也需要)
[root@MHA1 ~]# ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa
[root@MHA1 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.17.109
[root@MHA1 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.17.110
[root@MHA1 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.17.111
#在三台服务器都运行一遍!
[root@MHA3 ~]# masterha_check_ssh --conf=/etc/mha/mha1.cnf
Thu Feb 14 04:04:26 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Feb 14 04:04:26 2019 - [info] Reading application default configuration from /etc/mha/mha1.cnf..
Thu Feb 14 04:04:26 2019 - [info] Reading server configuration from /etc/mha/mha1.cnf..
Thu Feb 14 04:04:26 2019 - [info] Starting SSH connection tests..
Thu Feb 14 04:04:27 2019 - [debug]
Thu Feb 14 04:04:26 2019 - [debug] Connecting via SSH from root@192.168.17.109(192.168.17.109:22) to root@192.168.17.110(192.168.17.110:22)..
Thu Feb 14 04:04:26 2019 - [debug] ok.
Thu Feb 14 04:04:26 2019 - [debug] Connecting via SSH from root@192.168.17.109(192.168.17.109:22) to root@192.168.17.111(192.168.17.111:22)..
Thu Feb 14 04:04:27 2019 - [debug] ok.
Thu Feb 14 04:04:28 2019 - [debug]
Thu Feb 14 04:04:26 2019 - [debug] Connecting via SSH from root@192.168.17.110(192.168.17.110:22) to root@192.168.17.109(192.168.17.109:22)..
Thu Feb 14 04:04:27 2019 - [debug] ok.
Thu Feb 14 04:04:27 2019 - [debug] Connecting via SSH from root@192.168.17.110(192.168.17.110:22) to root@192.168.17.111(192.168.17.111:22)..
Thu Feb 14 04:04:28 2019 - [debug] ok.
Thu Feb 14 04:04:28 2019 - [debug]
Thu Feb 14 04:04:27 2019 - [debug] Connecting via SSH from root@192.168.17.111(192.168.17.111:22) to root@192.168.17.109(192.168.17.109:22)..
Thu Feb 14 04:04:27 2019 - [debug] ok.
Thu Feb 14 04:04:27 2019 - [debug] Connecting via SSH from root@192.168.17.111(192.168.17.111:22) to root@192.168.17.110(192.168.17.110:22)..
Thu Feb 14 04:04:28 2019 - [debug] ok.
Thu Feb 14 04:04:28 2019 - [info] All SSH connection tests passed successfully.
#三台MySQL主从复制节点ssh健康检测成功
#这里注意了,需要给所有节点都上一个MySQL主从复制账号,因为切换之后如果没有主从复制账号还咋验证?
[root@MHA2 data]# mysql -uroot -p666666 -e "grant replication slave on *.* to rep@'192.168.17.%' identified by '666666';"
[root@MHA3 data]# mysql -uroot -p666666 -e "grant replication slave on *.* to rep@'192.168.17.%' identified by '666666';"
#主从复制检测
[root@MHA3 ~]# which masterha_check_repl
/usr/bin/masterha_check_repl
[root@MHA3 ~]# masterha_check_repl --conf=/etc/mha/mha1.cnf
Thu Feb 14 04:06:37 2019 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Feb 14 04:06:37 2019 - [info] Reading application default configuration from /etc/mha/mha1.cnf..
Thu Feb 14 04:06:37 2019 - [info] Reading server configuration from /etc/mha/mha1.cnf..
Thu Feb 14 04:06:37 2019 - [info] MHA::MasterMonitor version 0.56.
Thu Feb 14 04:06:38 2019 - [info] GTID failover mode = 1
Thu Feb 14 04:06:38 2019 - [info] Dead Servers:
Thu Feb 14 04:06:38 2019 - [info] Alive Servers:
Thu Feb 14 04:06:38 2019 - [info] 192.168.17.109(192.168.17.109:3306)
Thu Feb 14 04:06:38 2019 - [info] 192.168.17.110(192.168.17.110:3306)
Thu Feb 14 04:06:38 2019 - [info] 192.168.17.111(192.168.17.111:3306)
Thu Feb 14 04:06:38 2019 - [info] Alive Slaves:
Thu Feb 14 04:06:38 2019 - [info] 192.168.17.110(192.168.17.110:3306) Version=5.6.17-log (oldest major version between slaves) log-bin:enabled
Thu Feb 14 04:06:38 2019 - [info] GTID ON
Thu Feb 14 04:06:38 2019 - [info] Replicating from 192.168.17.109(192.168.17.109:3306)
Thu Feb 14 04:06:38 2019 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Feb 14 04:06:38 2019 - [info] 192.168.17.111(192.168.17.111:3306) Version=5.6.17-log (oldest major version between slaves) log-bin:enabled
Thu Feb 14 04:06:38 2019 - [info] GTID ON
Thu Feb 14 04:06:38 2019 - [info] Replicating from 192.168.17.109(192.168.17.109:3306)
Thu Feb 14 04:06:38 2019 - [info] Current Alive Master: 192.168.17.109(192.168.17.109:3306)
Thu Feb 14 04:06:38 2019 - [info] Checking slave configurations..
Thu Feb 14 04:06:38 2019 - [info] Checking replication filtering settings..
Thu Feb 14 04:06:38 2019 - [info] binlog_do_db= , binlog_ignore_db=
Thu Feb 14 04:06:38 2019 - [info] Replication filtering check ok.
Thu Feb 14 04:06:38 2019 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Thu Feb 14 04:06:38 2019 - [info] Checking SSH publickey authentication settings on the current master..
Thu Feb 14 04:06:38 2019 - [info] HealthCheck: SSH to 192.168.17.109 is reachable.
Thu Feb 14 04:06:38 2019 - [info]
192.168.17.109(192.168.17.109:3306) (current master)
+--192.168.17.110(192.168.17.110:3306)
+--192.168.17.111(192.168.17.111:3306)
Thu Feb 14 04:06:38 2019 - [info] Checking replication health on 192.168.17.110..
Thu Feb 14 04:06:38 2019 - [info] ok.
Thu Feb 14 04:06:38 2019 - [info] Checking replication health on 192.168.17.111..
Thu Feb 14 04:06:38 2019 - [info] ok.
Thu Feb 14 04:06:38 2019 - [warning] master_ip_failover_script is not defined.
Thu Feb 14 04:06:38 2019 - [warning] shutdown_script is not defined.
Thu Feb 14 04:06:38 2019 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
#主从复制检测成功
[root@MHA3 ~]# which masterha_manager
/usr/bin/masterha_manager
[root@MHA3 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 2340
#nohup ... &:常用于禁止session(nohup)关闭或者ctrl+c(&)停止进程
[root@MHA3 ~]# ps -elf | grep perl | grep -v grep
0 S root 2340 2077 0 80 0 - 48831 hrtime 04:14 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
#启动成功
[root@MHA2 ~]# mysql -uroot -p666666 -e "show slave status\G"
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.17.109
#主库IP为192.168.17。109
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 660
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 870
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@MHA1 ~]# /etc/init.d/mysqld stop
Shutting down MySQL....... SUCCESS!
#停止成功
[root@MHA3 data]# mysql -uroot -p666666 -e "show slave status\G"
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.17.110
#master变为了MHA2
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 231
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 401
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
[root@MHA2 data]# mysql -uroot -p666666 -e "show master status;"
Warning: Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| mysql-bin.000004 | 231 | | | 76efda2a-2fc2-11e9-9a4a-000c2987f310:1-2,
b4654a7e-2fcd-11e9-9a93-000c2967becd:1 |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
#MHA2作为了MySQL主从同步的主
[root@MHA3 data]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 3721
#MHA后台进程自动终止了
[root@MHA3 data]# cat /etc/mha/mha1.cnf
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=666666
ping_interval=2
repl_password=666666
repl_user=rep
ssh_user=root
user=mha
#查看MHA配置文件发现server1自动消失了
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.17.110
port=3306
[server3]
hostname=192.168.17.111
port=3306
cat /var/log/mha/mha1/manager
...省略...
Thu Feb 14 04:43:26 2019 - [info] Resetting slave info on the new master..
#把从库信息录入到新的主上
Thu Feb 14 04:43:27 2019 - [info] 192.168.17.110: Resetting slave info succeeded.
Thu Feb 14 04:43:27 2019 - [info] Master failover to 192.168.17.110(192.168.17.110:3306) completed successfully.
#成功热备master切换
Thu Feb 14 04:43:27 2019 - [info] Deleted server1 entry from /etc/mha/mha1.cnf .
#删除/etc/mha/mha1.cnf中的server1模块
...省略...
我们需要重启MHA1的MySQL,然后配置作为从库MHA2的从库;由于切换后MHA进程停止了,我们需要重新配置并启动。
[root@MHA1 ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@MHA1 ~]# mysql -uroot -p666666 -e "change master to master_host='192.168.17.110',master_user='rep',master_password='666666',master_auto_position=1;"
[root@MHA1 ~]# mysql -uroot -p666666 -e "start slave;"
[root@MHA1 ~]# mysql -uroot -p666666 -e "show slave status\G"
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.17.110
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 231
Relay_Log_File: MHA1-relay-bin.000005
Relay_Log_Pos: 401
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
因为MHA故障转移切换之后,MHA1服务器在MHA配置文件里面的部分就自动消失了,我们需要手动重新写回来。
[root@MHA3 ~]# vim /etc/mha/mha1.cnf
[root@MHA3 ~]# cat /etc/mha/mha1.cnf
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=666666
ping_interval=2
repl_password=666666
repl_user=rep
ssh_user=root
user=mha
[server1]
hostname=192.168.17.109
port=3306
candidate_master=1
check_repl_delay=0
#将MHA1的server补回来作为首选固定切换的MySQL主
[server2]
hostname=192.168.17.110
port=3306
[server3]
hostname=192.168.17.111
port=3306
[root@MHA3 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 1573
[root@MHA3 ~]# ps -elf | grep perl | grep -v grep
0 S root 1573 1527 1 80 0 - 48831 hrtime 02:06 pts/0 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
#成功启动
[root@MHA2 ~]# /etc/init.d/mysqld stop
Shutting down MySQL..... SUCCESS!
[root@MHA3 ~]# mysql -uroot -p666666 -e "show slave status\G"
Warning: Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.17.109
#MHA3的主自动换回了MHA1服务器的MySQL
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 447
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 408
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#MHA2服务器
[root@MHA2 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
#MHA3服务器
[root@MHA3 ~]# vim /etc/mha/mha1.cnf
[root@MHA3 ~]# cat /etc/mha/mha1.cnf
[server default]
manager_log=/var/log/mha/mha1/manager
manager_workdir=/var/log/mha/mha1
master_binlog_dir=/usr/local/mysql/data
password=666666
ping_interval=2
repl_password=666666
repl_user=rep
ssh_user=root
user=mha
[server1]
hostname=192.168.17.109
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.17.110
port=3306
[server3]
hostname=192.168.17.111
port=3306
[root@MHA3 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 &
[1] 1653
上面部署MHA我们用的是rpm方式,源码方式部署代码如下
#MHA-Node部署
[root@mysql-db01 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
[root@mysql-db01 ~]# tar xf mha4mysql-node-0.56.tar.gz -C /usr/src/
[root@mysql-db01 ~]# cd /usr/src/mha4mysql-node-0.56/
[root@mysql-db01 mha4mysql-node-0.56]# perl Makefile.PL
[root@mysql-db01 mha4mysql-node-0.56]# make && make install
#MHA-manager部署
[root@mysql-db01 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
[root@mysql-db01 ~]# tar xf mha4mysql-manager-0.56.tar.gz -C /usr/src/
[root@mysql-db01 ~]# cd /usr/src/mha4mysql-manager-0.56/
[root@mysql-db01 mha4mysql-manager-0.56]# perl Makefile.PL
[root@mysql-db01 mha4mysql-manager-0.56]# make && make install