企业级 MySQL 8 全流程指南:源码编译安装、主从同步、延迟复制、半同步与 MHA 高可用搭建

1.安装依赖

安装mysql8的依赖软件:

bash 复制代码
yum install -y git bison openssl-devel ncurses-devel -y

安装cmake3:

解压:

bash 复制代码
tar xzf cmake3.tar.gz

来到make3文件夹:

bash 复制代码
 cd make3/

将下面的rpm包全部安装:

bash 复制代码
yum install *.rpm

查看安装版本:

bash 复制代码
cmake3 -version

安装gcc-11:

解压:

bash 复制代码
unzip gcc-11.zip

来到解压好的目录:

bash 复制代码
cd gcc-11/

将里面的rpm包全部安装:

bash 复制代码
yum install *.rpm
bash 复制代码
mkdir build

启动gcc并设置开机启动:

bash 复制代码
source /opt/rh/devtoolset-11/enable 
cat /opt/rh/devtoolset-11/enable >> ~/.bash_profile

查看版本:

bash 复制代码
gcc --version

解压mysql8源码包:

bash 复制代码
tar xzf mysql-boost-8.3.0.tar.gz 

进入目录:

bash 复制代码
cd mysql-8.3.0/

创建编译目录,防止编译污染目录环境:

bash 复制代码
mkdir build

进入编译目录进行编译:

bash 复制代码
cd build/

进行编译:

bash 复制代码
cmake3 .. 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 
-DMYSQL_DATADIR=/data/mysql 
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_EXTRA_CHARSETS=all 
-DDEFAULT_CHARSET=utf8mb4 
-DDEFAULT_COLLATION=utf8mb4_unicode_ci 
-DWITH_SSL=system 
-DWITH_BOOST=bundled 
-DWITH_DEBUG=OFF

#- j4 表示有几个核心就跑 几个进程

bash 复制代码
make -j4

完成如下图:

安装:

bash 复制代码
make install

2.部署mysql:

#生成启动脚本:

bash 复制代码
cd /usr/local/mysql/support-files/
bash 复制代码
cp -p mysql.server /etc/init.d/mysqld

#修改环境变量:

bash 复制代码
vim ~/.bash_profile
bash 复制代码
export PATH=$PATH:/usr/local/mysql/bin
bash 复制代码
source ~/.bash_profile

#建立数据库程序运行用户:

bash 复制代码
useradd -M -s /sbin/nologin mysql

#建立数据库数据目录:

bash 复制代码
cd ~
mkdir -p /data/mysql/
bash 复制代码
chown mysql.mysql /data/mysql/

#生成配置文件:

bash 复制代码
vim /etc/my.cnf
bash 复制代码
[mysqld]
server-id=10
datadir=/data/mysql
socket=/data/mysql/mysql.sock
default_authentication_plugin=mysql_native_password

#数据库初始化建立mysql基本数据:

bash 复制代码
 mysqld --initialize --user=mysql

将密码记住!!如下图圈起来的地方所示:

bash 复制代码
chkconfig mysqld on

#数据库安全初始化:

bash 复制代码
mysql_secure_installation

Securing the MySQL server deployment.Enter password for user root: #输入当前密码

The existing password for the user account root has expired. Please set a new

password.

New password: #输入新密码

Re-enter new password: #重复密码

VALIDATE PASSWORD PLUGIN can be used to test passwords

and improve security. It checks the strength of password

and allows the users to set only those passwords which are

secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: no #是否启用密码插件

Using existing password for root.

Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

#是否要重置密码

... skipping.

By default, a MySQL installation has an anonymous user,

allowing anyone to log into MySQL without having to have

a user account created for them. This is intended only for

testing, and to make the installation go a bit smoother.

You should remove them before moving into a production

environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #回车

Success.

Normally, root should only be allowed to connect from

'localhost'. This ensures that someone cannot guess at

the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y #回车

Success.

By default, MySQL comes with a database named 'test' that

anyone can access. This is also intended only for testing,

and should be removed before moving into a production

environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No)

: y #回车

  • Dropping test database...

Success.

  • Removing privileges on test database...

Success.

Reloading the privilege tables will ensure that all changes

made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #回车

Success.

测试:

mysql -uroot -p密码

bash 复制代码
mysql -uroot -p123

3.mysql安装排错:

如果上面的实验步骤出错,想要重新部署:

第一步先将mysql停止:

bash 复制代码
/etc/init.d/mysqld stop

stop运行不了就直接kill掉进程:

先查看mysql进程号:

bash 复制代码
netstat -antplue | grep mysql
bash 复制代码
kill -9 31477

将进程删除后将前面建立的/data/mysql文件夹清空,重新部署:

