本章内容:
目前 MySQL 已经成为市场上主流数据库之一,考虑到业务的重要性,MySQL 数据库 单点问题已成为企业网站架构中最大的隐患。随着技术的发展,MHA 的出现就是解决 MySQL 单点的问题。另外随着企业数据量越来越庞大,数据库的压力又成为企业的另一个 瓶颈,MySQL 多主多从架构的出现可以减轻 MySQL 本身的压力。本章将主要介绍 MHA 的搭建和模拟 MySQL 故障自动切换的过程
MHA(MasterHigh Availability)目前在 MySQL 高可用方面是一个相对成熟的解决方 案,它由日本 DeNA 公司 youshimaton(现就职于 Facebook 公司)开发,是一套优秀的 MySQL 高可用环境下故障切换和主从复制的软件。在 MySQL 故障切换过程中,MHA 能做 到在 0~30 秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程中,MHA 能在最大程度上保证数据的一致性,以达到真正意义上的高可用。
该软件由两部分组成:MHA Manager(管理节点)和 MHA Node(数据节点)。MHA Manager 可以单独部署在一台独立的机器上,管理多个 master-slave 集群;也可以部署在 一台 slave 节点上。MHA Node 运行在每台 MySQL 服务器上,MHA Manager 会定时探测 集群中的 master 节点。当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 master,然后将所有其他的 slave 重新指向新的 master。整个故障转移过程对应用程序完 全透明。
在 MHA 自动故障切换过程中,MHA 试图从宕机的主服务器上保存二进制日志,最大 程度的保证数据的不丢失,但这并不总是可行的。例如,如果主服务器硬件故障或无法通过SSH 访问,MHA 没法保存二进制日志,就会出现只进行故障转移但丢失了最新的数据的情 况。使用 MySQL 5.5 的半同步复制,可以大大降低数据丢失的风险。MHA 可以与半同步复 制结合起来。如果只有一个 slave 已经收到了最新的二进制日志,MHA 可以将最新的二进 制日志应用于其他所有的 slave 服务器上,因此可以保证所有节点的数据一致性
目前 MHA 主要支持一主多从的架构,要搭建 MHA 要求一个复制集群中必须最少有三 台数据库服务器,即一台充当 master,一台充当备用 master,另外一台充当从库。因为至 少需要三台服务器,出于机器成本的考虑,淘宝在该基础上进行了改造,目前淘宝 TMHA 已经支持一主一从。
上图中 MHA 可以同时监控并管理多个 MySQL 复制组,本案例只实验其中的一个复制 组
本案例要求通过 MHA 监控 MySQL 数据库在故障时进行自动切换,不影响业务。
在三台 mysql 节点上分别安装数据库,MySQL 版本使用 5.7.26, 二进制安装。下面只在 master 上面做演示,其他机器步骤相同,安装过程如下:
[root@master ~]# yum -y install gcc vim wget net-tools lrzsz libaio
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@master ~]# useradd -M -s /sbin/nologin mysql
- [root@master ~]# setenforce 0
- setenforce: SELinux is disabled
- [root@master ~]# systemctl stop firewalld
- [root@master ~]# tar zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
- [root@master ~]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
- [root@master ~]# mkdir /usr/local/mysql/data
- [root@master ~]# chown -R mysql:mysql /usr/local/mysql/data/
- [root@master ~]# cd /usr/local/mysql/bin/
- [root@master bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
-
- 2024-03-15T02:47:24.625118Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
- 2024-03-15T02:47:24.851610Z 0 [Warning] InnoDB: New log files created, LSN=45790
- 2024-03-15T02:47:24.888575Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
- 2024-03-15T02:47:24.950326Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5a9fc7dd-e276-11ee-b06c-000c2910f2b6.
- 2024-03-15T02:47:24.957182Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
- 2024-03-15T02:47:24.958231Z 1 [Note] A temporary password is generated for root@localhost: kAaWdpuIL4&C //密码字段需要保存
2024-03-15T02:47:24.958231Z 1 [Note] A temporary password is generated for root@localhost: kAaWdpuIL4&C //密码字段需要保存
- [root@master bin]# vim /etc/my.cnf
- [mysqld]
- [client]
- socket=/usr/local/mysql/data/mysql.sock
-
- [mysqld]
- socket=/usr/local/mysql/data/mysql.sock
- bind-address = 0.0.0.0
- port = 3306
- basedir=/usr/local/mysql
- datadir=/usr/local/mysql/data
- max_connections=2048
- character-set-server=utf8
- default-storage-engine=INNODB
- [root@master bin]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
- [root@master bin]# . /etc/profile
- [root@master bin]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
- [root@master bin]# chmod +x /etc/rc.d/init.d/mysqld
- [root@master bin]# vim /lib/systemd/system/mysqld.service
-
- [Unit]
- Description=mysqld
- After=network.target
-
- [Service]
- Type=forking
- ExecStart=/etc/rc.d/init.d/mysqld start
- ExecReload=/etc/rc.d/init.d/mysqld reload
- ExecStop=/etc/rc.d/init.d/mysqld stop
-
- [Install]
- WantedBy=multi-user.target
-
-
- [root@master bin]# systemctl daemon-reload
- [root@master bin]# systemctl enable mysqld.service
- [root@master bin]# systemctl start mysqld.service
- [root@master bin]# netstat -anpt | grep 3306
- tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 11941/mysqld
- [root@master bin]#
- [root@master bin]# mysql -u root -p //登录数据库
- Enter password: //输入之前字段中的默认密码
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.7.26
-
- Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
-
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
-
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
- mysql> set password = password('pwd123'); //修改密码为pwd123
- Query OK, 0 rows affected, 1 warning (0.01 sec)
-
- mysql>
- [root@master ~]# vim /etc/my.cnf
-
- server-id = 11
- log_bin = master-bin
- log-slave-updates = true
-
- [root@master ~]# systemctl restart mysqld.service //重启服务
- [root@slave02 ~]# vim /etc/my.cnf
- [root@slave02 ~]# cat /etc/my.cnf
- [mysqld]
- [client]
- socket=/usr/local/mysql/data/mysql.sock
-
- [mysqld]
- socket=/usr/local/mysql/data/mysql.sock
- bind-address = 0.0.0.0
- port = 3306
- basedir=/usr/local/mysql
- datadir=/usr/local/mysql/data
- max_connections=2048
- character-set-server=utf8
- default-storage-engine=INNODB
-
- server-id=33
- relay-log = relay-log-bin
- relay-log-index = slave-relay-bin-index
- log-bin = mysql-bin
- relay_log_purge = 0
-
-
- [root@slave01 ~]# systemctl restart mysqld.service //重启服务
注意:server-d 不能相同,两台slave 服务器的id分别为 22 和 33 其他相同
MySQL 的主从相对比较简单。本章实现配置一个主数据库,两个从数据库。
在所有数据库节点上授权两个用户,一个是从数据库同步使用,另一个是 manager使用
- mysql> grant replication slave on *.* to 'myslave'@'192.168.182.%' identified by 'pwd123';
- Query OK, 0 rows affected, 1 warning (0.01 sec)
-
- mysql> grant all privileges on *.* to 'mha'@'192.168.182.%' identified by 'pwd123';
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql>
下面三条授权按理论是不用添加的,但是在做案例实验时通过 MHA 检查 MySQL 主从 有报错,报两个从库通过主机名连接不上主库,所以三个数据库都添加下面的授权。
- mysql> grant all privileges on *.* to 'mha'@'master' identified by 'pwd123';
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql> grant all privileges on *.* to 'mha'@'slave01' identified by 'pwd123'
- ;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql> grant all privileges on *.* to 'mha'@'slave02' identified by 'pwd123';
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql>
在master 主机上查看二进制文件和同步点
- mysql> change master to master_host='192.168.182.101',master_user='myslave',master_password='pwd123',master_log_file='master-bin.000001',master_log_pos=
- 154;
- Query OK, 0 rows affected, 2 warnings (0.01 sec)
-
- mysql> start slave;
- Query OK, 0 rows affected (0.00 sec)
查看 IO 和SQL 线程都是yes 代表同步是否正常
- mysql> set global read_only=1;
- Query OK, 0 rows affected (0.01 sec)
-
- mysql>
在master 主库创建数据库,测试是否同步
mysql> create database test_db;
两个库分别查询,显示出主库创建的库说明主从同步正常
- [root@manager ~]# yum -y install epel-release
-
- [root@manager ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-ExUtils-CBuilder perl-ExUtils-MakeMaker perl-CPAN
拉取软件包
[root@manager ~]# wget https://github.com/yoshinorim/mha4mysql-node/releases/download/v0.58/mha4mysql-node-0.58.tar.gz
编译安装
- [root@manager ~]# tar zxvf mha4mysql-node-0.58.tar.gz
- [root@manager ~]# cd mha4mysql-node-0.58/
- [root@manager mha4mysql-node-0.58]# perl Makefile.PL
- [root@manager mha4mysql-node-0.58]# make && make install
拉取软件包
[root@manager ~]# wget https://github.com/yoshinorim/mha4mysql-manager/releases/download/v0.58/mha4mysql-manager-0.58.tar.gz
编译安装
- [root@manager ~]# tar zxvf mha4mysql-manager-0.58.tar.gz
- [root@manager ~]# cd mha4mysql-manager-0.58/
- [root@manager mha4mysql-manager-0.58]# perl Makefile.PL
- [root@manager mha4mysql-manager-0.58]# make && make install
manager 安装后在/usr/local/bin 下面会生成几个工具:
4)node 安装后也会在/usr/local/bin 下面会生成几个脚本(这些工具通常由manager 的)
脚本触发,无需认为操作)
- [root@manager ~]# ssh-keygen -t rsa //一只按回车键最后会出现一串密钥
- [root@manager ~]# ssh 192.168.182.104
- [root@manager ~]# ssh 192.168.182.103
- [root@manager ~]# ssh 192.168.182.101
-
- [root@master ~]# ssh-keygen -t rsa
- [root@master ~]# ssh-copy-id 192.168.182.103
- [root@master ~]# ssh-copy-id 192.168.182.104
- [root@slave01 ~]# ssh-keygen -t rsa
- [root@slave01 ~]# ssh-copy-id 192.168.182.101
- [root@slave01 ~]# ssh-copy-id 192.168.182.104
- [root@slave02 ~]# ssh-keygen -t rsa
- [root@slave02 ~]# ssh-copy-id 192.168.182.101
- [root@slave02 ~]# ssh-copy-id 192.168.182.104
- [root@manager ~]# ll mha4mysql-manager-0.58/samples/scripts/
- 总用量 32
- -rwxr-xr-x 1 luo luo 3648 3月 23 2018 master_ip_failover
- -rwxr-xr-x 1 luo luo 9870 3月 23 2018 master_ip_online_change
- -rwxr-xr-x 1 luo luo 11867 3月 23 2018 power_manager
- -rwxr-xr-x 1 luo luo 1360 3月 23 2018 send_report
- [root@manager ~]#
具体脚本作用如下:
[root@manager ~]# cp /root/mha4mysql-manager-0.58/samples/scripts/* /usr/local/bin/
复制“master_ip_failover”脚本到/usr/local/bin 目录,这里使用脚本管理 VIP,也是推荐 的一种方式,生产环境不建议使用 Keepalived。
- [root@manager ~]# vim /usr/local/bin/master_ip_failover
- my $vip = '192.168.182.200/24'; //配置VIP地址
- my $key = '1';
- my $ssh_start_vip = "/usr/sbin/ifconfig ens33:$key $vip up";
- my $ssh_stop_vip = "/usr/sbin/ifconfig ens33:$key down";
- [server default]
- manager_log=/var/log/masterha/app1/manager.log
- manager_workdir=/var/log/masterha/app1
- master_binlog_dir=/usr/local/mysql/data
- master_ip_failover_script=/usr/local/bin/master_ip_failover
- master_ip_online_change_script=/usr/local/bin/master_ip_onilover_change
- user=mha
- password=pwd123
- ping_interval=1
- remote_workdir=/tmp
- repl_user=myslave
- repl_password=pwd123
- secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.182.103 -s 192.168.182.104
- shutdown_script=""
- ssh_user=root
-
- [server1]
- hostname=192.168.182.101
- port=3306
-
- [server2]
- candidate_master=1
- check_repl_delay=0
- hostname=192.168.182.103
- port=3306
-
- [server3]
- hostname=192.168.182.104
- port=3306
candidate_master 与 check_repl_delay 的主要作用如下所示:
- [root@manager ~]# masterha_check_ssh -conf=/etc/masterha/app1.cnf
- Fri Mar 15 15:28:54 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
- Fri Mar 15 15:28:54 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
- Fri Mar 15 15:28:54 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
- Fri Mar 15 15:28:54 2024 - [info] Starting SSH connection tests..
- Fri Mar 15 15:28:56 2024 - [debug]
- Fri Mar 15 15:28:54 2024 - [debug] Connecting via SSH from root@192.168.182.101(192.168.182.101:22) to root@192.168.182.103(192.168.182.103:22)..
- Fri Mar 15 15:28:55 2024 - [debug] ok.
- Fri Mar 15 15:28:55 2024 - [debug] Connecting via SSH from root@192.168.182.101(192.168.182.101:22) to root@192.168.182.104(192.168.182.104:22)..
- Fri Mar 15 15:28:55 2024 - [debug] ok.
- Fri Mar 15 15:28:57 2024 - [debug]
- Fri Mar 15 15:28:55 2024 - [debug] Connecting via SSH from root@192.168.182.103(192.168.182.103:22) to root@192.168.182.101(192.168.182.101:22)..
- Fri Mar 15 15:28:55 2024 - [debug] ok.
- Fri Mar 15 15:28:55 2024 - [debug] Connecting via SSH from root@192.168.182.103(192.168.182.103:22) to root@192.168.182.104(192.168.182.104:22)..
- Fri Mar 15 15:28:56 2024 - [debug] ok.
- Fri Mar 15 15:28:57 2024 - [debug]
- Fri Mar 15 15:28:55 2024 - [debug] Connecting via SSH from root@192.168.182.104(192.168.182.104:22) to root@192.168.182.101(192.168.182.101:22)..
- Fri Mar 15 15:28:56 2024 - [debug] ok.
- Fri Mar 15 15:28:56 2024 - [debug] Connecting via SSH from root@192.168.182.104(192.168.182.104:22) to root@192.168.182.103(192.168.182.103:22)..
- Fri Mar 15 15:28:56 2024 - [debug] ok.
- Fri Mar 15 15:28:57 2024 - [info] All SSH connection tests passed successfully.
- [root@manager ~]#
- [root@manager ~]# masterha_check_repl -conf=/etc/masterha/app1.cnf
- Fri Mar 15 15:33:48 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
- Fri Mar 15 15:33:48 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
- Fri Mar 15 15:33:48 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..
- Fri Mar 15 15:33:48 2024 - [info] MHA::MasterMonitor version 0.58.
- Fri Mar 15 15:33:49 2024 - [info] GTID failover mode = 0
- Fri Mar 15 15:33:49 2024 - [info] Dead Servers:
- Fri Mar 15 15:33:49 2024 - [info] Alive Servers:
- Fri Mar 15 15:33:49 2024 - [info] 192.168.182.101(192.168.182.101:3306)
- Fri Mar 15 15:33:49 2024 - [info] 192.168.182.103(192.168.182.103:3306)
- Fri Mar 15 15:33:49 2024 - [info] 192.168.182.104(192.168.182.104:3306)
- Fri Mar 15 15:33:49 2024 - [info] Alive Slaves:
- Fri Mar 15 15:33:49 2024 - [info] 192.168.182.103(192.168.182.103:3306) Version=5.7.26-log (oldest major version between slaves) log-bin:enabled
- Fri Mar 15 15:33:49 2024 - [info] Replicating from 192.168.182.101(192.168.182.101:3306)
- Fri Mar 15 15:33:49 2024 - [info] Primary candidate for the new Master (candidate_master is set)
- Fri Mar 15 15:33:49 2024 - [info] 192.168.182.104(192.168.182.104:3306) Version=5.7.26-log (oldest major version between slaves) log-bin:enabled
- Fri Mar 15 15:33:49 2024 - [info] Replicating from 192.168.182.101(192.168.182.101:3306)
- Fri Mar 15 15:33:49 2024 - [info] Current Alive Master: 192.168.182.101(192.168.182.101:3306)
- Fri Mar 15 15:33:49 2024 - [info] Checking slave configurations..
- Fri Mar 15 15:33:49 2024 - [info] Checking replication filtering settings..
- Fri Mar 15 15:33:49 2024 - [info] binlog_do_db= , binlog_ignore_db=
- Fri Mar 15 15:33:49 2024 - [info] Replication filtering check ok.
- Fri Mar 15 15:33:49 2024 - [info] GTID (with auto-pos) is not supported
- Fri Mar 15 15:33:49 2024 - [info] Starting SSH connection tests..
- Fri Mar 15 15:33:52 2024 - [info] All SSH connection tests passed successfully.
- Fri Mar 15 15:33:52 2024 - [info] Checking MHA Node version..
- Fri Mar 15 15:33:52 2024 - [info] Version check ok.
- Fri Mar 15 15:33:52 2024 - [info] Checking SSH publickey authentication settings on the current master..
- Fri Mar 15 15:33:53 2024 - [info] HealthCheck: SSH to 192.168.182.101 is reachable.
- Fri Mar 15 15:33:53 2024 - [info] Master MHA Node version is 0.58.
- Fri Mar 15 15:33:53 2024 - [info] Checking recovery script configurations on 192.168.182.101(192.168.182.101:3306)..
- Fri Mar 15 15:33:53 2024 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/usr/local/mysql/data --output_file=/tmp/save_binary_logs_test --manager_version=0.58 --start_file=master-bin.000001
- Fri Mar 15 15:33:53 2024 - [info] Connecting to root@192.168.182.101(192.168.182.101:22)..
- Creating /tmp if not exists.. ok.
- Checking output directory is accessible or not..
- ok.
- Binlog found at /usr/local/mysql/data, up to master-bin.000001
- Fri Mar 15 15:33:53 2024 - [info] Binlog setting check done.
- Fri Mar 15 15:33:53 2024 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
- Fri Mar 15 15:33:53 2024 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.182.103 --slave_ip=192.168.182.103 --slave_port=3306 --workdir=/tmp --target_version=5.7.26-log --manager_version=0.58 --relay_log_info=/usr/local/mysql/data/relay-log.info --relay_dir=/usr/local/mysql/data/ --slave_pass=xxx
- Fri Mar 15 15:33:53 2024 - [info] Connecting to root@192.168.182.103(192.168.182.103:22)..
- Checking slave recovery environment settings..
- Opening /usr/local/mysql/data/relay-log.info ... ok.
- Relay log found at /usr/local/mysql/data, up to relay-log-bin.000002
- Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000002
- Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
- Testing mysql connection and privileges..
- mysql: [Warning] Using a password on the command line interface can be insecure.
- done.
- Testing mysqlbinlog output.. done.
- Cleaning up test file(s).. done.
- Fri Mar 15 15:33:53 2024 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='mha' --slave_host=192.168.182.104 --slave_ip=192.168.182.104 --slave_port=3306 --workdir=/tmp --target_version=5.7.26-log --manager_version=0.58 --relay_log_info=/usr/local/mysql/data/relay-log.info --relay_dir=/usr/local/mysql/data/ --slave_pass=xxx
- Fri Mar 15 15:33:53 2024 - [info] Connecting to root@192.168.182.104(192.168.182.104:22)..
- Checking slave recovery environment settings..
- Opening /usr/local/mysql/data/relay-log.info ... ok.
- Relay log found at /usr/local/mysql/data, up to relay-log-bin.000002
- Temporary relay log file is /usr/local/mysql/data/relay-log-bin.000002
- Checking if super_read_only is defined and turned on.. not present or turned off, ignoring.
- Testing mysql connection and privileges..
- mysql: [Warning] Using a password on the command line interface can be insecure.
- done.
- Testing mysqlbinlog output.. done.
- Cleaning up test file(s).. done.
- Fri Mar 15 15:34:04 2024 - [info] Slaves settings check done.
- Fri Mar 15 15:34:04 2024 - [info]
- 192.168.182.101(192.168.182.101:3306) (current master)
- +--192.168.182.103(192.168.182.103:3306)
- +--192.168.182.104(192.168.182.104:3306)
-
- Fri Mar 15 15:34:04 2024 - [info] Checking replication health on 192.168.182.103..
- Fri Mar 15 15:34:04 2024 - [info] ok.
- Fri Mar 15 15:34:04 2024 - [info] Checking replication health on 192.168.182.104..
- Fri Mar 15 15:34:04 2024 - [info] ok.
- Fri Mar 15 15:34:04 2024 - [info] Checking master_ip_failover_script status:
- Fri Mar 15 15:34:04 2024 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.182.101 --orig_master_ip=192.168.182.101 --orig_master_port=3306
- Fri Mar 15 15:34:04 2024 - [info] OK.
- Fri Mar 15 15:34:04 2024 - [warning] shutdown_script is not defined.
- Fri Mar 15 15:34:04 2024 - [info] Got exit code 0 (Not master dead).
-
- MySQL Replication Health is OK.
- [root@manager ~]#
如果在执行过程中,由如下保存信息:
- Can't exec "mysqlbinlog": No such file or directory at
- /usr/lib64/perl5/vendor_perl/MHA/BinlogManager.pm line 99
- [root@master ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/bin/mysqlbinlo
- [root@master ~]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysqlbinlog
如果出现如下报错信息
- Bareword "FIXME_xxx" not allowed while "strict subs" in use at /usr/local/bin/master_ip_failover line
- 100
- [root@manager ~]# vim /usr/local/bin/master_ip_failover
- ## Update master ip on the catalog database, etc
- # FIXME_xxx; //97行注释
- [root@master ~]# ifconfig ens33:1 192.168.182.200
- [root@master ~]# ifconfig ens33:1
- ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
- inet 192.168.182.200 netmask 255.255.255.0 broadcast 192.168.182.255
- ether 00:0c:29:10:f2:b6 txqueuelen 1000 (Ethernet)
-
- [root@master ~]#
VIP 地址不会应为 manager 节点停止 MHA 服务而消失
[root@manager ~]# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/masterha/app1/manager.log 2>1 &
配置参数具体作用如下:
[root@manager ~]# masterha_stop --conf=/etc/manager/app1.cnf
[root@manager ~]# cat /var/log/masterha/app1/manager.log
- [root@manager ~]# tailf /var/log/masterha/app1/manager.log
- +--192.168.182.104(192.168.182.104:3306)
-
- Fri Mar 15 15:43:56 2024 - [info] Checking master_ip_failover_script status:
- Fri Mar 15 15:43:56 2024 - [info] /usr/local/bin/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.182.101 --orig_master_ip=192.168.182.101 --orig_master_port=3306
- Fri Mar 15 15:43:56 2024 - [info] OK.
- Fri Mar 15 15:43:56 2024 - [warning] shutdown_script is not defined.
- Fri Mar 15 15:43:56 2024 - [info] Set master ping interval 1 seconds.
- Fri Mar 15 15:43:56 2024 - [info] Set secondary check script: /usr/local/bin/masterha_secondary_check -s 192.168.182.103 -s 192.168.182.104
- Fri Mar 15 15:43:56 2024 - [info] Starting ping health check on 192.168.182.101(192.168.182.101:3306)..
- Fri Mar 15 15:43:56 2024 - [info] Ping(SELECT) succeeded, waiting until MySQL doesn't respond..
- [root@master ~]# poweroff
-
- [root@manager ~]# tailf /var/log/masterha/app1/manager.log
-
- #######忽略大部分日志
-
- Started automated(non-interactive) failover.
- Invalidated master IP address on 192.168.182.101(192.168.182.101:3306)
- The latest slave 192.168.182.103(192.168.182.103:3306) has all relay logs for recovery.
- Selected 192.168.182.103(192.168.182.103:3306) as a new master.
- 192.168.182.103(192.168.182.103:3306): OK: Applying all logs succeeded.
- Failed to activate master IP address for 192.168.182.103(192.168.182.103:3306) with return code 10:0
- 192.168.182.104(192.168.182.104:3306): This host has the latest relay log events.
- Generating relay diff files from the latest slave succeeded.
- 192.168.182.104(192.168.182.104:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.182.103(192.168.182.103:3306)
- 192.168.182.103(192.168.182.103:3306): Resetting slave info succeeded.
- Master failover to 192.168.182.103(192.168.182.103:3306) completed successfully.
[root@slave01 ~]# ifconfig
- mysql> show slave status\G
- *************************** 1. row ***************************
- Slave_IO_State: Waiting for master to send event
- Master_Host: 192.168.182.103
- mysql> show master status;
- +------------------+----------+--------------+------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+------------------+-------------------+
- | mysql-bin.000001 | 868 | | | |
- +------------------+----------+--------------+------------------+-------------------+
- 1 row in set (0.00 sec)
-
- mysql>
- [root@master ~]# systemctl start mysqld
- [root@master ~]# mysql -u root -p
- Enter password:
-
- mysql> change master to master_host='192.168.182.103',master_user='myslave',master_password='pwd123',master_log_file='mysql-bin.000001',master_log_pos=868;
- Query OK, 0 rows affected, 2 warnings (0.01 sec)
-
- mysql>
- mysql> start slave;
- Query OK, 0 rows affected (0.01 sec)
-
- mysql> set global read_only=1;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql>
- mysql> stop slave;
- Query OK, 0 rows affected, 1 warning (0.00 sec)
-
- mysql> reset slave;
- Query OK, 0 rows affected (0.00 sec)
-
- mysql>