bash 复制代码
rm -rf /data/mysql/*

再重新进行数据库初始化:

bash 复制代码
mysqld --initialize --user=mysql

将新密码记住:

启动mysql服务:

bash 复制代码
/etc/init.d/mysqld start

mysqld 服务设置为系统开机自动启动

bash 复制代码
chkconfig mysqld on

进行数据库安全初始化:

bash 复制代码
mysql_secure_installation

Securing the MySQL server deployment.Enter password for user root: #输入当前密码

The existing password for the user account root has expired. Please set a new

password.

New password: #输入新密码

Re-enter new password: #重复密码

VALIDATE PASSWORD PLUGIN can be used to test passwords

and improve security. It checks the strength of password

and allows the users to set only those passwords which are

secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: no #是否启用密码插件

Using existing password for root.

Change the password for root ? ((Press y|Y for Yes, any other key for No) : no

#是否要重置密码

... skipping.

By default, a MySQL installation has an anonymous user,

allowing anyone to log into MySQL without having to have

a user account created for them. This is intended only for

testing, and to make the installation go a bit smoother.

You should remove them before moving into a production

environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #回车

Success.

Normally, root should only be allowed to connect from

'localhost'. This ensures that someone cannot guess at

the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y #回车

Success.

By default, MySQL comes with a database named 'test' that

anyone can access. This is also intended only for testing,

and should be removed before moving into a production

environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No)

: y #回车

  • Dropping test database...

Success.

  • Removing privileges on test database...

Success.

Reloading the privilege tables will ensure that all changes

made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #回车

Success.

登录mysql:

bash 复制代码
mysql -uroot -p123

4.mysql 的主从复制

4.1配置master:

bash 复制代码
vim /etc/my.cnf
bash 复制代码
[mysqld]
server-id=10
datadir=/data/mysql
socket=/data/mysql/mysql.sock
default_authentication_plugin=mysql_native_password
symbolic-links=0
log-bin=mysql-bin

重启mysql服务:

bash 复制代码
/etc/init.d/mysqld restart

#进入数据库配置用户权限:

bash 复制代码
mysql -p123

#生成专门用来做复制的用户,此用户是用于slave端做认证用

sql 复制代码
CREATE USER rin@'%' IDENTIFIED WITH mysql_native_password BY 'rin';

#对这个用户进行授权

sql 复制代码
GRANT REPLICATION SLAVE ON *.* TO 'rin'@'%';

#查看master的状态

sql 复制代码
SHOW MASTER STATUS;

##查看二进制日志

bash 复制代码
cd /data/mysql/
mysqlbinlog mysql-bin.000001 -vv

4.2配置salve:

4.2.1对从库安装mysql

(如果从库已经安装就跳过)

让从库的目录结构与主库一致,先进行目录创建:

bash 复制代码
mkdir -p /usr/local/mysql
mkdir -p /data/mysql

将主库编译好的mysql文件复制过来:

bash 复制代码
scp -r root@172.25.254.10:/usr/local/mysql/* /usr/local/mysql/

生成启动脚本:

bash 复制代码
cd /usr/local/mysql/support-files/
cp -p mysql.server /etc/init.d/mysqld

修改环境变量:

bash 复制代码
vim ~/.bash_profile 
bash 复制代码
export PATH=$PATH:/usr/local/mysql/bin

重新加载环境配置:

bash 复制代码
source ~/.bash_profile

建立数据库程序运行用户:

bash 复制代码
useradd -M -s /sbin/nologin mysql

对数据库用给予权限:

bash 复制代码
chown mysql.mysql /data/mysql/

生成配置文件:

bash 复制代码
vim /etc/my.cnf
bash 复制代码
[mysqld]
server-id=20
datadir=/data/mysql
socket=/data/mysql/mysql.sock
default_authentication_plugin=mysql_native_password
symbolic-links=0
log-bin=mysql-bin

数据库初始化建立mysql基本数据:

bash 复制代码
mysqld --initialize --user=mysql

记住密码:

启动mysql:

bash 复制代码
/etc/init.d/mysqld start

mysqld 服务设置为系统开机自动启动

bash 复制代码
chkconfig mysqld on

数据库安全初始化:

bash 复制代码
mysql_secure_installation

4.2.2对从库进行主从配置:

编辑配置文件

bash 复制代码
vim /etc/my.cnf

配置内容如下:

bash 复制代码
[mysqld]
server-id=20
datadir=/data/mysql
socket=/data/mysql/mysql.sock
default_authentication_plugin=mysql_native_password
symbolic-links=0
log-bin=mysql-bin

重启服务:

bash 复制代码
/etc/init.d/mysqld restart

登录mysql:

bash 复制代码
 mysql -uroot -p
sql 复制代码
-- 方法1:单行书写(推荐,避免换行问题)
CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='rin', MASTER_PASSWORD='rin', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=691;

-- 方法2:换行时确保关键字完整
CHANGE MASTER TO
  MASTER_HOST='172.25.254.10',
  MASTER_USER='rin',
  MASTER_PASSWORD='rin',
  MASTER_LOG_FILE='mysql-bin.000001',
  MASTER_LOG_POS=691;

启动从库复制进程

sql 复制代码
START SLAVE;

查看主从服务是否配置成功:

sql 复制代码
 SHOW SLAVE STATUS\G

Slave_IO_Running: YesSlave_SQL_Running: Yes,则成功。

测试:

在主库建立数据库并且建表插入数据,查看从库是否同步:

sql 复制代码
CREATE DATABASE rin_2;
SELECT * FROM rin_2.userlist;
INSERT INTO rin_2.userlist VALUE ('rin','123');
show databases;

在从库进行查看:

sql 复制代码
SELECT * FROM rin_2.userlist;

4.3当有数据时添加salve2:

bash 复制代码
vim /etc/my.cnf
bash 复制代码
[mysqld]
server-id=30
datadir=/data/mysql
socket=/data/mysql/mysql.sock
default_authentication_plugin=mysql_native_password
symbolic-links=0
log-bin=mysql-bin

重启服务:

bash 复制代码
/etc/init.d/mysqld restart

在master节点(主库)备份数据:

!NOTE

生产环境中备份时需要锁表,保证备份前后的数据一致

mysql> FLUSH TABLES WITH READ LOCK;

备份后再解锁

mysql> UNLOCK TABLES;

mysqldump命令备份的数据文件,在还原时先DROP TABLE,需要合并数据时需要删除此语句

-- Table structure for table `userlist`

--

DROP TABLE IF EXISTS `userlist`; #需要合并数据时需要删除此语句

/*!40101 SET @saved_cs_client = @@character_set_client */;

/*!40101 SET character_set_client = utf8 */;

sql 复制代码
show databases;

进行备份:

bash 复制代码
mysqldump -uroot -p123 rin_2 > rin_2.sql

将备份好的sql文件传到salve(从库):

bash 复制代码
scp -r root@172.25.254.10:~/rin_2.sql .

#利用master节点中备份出来的lee.sql在slave2中拉平数据

bash 复制代码
mysql -uroot -p123 -e "create database rin_2;"
mysql -uroot -p123 rin_2 < rin_2.sql
mysql -uroot -p123 -e "select * from rin_2.userlist;"

测试:

在主库插入内容:

bash 复制代码
mysql -uroot -p123 -e "INSERT INTO rin_2.userlist values('user9','123');"

分别查看插入内容:

主库:

bash 复制代码
mysql -uroot -p123 -e "select * from rin_2.userlist;"

从库:

bash 复制代码
mysql -uroot -p123 -e "select * from rin_2.userlist;"

5.延迟复制:

延迟复制时用来控制sql线程的,和i/o线程无关

这个延迟复制不是i/o线程过段时间来复制,i/o是正常工作的

是日志已经保存在slave端了,那个sql要等多久进行回放

#在slave端

mysql> STOP SLAVE SQL_THREAD;

mysql> CHANGE MASTER TO MASTER_DELAY=60;

mysql> START SLAVE SQL_THREAD;

mysql> SHOW SLAVE STATUS\G;

Master_Server_Id: 1

Master_UUID: db2d8c92-4dc2-11ef-b6b0-000c299355ea

Master_Info_File: /data/mysql/master.info

SQL_Delay: 60 ##延迟效果

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more

updates

Master_Retry_Count: 86400

测试:

在master中写入数据后过了延迟时间才能被查询到

6.半同步模式:

6.1gtid模式:

bash 复制代码
vim /etc/my.cnf

重启服务:

bash 复制代码
/etc/init.d/mysqld restart

停止slave端:

bash 复制代码
mysql -uroot -p123
sql 复制代码
stop slave;

开启slave端的gtid:

sql 复制代码
CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='root',MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;
sql 复制代码
start slave;
sql 复制代码
show slave status\G;

开启成功如图:

6.2启用半同步模式:

6.2.1在master端配置开启半同步功能:

配置文件:

bash 复制代码
 vim /etc/my.cnf
bash 复制代码
rpl_semi_sync_master_enabled=1

安装半同步插件:

bash 复制代码
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

查看插件情况:

sql 复制代码
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%';

打开半同步功能:

bash 复制代码
SET GLOBAL rpl_semi_sync_master_enabled = 1;

查看半同步功能:

bash 复制代码
SHOW VARIABLES LIKE 'rpl_semi_sync%';
bash 复制代码
SHOW STATUS LIKE 'Rpl_semi_sync%';

6.2.2在slave端配置开启半同步功能:

bash 复制代码
vim /etc/my.cnf
bash 复制代码
rpl_semi_sync_slave_enabled=1
sql 复制代码
mysql -uroot -p123
INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
SET GLOBAL rpl_semi_sync_slave_enabled =1;
STOP SLAVE IO_THREAD;
START SLAVE IO_THREAD;

查看开启状况:

sql 复制代码
SHOW VARIABLES LIKE 'rpl_semi_sync%';
SHOW STATUS LIKE 'Rpl_semi_sync%';

测试:

sql 复制代码
插入三次数据:
insert into rin_2.userlist values ('user455','123');
insert into rin_2.userlist values ('user45','123');
insert into rin_2.userlist values ('user456','123');

查看:

sql 复制代码
SHOW STATUS LIKE 'Rpl_semi_sync%';

模拟故障情况:

将两个slave端都关闭,

sql 复制代码
STOP SLAVE IO_THREAD;

测试插入数据:

sql 复制代码
insert into rin_2.userlist values ('user5','555');

再将slave端全部开启:

bash 复制代码
START SLAVE IO_THREAD;

测试插入数据:

bash 复制代码
insert into rin_2.userlist values ('user5','555');

7.配置MHA管理环境:

类别 主库(Server1) 候选从库(Server2) 从库(Server3) MHA 管理节点
节点标识 [server1] [server2] [server3] -
IP 地址 172.25.254.10 172.25.254.20 172.25.254.30 与实验节点同网段(如复用 Server1/2/3)
主机名 172.25.254.10(IP 直接配置) 172.25.254.20(IP 直接配置) 172.25.254.30(修正后 IP) -
MySQL 角色 主库(写入节点) 从库 + 候选主库(故障时优先切换) 从库(非候选主库) 无 MySQL 服务(仅运行 MHA 管理器)
核心配置参数 candidate_master=1 check_repl_delay=0 candidate_master=1 check_repl_delay=0 no_master=1 -
MySQL 账号 用户名:root 密码:123 用户名:root 密码:123 用户名:root 密码:123 -
复制配置 复制用户:rin 密码:rin binlog 目录:/data/mysql 复制用户:rin 密码:rin 同步源:172.25.254.10 复制用户:rin 密码:rin 同步源:172.25.254.10 -
SSH 配置 SSH 用户:root 与所有节点免密互信 SSH 用户:root 与所有节点免密互信 SSH 用户:root 与所有节点免密互信 SSH 用户:root 与所有 MySQL 节点免密互信

7.1关于软件安装:

在MHA节点:

安装MHA需要的软件:

bash 复制代码
unzip MHA-7.zip
cd ./MHA-7/
yum install ./*.rpm

将 mha4mysql-node-0.58-0.el7.centos.noarch.rpm 包传到各个服务器节点上:

bash 复制代码
scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.10:/mnt
scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.20:/mnt
scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.30:/mnt
bash 复制代码
yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y
yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y

mha配置:

7.2所有节点配置如下:

以下为所有节点通用配置:

bash 复制代码
/etc/init.d/mysqld stop
bash 复制代码
rm -fr /data/mysql/*

编辑配置文件内容:

bash 复制代码
vim /etc/my.cnf

内容如下:

bash 复制代码
[mysqld]
server-id=10
datadir=/data/mysql
socket=/data/mysql/mysql.sock
default_authentication_plugin=mysql_native_password
log-bin=binlog
gtid_mode=ON
skip-name-resolve
binlog_format=ROW
enforce-gtid-consistency=ON
bash 复制代码
mysqld --user mysql --initialize
bash 复制代码
/etc/init.d/mysqld start
bash 复制代码
mysql_secure_installation

7.2.1master:

master节点单独配置:

sql 复制代码
mysql> CREATE USER IF NOT EXISTS 'repl_rin'@'%' IDENTIFIED BY 'rin';
Query OK, 0 rows affected (10.02 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl_rin'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)


mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> SET GLOBAL sql_slave_skip_counter = 1;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
Query OK, 0 rows affected (0.00 sec)

7.2.2slave:

slave节点单独配置:

sql 复制代码
mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='repl_rin', MASTER_PASSWORD='rin', MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 7 warnings (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.06 sec)

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
Query OK, 0 rows affected (0.00 sec)

mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Rpl_semi_sync_slave_status | ON    |
+----------------------------+-------+
1 row in set (0.01 sec)

mysql> SET GLOBAL sql_slave_skip_counter = 1;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

ssl免密认证:

bash 复制代码
 ssh-keygen -t rsa

ssh-copy-id -i ~/.ssh/id_rsa.pub root@其余节点IP

查看从库节点状况:

bash 复制代码
show slave status\G;
sql 复制代码
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.10
                  Master_User: repl_rin
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 1654
               Relay_Log_File: mysql-b-relay-bin.000003
                Relay_Log_Pos: 451
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1654
              Relay_Log_Space: 2370
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 10
                  Master_UUID: 506a67d6-7129-11f0-8460-000c29b1341f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 506a67d6-7129-11f0-8460-000c29b1341f:1-6
            Executed_Gtid_Set: 506a67d6-7129-11f0-8460-000c29b1341f:1-6,
528ca525-7129-11f0-8ea4-000c2923893f:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

ERROR: 
No query specified

7.2.3mha中:

mha节点单独配置:

配置MHA 的管理环境:

bash 复制代码
[root@mysql-mha ~]# mkdir  /etc/masterha
[root@mysql-mha MHA-7]# tar zxf mha4mysql-manager-0.58.tar.gz
[root@mysql-mha MHA-7]# cd  mha4mysql-manager-0.58/samples/conf/
[root@mysql-mha conf]# cat masterha_default.cnf app1.cnf  > /etc/masterha/app1.cnf
bash 复制代码
[root@MHA ~]# vim /etc/masterha/app1.cnf
[root@MHA ~]# cat /etc/masterha/app1.cnf
[server default]
user=root
password=123
ssh_user=root
repl_user=repl_rin
repl_password=rin
master_binlog_dir= /data/mysql
remote_workdir=/tmp
secondary_check_script= masterha_secondary_check -s 172.25.254.10 -s 172.25.254.30
ping_interval=3
# master_ip_failover_script= /script/masterha/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
# master_ip_online_change_script= /script/masterha/master_ip_online_change
[server default]
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/manager.log

[server1]
hostname=172.25.254.10
candidate_master=1
check_repl_delay=0

[server2]
hostname=172.25.254.20
candidate_master=1
check_repl_delay=0

[server3]
hostname=172.25.254.30
no_master=1

允许root用户远程登录:

sql 复制代码
mysql> CREATE USER IF NOT EXISTS root@'%' identified by '123';
Query OK, 0 rows affected (0.01 sec)

mysql> grant ALL ON *.* TO root@'%';
Query OK, 0 rows affected (0.01 sec)

7.3检测:

在MHA上进行

检测网络以及ssh免密:

bash 复制代码
masterha_check_ssh  --conf=/etc/masterha/app1.cnf
bash 复制代码
[root@MHA ~]# masterha_check_ssh  --conf=/etc/masterha/app1.cnf
Mon Aug  4 20:12:40 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 20:12:40 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 20:12:40 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 20:12:40 2025 - [info] Starting SSH connection tests..
Mon Aug  4 20:12:41 2025 - [debug] 
Mon Aug  4 20:12:40 2025 - [debug]  Connecting via SSH from root@172.25.254.10(172.25.254.10:22) to root@172.25.254.20(172.25.254.20:22)..
Mon Aug  4 20:12:41 2025 - [debug]   ok.
Mon Aug  4 20:12:41 2025 - [debug]  Connecting via SSH from root@172.25.254.10(172.25.254.10:22) to root@172.25.254.30(172.25.254.30:22)..
Mon Aug  4 20:12:41 2025 - [debug]   ok.
Mon Aug  4 20:12:42 2025 - [debug] 
Mon Aug  4 20:12:41 2025 - [debug]  Connecting via SSH from root@172.25.254.20(172.25.254.20:22) to root@172.25.254.10(172.25.254.10:22)..
Mon Aug  4 20:12:41 2025 - [debug]   ok.
Mon Aug  4 20:12:41 2025 - [debug]  Connecting via SSH from root@172.25.254.20(172.25.254.20:22) to root@172.25.254.30(172.25.254.30:22)..
Mon Aug  4 20:12:42 2025 - [debug]   ok.
Mon Aug  4 20:12:43 2025 - [debug] 
Mon Aug  4 20:12:41 2025 - [debug]  Connecting via SSH from root@172.25.254.30(172.25.254.30:22) to root@172.25.254.10(172.25.254.10:22)..
Mon Aug  4 20:12:42 2025 - [debug]   ok.
Mon Aug  4 20:12:42 2025 - [debug]  Connecting via SSH from root@172.25.254.30(172.25.254.30:22) to root@172.25.254.20(172.25.254.20:22)..
Mon Aug  4 20:12:42 2025 - [debug]   ok.
Mon Aug  4 20:12:43 2025 - [info] All SSH connection tests passed successfully.

检测数据主从复制情况:

bash 复制代码
masterha_check_repl --conf=/etc/masterha/app1.cnf
bash 复制代码
[root@MHA ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Mon Aug  4 20:14:23 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 20:14:23 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 20:14:23 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 20:14:23 2025 - [info] MHA::MasterMonitor version 0.58.
Mon Aug  4 20:14:24 2025 - [info] GTID failover mode = 1
Mon Aug  4 20:14:24 2025 - [info] Dead Servers:
Mon Aug  4 20:14:24 2025 - [info] Alive Servers:
Mon Aug  4 20:14:24 2025 - [info]   172.25.254.10(172.25.254.10:3306)
Mon Aug  4 20:14:24 2025 - [info]   172.25.254.20(172.25.254.20:3306)
Mon Aug  4 20:14:24 2025 - [info]   172.25.254.30(172.25.254.30:3306)
Mon Aug  4 20:14:24 2025 - [info] Alive Slaves:
Mon Aug  4 20:14:24 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 20:14:24 2025 - [info]     GTID ON
Mon Aug  4 20:14:24 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 20:14:24 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 20:14:24 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 20:14:24 2025 - [info]     GTID ON
Mon Aug  4 20:14:24 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 20:14:24 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 20:14:24 2025 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 20:14:24 2025 - [info] Checking slave configurations..
Mon Aug  4 20:14:24 2025 - [info]  read_only=1 is not set on slave 172.25.254.20(172.25.254.20:3306).
Mon Aug  4 20:14:24 2025 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).
Mon Aug  4 20:14:24 2025 - [info] Checking replication filtering settings..
Mon Aug  4 20:14:24 2025 - [info]  binlog_do_db= , binlog_ignore_db= 
Mon Aug  4 20:14:24 2025 - [info]  Replication filtering check ok.
Mon Aug  4 20:14:24 2025 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Aug  4 20:14:24 2025 - [info] Checking SSH publickey authentication settings on the current master..
Mon Aug  4 20:14:25 2025 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.
Mon Aug  4 20:14:25 2025 - [info] 
172.25.254.10(172.25.254.10:3306) (current master)
 +--172.25.254.20(172.25.254.20:3306)
 +--172.25.254.30(172.25.254.30:3306)

Mon Aug  4 20:14:25 2025 - [info] Checking replication health on 172.25.254.20..
Mon Aug  4 20:14:25 2025 - [info]  ok.
Mon Aug  4 20:14:25 2025 - [info] Checking replication health on 172.25.254.30..
Mon Aug  4 20:14:25 2025 - [info]  ok.
Mon Aug  4 20:14:25 2025 - [warning] master_ip_failover_script is not defined.
Mon Aug  4 20:14:25 2025 - [warning] shutdown_script is not defined.
Mon Aug  4 20:14:25 2025 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

7.3MHA的故障切换:

7.3.1未出现故障时手动切换master:

检测当前的集群状况:

bash 复制代码
 masterha_check_repl --conf=/etc/masterha/app1.cnf

可以看到,当前的集群状况,10为master,20为备选master,30不参与主库竞选,

bash 复制代码
[root@MHA ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Mon Aug  4 21:27:17 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 21:27:17 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:27:17 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:27:17 2025 - [info] MHA::MasterMonitor version 0.58.
Mon Aug  4 21:27:18 2025 - [info] GTID failover mode = 1
Mon Aug  4 21:27:18 2025 - [info] Dead Servers:
Mon Aug  4 21:27:18 2025 - [info] Alive Servers:
Mon Aug  4 21:27:18 2025 - [info]   172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:27:18 2025 - [info]   172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:27:18 2025 - [info]   172.25.254.30(172.25.254.30:3306)
Mon Aug  4 21:27:18 2025 - [info] Alive Slaves:
Mon Aug  4 21:27:18 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:27:18 2025 - [info]     GTID ON
Mon Aug  4 21:27:18 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:27:18 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:27:18 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:27:18 2025 - [info]     GTID ON
Mon Aug  4 21:27:18 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:27:18 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 21:27:18 2025 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:27:18 2025 - [info] Checking slave configurations..
Mon Aug  4 21:27:18 2025 - [info]  read_only=1 is not set on slave 172.25.254.20(172.25.254.20:3306).
Mon Aug  4 21:27:18 2025 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).
Mon Aug  4 21:27:18 2025 - [info] Checking replication filtering settings..
Mon Aug  4 21:27:18 2025 - [info]  binlog_do_db= , binlog_ignore_db= 
Mon Aug  4 21:27:18 2025 - [info]  Replication filtering check ok.
Mon Aug  4 21:27:18 2025 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Aug  4 21:27:18 2025 - [info] Checking SSH publickey authentication settings on the current master..
Mon Aug  4 21:27:18 2025 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.
Mon Aug  4 21:27:18 2025 - [info] 
172.25.254.10(172.25.254.10:3306) (current master)
 +--172.25.254.20(172.25.254.20:3306)
 +--172.25.254.30(172.25.254.30:3306)

Mon Aug  4 21:27:18 2025 - [info] Checking replication health on 172.25.254.20..
Mon Aug  4 21:27:18 2025 - [info]  ok.
Mon Aug  4 21:27:18 2025 - [info] Checking replication health on 172.25.254.30..
Mon Aug  4 21:27:18 2025 - [info]  ok.
Mon Aug  4 21:27:18 2025 - [warning] master_ip_failover_script is not defined.
Mon Aug  4 21:27:18 2025 - [warning] shutdown_script is not defined.
Mon Aug  4 21:27:18 2025 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

手动切换master:

bash 复制代码
masterha_master_switch 
--conf=/etc/masterha/app1.cnf 
--master_state=alive 
--new_master_host=172.25.254.20 
--new_master_port=3306 
--orig_master_is_new_slave 
--running_updates_limit=10000

切换过程如下:
三次询问均为yes:

bash 复制代码
[root@MHA ~]# masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=172.25.254.20 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000
Mon Aug  4 21:34:39 2025 - [info] MHA::MasterRotate version 0.58.
Mon Aug  4 21:34:39 2025 - [info] Starting online master switch..
Mon Aug  4 21:34:39 2025 - [info] 
Mon Aug  4 21:34:39 2025 - [info] * Phase 1: Configuration Check Phase..
Mon Aug  4 21:34:39 2025 - [info] 
Mon Aug  4 21:34:39 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 21:34:39 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:34:39 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:34:40 2025 - [info] GTID failover mode = 1
Mon Aug  4 21:34:40 2025 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:34:40 2025 - [info] Alive Slaves:
Mon Aug  4 21:34:40 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:34:40 2025 - [info]     GTID ON
Mon Aug  4 21:34:40 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:34:40 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:34:40 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:34:40 2025 - [info]     GTID ON
Mon Aug  4 21:34:40 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:34:40 2025 - [info]     Not candidate for the new Master (no_master is set)

It is better to execute FLUSH NO_WRITE_TO_BINLOG TABLES on the master before switching. Is it ok to execute on 172.25.254.10(172.25.254.10:3306)? (YES/no): yes
Mon Aug  4 21:34:42 2025 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Mon Aug  4 21:34:42 2025 - [info]  ok.
Mon Aug  4 21:34:42 2025 - [info] Checking MHA is not monitoring or doing failover..
Mon Aug  4 21:34:42 2025 - [info] Checking replication health on 172.25.254.20..
Mon Aug  4 21:34:42 2025 - [info]  ok.
Mon Aug  4 21:34:42 2025 - [info] Checking replication health on 172.25.254.30..
Mon Aug  4 21:34:42 2025 - [info]  ok.
Mon Aug  4 21:34:42 2025 - [info] 172.25.254.20 can be new master.
Mon Aug  4 21:34:42 2025 - [info] 
From:
172.25.254.10(172.25.254.10:3306) (current master)
 +--172.25.254.20(172.25.254.20:3306)
 +--172.25.254.30(172.25.254.30:3306)

To:
172.25.254.20(172.25.254.20:3306) (new master)
 +--172.25.254.30(172.25.254.30:3306)
 +--172.25.254.10(172.25.254.10:3306)

Starting master switch from 172.25.254.10(172.25.254.10:3306) to 172.25.254.20(172.25.254.20:3306)? (yes/NO): yes
Mon Aug  4 21:34:44 2025 - [info] Checking whether 172.25.254.20(172.25.254.20:3306) is ok for the new master..
Mon Aug  4 21:34:44 2025 - [info]  ok.
Mon Aug  4 21:34:44 2025 - [info] 172.25.254.10(172.25.254.10:3306): SHOW SLAVE STATUS returned empty result. To check replication filtering rules, temporarily executing CHANGE MASTER to a dummy host.
Mon Aug  4 21:34:44 2025 - [info] 172.25.254.10(172.25.254.10:3306): Resetting slave pointing to the dummy host.
Mon Aug  4 21:34:44 2025 - [info] ** Phase 1: Configuration Check Phase completed.
Mon Aug  4 21:34:44 2025 - [info] 
Mon Aug  4 21:34:44 2025 - [info] * Phase 2: Rejecting updates Phase..
Mon Aug  4 21:34:44 2025 - [info] 
master_ip_online_change_script is not defined. If you do not disable writes on the current master manually, applications keep writing on the current master. Is it ok to proceed? (yes/NO): yes
Mon Aug  4 21:34:58 2025 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Mon Aug  4 21:34:58 2025 - [info] Executing FLUSH TABLES WITH READ LOCK..
Mon Aug  4 21:34:58 2025 - [info]  ok.
Mon Aug  4 21:34:58 2025 - [info] Orig master binlog:pos is binlog.000002:2168.
Mon Aug  4 21:34:58 2025 - [info]  Waiting to execute all relay logs on 172.25.254.20(172.25.254.20:3306)..
Mon Aug  4 21:34:58 2025 - [info]  master_pos_wait(binlog.000002:2168) completed on 172.25.254.20(172.25.254.20:3306). Executed 0 events.
Mon Aug  4 21:34:58 2025 - [info]   done.
Mon Aug  4 21:34:58 2025 - [info] Getting new master's binlog name and position..
Mon Aug  4 21:34:58 2025 - [info]  binlog.000002:2514
Mon Aug  4 21:34:58 2025 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.25.254.20', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl_rin', MASTER_PASSWORD='xxx';
Mon Aug  4 21:34:58 2025 - [info] 
Mon Aug  4 21:34:58 2025 - [info] * Switching slaves in parallel..
Mon Aug  4 21:34:58 2025 - [info] 
Mon Aug  4 21:34:58 2025 - [info] -- Slave switch on host 172.25.254.30(172.25.254.30:3306) started, pid: 4410
Mon Aug  4 21:34:58 2025 - [info] 
Mon Aug  4 21:34:59 2025 - [info] Log messages from 172.25.254.30 ...
Mon Aug  4 21:34:59 2025 - [info] 
Mon Aug  4 21:34:58 2025 - [info]  Waiting to execute all relay logs on 172.25.254.30(172.25.254.30:3306)..
Mon Aug  4 21:34:58 2025 - [info]  master_pos_wait(binlog.000002:2168) completed on 172.25.254.30(172.25.254.30:3306). Executed 0 events.
Mon Aug  4 21:34:58 2025 - [info]   done.
Mon Aug  4 21:34:58 2025 - [info]  Resetting slave 172.25.254.30(172.25.254.30:3306) and starting replication from the new master 172.25.254.20(172.25.254.20:3306)..
Mon Aug  4 21:34:58 2025 - [info]  Executed CHANGE MASTER.
Mon Aug  4 21:34:58 2025 - [info]  Slave started.
Mon Aug  4 21:34:59 2025 - [info] End of log messages from 172.25.254.30 ...
Mon Aug  4 21:34:59 2025 - [info] 
Mon Aug  4 21:34:59 2025 - [info] -- Slave switch on host 172.25.254.30(172.25.254.30:3306) succeeded.
Mon Aug  4 21:34:59 2025 - [info] Unlocking all tables on the orig master:
Mon Aug  4 21:34:59 2025 - [info] Executing UNLOCK TABLES..
Mon Aug  4 21:34:59 2025 - [info]  ok.
Mon Aug  4 21:34:59 2025 - [info] Starting orig master as a new slave..
Mon Aug  4 21:34:59 2025 - [info]  Resetting slave 172.25.254.10(172.25.254.10:3306) and starting replication from the new master 172.25.254.20(172.25.254.20:3306)..
Mon Aug  4 21:34:59 2025 - [info]  Executed CHANGE MASTER.
Mon Aug  4 21:34:59 2025 - [info]  Slave started.
Mon Aug  4 21:34:59 2025 - [info] All new slave servers switched successfully.
Mon Aug  4 21:34:59 2025 - [info] 
Mon Aug  4 21:34:59 2025 - [info] * Phase 5: New master cleanup phase..
Mon Aug  4 21:34:59 2025 - [info] 
Mon Aug  4 21:34:59 2025 - [info]  172.25.254.20: Resetting slave info succeeded.
Mon Aug  4 21:34:59 2025 - [info] Switching master to 172.25.254.20(172.25.254.20:3306) completed successfully.

再次进行集群状况检测:

bash 复制代码
masterha_check_repl --conf=/etc/masterha/app1.cnf

集群以及将20变更为master,10变为master备选,

bash 复制代码
[root@MHA ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Mon Aug  4 21:35:05 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 21:35:05 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:35:05 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:35:05 2025 - [info] MHA::MasterMonitor version 0.58.
Mon Aug  4 21:35:10 2025 - [info] GTID failover mode = 1
Mon Aug  4 21:35:10 2025 - [info] Dead Servers:
Mon Aug  4 21:35:10 2025 - [info] Alive Servers:
Mon Aug  4 21:35:10 2025 - [info]   172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:35:10 2025 - [info]   172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:35:10 2025 - [info]   172.25.254.30(172.25.254.30:3306)
Mon Aug  4 21:35:10 2025 - [info] Alive Slaves:
Mon Aug  4 21:35:10 2025 - [info]   172.25.254.10(172.25.254.10:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:35:10 2025 - [info]     GTID ON
Mon Aug  4 21:35:10 2025 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:35:10 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:35:10 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:35:10 2025 - [info]     GTID ON
Mon Aug  4 21:35:10 2025 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:35:10 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 21:35:10 2025 - [info] Current Alive Master: 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:35:10 2025 - [info] Checking slave configurations..
Mon Aug  4 21:35:10 2025 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).
Mon Aug  4 21:35:10 2025 - [info] Checking replication filtering settings..
Mon Aug  4 21:35:10 2025 - [info]  binlog_do_db= , binlog_ignore_db= 
Mon Aug  4 21:35:10 2025 - [info]  Replication filtering check ok.
Mon Aug  4 21:35:10 2025 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Aug  4 21:35:10 2025 - [info] Checking SSH publickey authentication settings on the current master..
Mon Aug  4 21:35:10 2025 - [info] HealthCheck: SSH to 172.25.254.20 is reachable.
Mon Aug  4 21:35:10 2025 - [info] 
172.25.254.20(172.25.254.20:3306) (current master)
 +--172.25.254.10(172.25.254.10:3306)
 +--172.25.254.30(172.25.254.30:3306)

Mon Aug  4 21:35:10 2025 - [info] Checking replication health on 172.25.254.10..
Mon Aug  4 21:35:10 2025 - [info]  ok.
Mon Aug  4 21:35:10 2025 - [info] Checking replication health on 172.25.254.30..
Mon Aug  4 21:35:10 2025 - [info]  ok.
Mon Aug  4 21:35:10 2025 - [warning] master_ip_failover_script is not defined.
Mon Aug  4 21:35:10 2025 - [warning] shutdown_script is not defined.
Mon Aug  4 21:35:10 2025 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

7.3.2出现故障时手动切换master:

检测当前的集群状况:

bash 复制代码
 masterha_check_repl --conf=/etc/masterha/app1.cnf

可以看到,当前的集群状况,10为master,20为备选master,30不参与主库竞选,

bash 复制代码
[root@MHA ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Mon Aug  4 21:42:20 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 21:42:20 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:42:20 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:42:21 2025 - [info] MHA::MasterMonitor version 0.58.
Mon Aug  4 21:42:22 2025 - [info] GTID failover mode = 1
Mon Aug  4 21:42:22 2025 - [info] Dead Servers:
Mon Aug  4 21:42:22 2025 - [info] Alive Servers:
Mon Aug  4 21:42:22 2025 - [info]   172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:42:22 2025 - [info]   172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:42:22 2025 - [info]   172.25.254.30(172.25.254.30:3306)
Mon Aug  4 21:42:22 2025 - [info] Alive Slaves:
Mon Aug  4 21:42:22 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:42:22 2025 - [info]     GTID ON
Mon Aug  4 21:42:22 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:42:22 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:42:22 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:42:22 2025 - [info]     GTID ON
Mon Aug  4 21:42:22 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:42:22 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 21:42:22 2025 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:42:22 2025 - [info] Checking slave configurations..
Mon Aug  4 21:42:22 2025 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).
Mon Aug  4 21:42:22 2025 - [info] Checking replication filtering settings..
Mon Aug  4 21:42:22 2025 - [info]  binlog_do_db= , binlog_ignore_db= 
Mon Aug  4 21:42:22 2025 - [info]  Replication filtering check ok.
Mon Aug  4 21:42:22 2025 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Aug  4 21:42:22 2025 - [info] Checking SSH publickey authentication settings on the current master..
Mon Aug  4 21:42:22 2025 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.
Mon Aug  4 21:42:22 2025 - [info] 
172.25.254.10(172.25.254.10:3306) (current master)
 +--172.25.254.20(172.25.254.20:3306)
 +--172.25.254.30(172.25.254.30:3306)

Mon Aug  4 21:42:22 2025 - [info] Checking replication health on 172.25.254.20..
Mon Aug  4 21:42:22 2025 - [info]  ok.
Mon Aug  4 21:42:22 2025 - [info] Checking replication health on 172.25.254.30..
Mon Aug  4 21:42:22 2025 - [info]  ok.
Mon Aug  4 21:42:22 2025 - [warning] master_ip_failover_script is not defined.
Mon Aug  4 21:42:22 2025 - [warning] shutdown_script is not defined.
Mon Aug  4 21:42:22 2025 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

模拟故障情况发生:

将当前master,10节点停用:

bash 复制代码
 /etc/init.d/mysqld stop

查看集群状况:

bash 复制代码
masterha_check_repl --conf=/etc/masterha/app1.cnf
bash 复制代码
[root@MHA ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Mon Aug  4 21:44:47 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 21:44:47 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:44:47 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:44:47 2025 - [info] MHA::MasterMonitor version 0.58.
Mon Aug  4 21:44:49 2025 - [info] GTID failover mode = 1
Mon Aug  4 21:44:49 2025 - [info] Dead Servers:
Mon Aug  4 21:44:49 2025 - [info]   172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:44:49 2025 - [info] Alive Servers:
Mon Aug  4 21:44:49 2025 - [info]   172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:44:49 2025 - [info]   172.25.254.30(172.25.254.30:3306)
Mon Aug  4 21:44:49 2025 - [info] Alive Slaves:
Mon Aug  4 21:44:49 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:44:49 2025 - [info]     GTID ON
Mon Aug  4 21:44:49 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:44:49 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:44:49 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:44:49 2025 - [info]     GTID ON
Mon Aug  4 21:44:49 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:44:49 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 21:44:49 2025 - [warning] MySQL master is not currently alive!
Mon Aug  4 21:44:49 2025 - [info] Checking slave configurations..
Mon Aug  4 21:44:49 2025 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).
Mon Aug  4 21:44:49 2025 - [info] Checking replication filtering settings..
Mon Aug  4 21:44:49 2025 - [info]  Replication filtering check ok.
Mon Aug  4 21:44:49 2025 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Aug  4 21:44:49 2025 - [info] Getting current master (maybe dead) info ..
Mon Aug  4 21:44:49 2025 - [info] Identified master is 172.25.254.10(172.25.254.10:3306).
Mon Aug  4 21:44:49 2025 - [info] Checking SSH publickey authentication settings on the current master..
Mon Aug  4 21:44:49 2025 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.
Mon Aug  4 21:44:49 2025 - [info] Master MHA Node version is 0.58.
Mon Aug  4 21:44:49 2025 - [info] 
172.25.254.10(172.25.254.10:3306) (current master)
 +--172.25.254.20(172.25.254.20:3306)
 +--172.25.254.30(172.25.254.30:3306)

Mon Aug  4 21:44:49 2025 - [info] Checking replication health on 172.25.254.20..
Mon Aug  4 21:44:49 2025 - [error][/usr/share/perl5/vendor_perl/MHA/Server.pm, ln490] Slave IO thread is not running on 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:44:49 2025 - [error][/usr/share/perl5/vendor_perl/MHA/ServerManager.pm, ln1526]  failed!
Mon Aug  4 21:44:49 2025 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln427] Error happened on checking configurations.  at /usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm line 420.
Mon Aug  4 21:44:49 2025 - [error][/usr/share/perl5/vendor_perl/MHA/MasterMonitor.pm, ln525] Error happened on monitoring servers.
Mon Aug  4 21:44:49 2025 - [info] Got exit code 1 (Not master dead).

MySQL Replication Health is NOT OK!

迁移master:

bash 复制代码
masterha_master_switch 
--master_state=dead 
--conf=/etc/masterha/app1.cnf 
--dead_master_host=172.25.254.10 
--dead_master_port=3306 
--new_master_host=172.25.254.20 
--new_master_port=3306 
--ignore_last_failover
参数 作用
--master_state=dead 声明原主库已宕机(核心参数,与在线切换区分)
--conf=/etc/masterha/app1.cnf 指定 MHA 集群配置文件路径
--dead_master_host=172.25.254.10 明确已宕机的原主库 IP
--dead_master_port=3306 宕机主库的 MySQL 端口
--new_master_host=172.25.254.20 指定新主库的 IP(需是集群中的候选从库)
--new_master_port=3306 新主库的 MySQL 端口
--ignore_last_failover 忽略上一次故障转移的记录(默认 MHA 会阻止短时间内重复故障转移,此参数用于强制绕过)

迁移过程:

bash 复制代码
[root@MHA ~]# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=172.25.254.10 --dead_master_port=3306 --new_master_host=172.25.254.20 --new_master_port=3306 --ignore_last_failover
--dead_master_ip=<dead_master_ip> is not set. Using 172.25.254.10.
Mon Aug  4 21:52:25 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 21:52:25 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:52:25 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 21:52:25 2025 - [info] MHA::MasterFailover version 0.58.
Mon Aug  4 21:52:25 2025 - [info] Starting master failover.
Mon Aug  4 21:52:25 2025 - [info] 
Mon Aug  4 21:52:25 2025 - [info] * Phase 1: Configuration Check Phase..
Mon Aug  4 21:52:25 2025 - [info] 
Mon Aug  4 21:52:27 2025 - [info] GTID failover mode = 1
Mon Aug  4 21:52:27 2025 - [info] Dead Servers:
Mon Aug  4 21:52:27 2025 - [info]   172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:52:27 2025 - [info] Checking master reachability via MySQL(double check)...
Mon Aug  4 21:52:27 2025 - [info]  ok.
Mon Aug  4 21:52:27 2025 - [info] Alive Servers:
Mon Aug  4 21:52:27 2025 - [info]   172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:52:27 2025 - [info]   172.25.254.30(172.25.254.30:3306)
Mon Aug  4 21:52:27 2025 - [info] Alive Slaves:
Mon Aug  4 21:52:27 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:52:27 2025 - [info]     GTID ON
Mon Aug  4 21:52:27 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:52:27 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:52:27 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:52:27 2025 - [info]     GTID ON
Mon Aug  4 21:52:27 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:52:27 2025 - [info]     Not candidate for the new Master (no_master is set)
Master 172.25.254.10(172.25.254.10:3306) is dead. Proceed? (yes/NO): yes
Mon Aug  4 21:52:30 2025 - [info] Starting GTID based failover.
Mon Aug  4 21:52:30 2025 - [info] 
Mon Aug  4 21:52:30 2025 - [info] ** Phase 1: Configuration Check Phase completed.
Mon Aug  4 21:52:30 2025 - [info] 
Mon Aug  4 21:52:30 2025 - [info] * Phase 2: Dead Master Shutdown Phase..
Mon Aug  4 21:52:30 2025 - [info] 
Mon Aug  4 21:52:30 2025 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.
Mon Aug  4 21:52:31 2025 - [info] Forcing shutdown so that applications never connect to the current master..
Mon Aug  4 21:52:31 2025 - [warning] master_ip_failover_script is not set. Skipping invalidating dead master IP address.
Mon Aug  4 21:52:31 2025 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Mon Aug  4 21:52:31 2025 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Mon Aug  4 21:52:31 2025 - [info] 
Mon Aug  4 21:52:31 2025 - [info] * Phase 3: Master Recovery Phase..
Mon Aug  4 21:52:31 2025 - [info] 
Mon Aug  4 21:52:31 2025 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Mon Aug  4 21:52:31 2025 - [info] 
Mon Aug  4 21:52:31 2025 - [info] The latest binary log file/position on all slaves is binlog.000002:2465
Mon Aug  4 21:52:31 2025 - [info] Latest slaves (Slaves that received relay log files to the latest):
Mon Aug  4 21:52:31 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:52:31 2025 - [info]     GTID ON
Mon Aug  4 21:52:31 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:52:31 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:52:31 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:52:31 2025 - [info]     GTID ON
Mon Aug  4 21:52:31 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:52:31 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 21:52:31 2025 - [info] The oldest binary log file/position on all slaves is binlog.000002:2465
Mon Aug  4 21:52:31 2025 - [info] Oldest slaves:
Mon Aug  4 21:52:31 2025 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:52:31 2025 - [info]     GTID ON
Mon Aug  4 21:52:31 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:52:31 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 21:52:31 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 21:52:31 2025 - [info]     GTID ON
Mon Aug  4 21:52:31 2025 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Mon Aug  4 21:52:31 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 21:52:31 2025 - [info] 
Mon Aug  4 21:52:31 2025 - [info] * Phase 3.3: Determining New Master Phase..
Mon Aug  4 21:52:31 2025 - [info] 
Mon Aug  4 21:52:31 2025 - [info] 172.25.254.20 can be new master.
Mon Aug  4 21:52:31 2025 - [info] New master is 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:52:31 2025 - [info] Starting master failover..
Mon Aug  4 21:52:31 2025 - [info] 
From:
172.25.254.10(172.25.254.10:3306) (current master)
 +--172.25.254.20(172.25.254.20:3306)
 +--172.25.254.30(172.25.254.30:3306)

To:
172.25.254.20(172.25.254.20:3306) (new master)
 +--172.25.254.30(172.25.254.30:3306)

Starting master switch from 172.25.254.10(172.25.254.10:3306) to 172.25.254.20(172.25.254.20:3306)? (yes/NO): yes
Mon Aug  4 21:52:33 2025 - [info] New master decided manually is 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 21:52:33 2025 - [info] 
Mon Aug  4 21:52:33 2025 - [info] * Phase 3.3: New Master Recovery Phase..
Mon Aug  4 21:52:33 2025 - [info] 
Mon Aug  4 21:52:33 2025 - [info]  Waiting all logs to be applied.. 
Mon Aug  4 21:52:33 2025 - [info]   done.
Mon Aug  4 21:52:33 2025 - [info] Getting new master's binlog name and position..
Mon Aug  4 21:52:33 2025 - [info]  binlog.000002:2514
Mon Aug  4 21:52:33 2025 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.25.254.20', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='repl_rin', MASTER_PASSWORD='xxx';
Mon Aug  4 21:52:33 2025 - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: binlog.000002, 2514, 506a67d6-7129-11f0-8460-000c29b1341f:1-8,
528ca525-7129-11f0-8ea4-000c2923893f:1
Mon Aug  4 21:52:33 2025 - [warning] master_ip_failover_script is not set. Skipping taking over new master IP address.
Mon Aug  4 21:52:33 2025 - [info] Setting read_only=0 on 172.25.254.20(172.25.254.20:3306)..
Mon Aug  4 21:52:33 2025 - [info]  ok.
Mon Aug  4 21:52:33 2025 - [info] ** Finished master recovery successfully.
Mon Aug  4 21:52:33 2025 - [info] * Phase 3: Master Recovery Phase completed.
Mon Aug  4 21:52:33 2025 - [info] 
Mon Aug  4 21:52:33 2025 - [info] * Phase 4: Slaves Recovery Phase..
Mon Aug  4 21:52:33 2025 - [info] 
Mon Aug  4 21:52:33 2025 - [info] 
Mon Aug  4 21:52:33 2025 - [info] * Phase 4.1: Starting Slaves in parallel..
Mon Aug  4 21:52:33 2025 - [info] 
Mon Aug  4 21:52:33 2025 - [info] -- Slave recovery on host 172.25.254.30(172.25.254.30:3306) started, pid: 4645. Check tmp log /var/log/masterha/app1/172.25.254.30_3306_20250804215225.log if it takes time..
Mon Aug  4 21:52:34 2025 - [info] 
Mon Aug  4 21:52:34 2025 - [info] Log messages from 172.25.254.30 ...
Mon Aug  4 21:52:35 2025 - [info] 
Mon Aug  4 21:52:33 2025 - [info]  Resetting slave 172.25.254.30(172.25.254.30:3306) and starting replication from the new master 172.25.254.20(172.25.254.20:3306)..
Mon Aug  4 21:52:34 2025 - [info]  Executed CHANGE MASTER.
Mon Aug  4 21:52:34 2025 - [info]  Slave started.
Mon Aug  4 21:52:34 2025 - [info]  gtid_wait(506a67d6-7129-11f0-8460-000c29b1341f:1-8,
528ca525-7129-11f0-8ea4-000c2923893f:1) completed on 172.25.254.30(172.25.254.30:3306). Executed 0 events.
Mon Aug  4 21:52:35 2025 - [info] End of log messages from 172.25.254.30.
Mon Aug  4 21:52:35 2025 - [info] -- Slave on host 172.25.254.30(172.25.254.30:3306) started.
Mon Aug  4 21:52:35 2025 - [info] All new slave servers recovered successfully.
Mon Aug  4 21:52:35 2025 - [info] 
Mon Aug  4 21:52:35 2025 - [info] * Phase 5: New master cleanup phase..
Mon Aug  4 21:52:35 2025 - [info] 
Mon Aug  4 21:52:35 2025 - [info] Resetting slave info on the new master..
Mon Aug  4 21:52:35 2025 - [info]  172.25.254.20: Resetting slave info succeeded.
Mon Aug  4 21:52:35 2025 - [info] Master failover to 172.25.254.20(172.25.254.20:3306) completed successfully.
Mon Aug  4 21:52:35 2025 - [info] 

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

app1: MySQL Master failover 172.25.254.10(172.25.254.10:3306) to 172.25.254.20(172.25.254.20:3306) succeeded

Master 172.25.254.10(172.25.254.10:3306) is down!

Check MHA Manager logs at MHA.rin.com for details.

Started manual(interactive) failover.
Selected 172.25.254.20(172.25.254.20:3306) as a new master.
172.25.254.20(172.25.254.20:3306): OK: Applying all logs succeeded.
172.25.254.30(172.25.254.30:3306): OK: Slave started, replicating from 172.25.254.20(172.25.254.20:3306)
172.25.254.20(172.25.254.20:3306): Resetting slave info succeeded.
Master failover to 172.25.254.20(172.25.254.20:3306) completed successfully.

恢复故障:

将10节点启用:

bash 复制代码
/etc/init.d/mysqld start

将10节点启用后,当前集群内有两个主库master,所有我们对于当前重新启用的10节点,应该指定其所属的主库20,更改master信息:

sql 复制代码
CHANGE MASTER TO MASTER_HOST='172.25.254.20', MASTER_USER='repl_rin', MASTER_PASSWORD='rin', MASTER_AUTO_POSITION=1;

启用从库复制进程:

sql 复制代码
START SLAVE;

从库10从库状态:

bash 复制代码
show slave status\G;
sql 复制代码
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.20
                  Master_User: repl_rin
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000002
          Read_Master_Log_Pos: 2514
               Relay_Log_File: mysql-a-relay-bin.000002
                Relay_Log_Pos: 411
        Relay_Master_Log_File: binlog.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2514
              Relay_Log_Space: 623
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 20
                  Master_UUID: 528ca525-7129-11f0-8ea4-000c2923893f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 506a67d6-7129-11f0-8460-000c29b1341f:1-8,
528ca525-7129-11f0-8ea4-000c2923893f:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.00 sec)

ERROR: 
No query specified

在MHA上检测集群状况:

bash 复制代码
masterha_check_repl --conf=/etc/masterha/app1.cnf

集群状况为20为主库,10、30为从库,状况良好。

bash 复制代码
[root@MHA ~]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Mon Aug  4 22:11:40 2025 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Mon Aug  4 22:11:40 2025 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Mon Aug  4 22:11:40 2025 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Mon Aug  4 22:11:40 2025 - [info] MHA::MasterMonitor version 0.58.
Mon Aug  4 22:11:41 2025 - [info] GTID failover mode = 1
Mon Aug  4 22:11:41 2025 - [info] Dead Servers:
Mon Aug  4 22:11:41 2025 - [info] Alive Servers:
Mon Aug  4 22:11:41 2025 - [info]   172.25.254.10(172.25.254.10:3306)
Mon Aug  4 22:11:41 2025 - [info]   172.25.254.20(172.25.254.20:3306)
Mon Aug  4 22:11:41 2025 - [info]   172.25.254.30(172.25.254.30:3306)
Mon Aug  4 22:11:41 2025 - [info] Alive Slaves:
Mon Aug  4 22:11:41 2025 - [info]   172.25.254.10(172.25.254.10:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 22:11:41 2025 - [info]     GTID ON
Mon Aug  4 22:11:41 2025 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 22:11:41 2025 - [info]     Primary candidate for the new Master (candidate_master is set)
Mon Aug  4 22:11:41 2025 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.0.40 (oldest major version between slaves) log-bin:enabled
Mon Aug  4 22:11:41 2025 - [info]     GTID ON
Mon Aug  4 22:11:41 2025 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 22:11:41 2025 - [info]     Not candidate for the new Master (no_master is set)
Mon Aug  4 22:11:41 2025 - [info] Current Alive Master: 172.25.254.20(172.25.254.20:3306)
Mon Aug  4 22:11:41 2025 - [info] Checking slave configurations..
Mon Aug  4 22:11:41 2025 - [info]  read_only=1 is not set on slave 172.25.254.10(172.25.254.10:3306).
Mon Aug  4 22:11:41 2025 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).
Mon Aug  4 22:11:41 2025 - [info] Checking replication filtering settings..
Mon Aug  4 22:11:41 2025 - [info]  binlog_do_db= , binlog_ignore_db= 
Mon Aug  4 22:11:41 2025 - [info]  Replication filtering check ok.
Mon Aug  4 22:11:41 2025 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Mon Aug  4 22:11:41 2025 - [info] Checking SSH publickey authentication settings on the current master..
Mon Aug  4 22:11:41 2025 - [info] HealthCheck: SSH to 172.25.254.20 is reachable.
Mon Aug  4 22:11:41 2025 - [info] 
172.25.254.20(172.25.254.20:3306) (current master)
 +--172.25.254.10(172.25.254.10:3306)
 +--172.25.254.30(172.25.254.30:3306)

Mon Aug  4 22:11:41 2025 - [info] Checking replication health on 172.25.254.10..
Mon Aug  4 22:11:41 2025 - [info]  ok.
Mon Aug  4 22:11:41 2025 - [info] Checking replication health on 172.25.254.30..
Mon Aug  4 22:11:41 2025 - [info]  ok.
Mon Aug  4 22:11:41 2025 - [warning] master_ip_failover_script is not defined.
Mon Aug  4 22:11:41 2025 - [warning] shutdown_script is not defined.
Mon Aug  4 22:11:41 2025 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

8.MySQL组复制:

准备工作:

停止10-30的mysql:

bash 复制代码
/etc/init.d/mysqld stop

编辑dns:

bash 复制代码
vim /etc/hosts
bash 复制代码
172.25.254.10    mysql-a.rin.com
172.25.254.20    mysql-b.rin.com
172.25.254.30    mysql-c.rin.com
bash 复制代码
rm -rf /data/mysql/*
bash 复制代码
vim /etc/my.cnf
bash 复制代码
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
server-id=10
default_authentication_plugin=mysql_native_password
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
transaction_write_set_extraction=XXHASH64

接着进行初始化:

初始化完成后再编辑文件my.cnf,添加参数:

bash 复制代码
vim /etc/my.cnf
bash 复制代码
plugin_load_add='group_replication.so'group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address="172.25.254.10:33061"
group_replication_group_seeds="172.25.254.10:33061,172.25.254.20:33061,172.25.254.30:33061"
group_replication_ip_whitelist="172.25.254.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
相关推荐
sz-lcw7 小时前
MySQL知识笔记
笔记·mysql·adb
Java-xy²12 小时前
AlmaLinux release 9.6服务器离线安装MySQL8.0.27详细步骤
运维·服务器·adb
冰夏之夜影2 天前
【ADB】常用按键代码:adb的所有模拟按键
adb
肖永威2 天前
MuMu模拟器使用入门实践指南:从ADB连接到Frida动态分析
python·adb·frida·mumu
color_leo3 天前
虚拟机ubuntu用wifi adb 调试手机
ubuntu·adb·智能手机
维尔切3 天前
MySQL 主从复制
linux·运维·数据库·mysql·adb
浅拾光º3 天前
mysql重启,服务器计划重启,如何优雅地停止MySQL?
服务器·mysql·adb
mg6683 天前
智能电视玩机攻略_开启设备隐藏ADB 自由安装第三方应用
adb·智能电视
珊瑚礁的猪猪侠3 天前
ADB使用指南
python·adb·visual studio code