在rhel9中部署MySQL

一、MySQL源码编译

1.下载安装包

复制代码
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-8.3.0.tar.gz

2.源码编译

👇

复制代码
dnf install cmake3 gcc git bison openssl-devel ncurses-devel systemd-devel rpcgen.x86_64 libtirpc-devel-1.3.3-9.el9.x86_64.rpm  gcc-toolset-12-gcc gcc-toolset-12-gcc-c++ gcc-toolset-12-binutils gcc-toolset-12-annobin-annocheck gcc-toolset-12-annobin-plugin-gcc -y

👇

复制代码
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_BOOST=bundled  -DWITH_SSL=system -DWITH_DEBUG=OFF

3.部署mysql

4.mysql数据结构初始化

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

5.启动mysql

复制代码
dnf install initscripts-10.11.8-4.el9.x86_64 -y

6.mysql的安全初始化

复制代码
[root@mysql-node1 ~]# mysql_secure_installation   # 执行MySQL安全加固脚本

Securing the MySQL server deployment.

Enter password for user root:   # 输入当前root密码(初始化后可能为空或临时密码)

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

New password:   # 设置新的root管理员密码

Re-enter new password:   # 再次确认新密码

VALIDATE PASSWORD COMPONENT 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 component?

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   # 是否再次修改root密码,刚才已设置,选择不再修改


Remove anonymous users? (Press y|Y for Yes, any other key for No) : y   # 移除匿名用户,防止未授权访问,生产环境必须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   # 禁止root远程登录,只允许本地localhost连接,提升安全性
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   # 删除默认的test测试数据库,减少安全隐患
 - 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.

All done!

7.在新主机上部署MySQL

复制代码
#在新主机上做
useradd -r -s /sbin/nologin -M mysql
mkdir -p /data/mysql
chown mysql.mysql /data/mysql/

#在已源码编译安装MySQL主机上做
scp -rp /usr/local/mysql/ root@172.25.254.40:/usr/local/

二、主从复制

1.编写my.cnf 主配置文件

复制代码
[root@mysql-node1 ~]# vim /etc/my.cnf
[mysqld]
# 1. 数据存储相关配置
datadir=/data/mysql          # 指定MySQL数据文件的存储目录(核心目录,所有数据库/表数据都存在这里)
socket=/data/mysql/mysql.sock # MySQL本地套接字文件路径,用于本地客户端(如mysql命令)与服务端通信
symbolic-links=0             # 禁用符号链接(软链接),防止因链接指向异常导致数据损坏,提升安全性

# 2. MySQL主从复制(Replication)相关配置
server-id=10                 # 主从复制中唯一标识当前MySQL实例的ID(范围1-2^32-1),主从节点ID必须不同
log-bin=mysql-bin            # 开启二进制日志(binlog),命名前缀为mysql-bin,主从复制必须开启该日志


[root@mysql-node2 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0

server-id=20
log-bin=mysql-bin


[root@mysql-node3 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0

server-id=30
log-bin=mysql-bin


#在三台主机中重启数据库
[root@mysql-node1~3 ~]# /etc/init.d/mysqld restart

2.建立同步时需要用到的数据库账号

复制代码
[root@mysql-node1 ~]# mysql -uroot -p123456

mysql> SHOW VARIABLES LIKE 'default_authentication_plugin';
+-------------------------------+-----------------------+
| Variable_name                 | Value                 |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.00 sec)

mysql> create user zk@'%' identified with mysql_native_password by '123';    #建立用户
Query OK, 0 rows affected (0.04 sec)

mysql> select User from mysql.user;
+------------------+
| User             |
+------------------+
| zk               |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
+------------------+
5 rows in set (0.00 sec)

mysql>  GRANT replication slave ON *.* to zk@'%';		#给用户授权
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW GRANTS FOR zk@'%';
+---------------------------------------------+
| Grants for lee@%                            |
+---------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `zk`@`%`  |
+---------------------------------------------+
1 row in set (0.00 sec)


#在其他主机中
[root@mysql-node2 ~]# mysql -uzk -p123 -h172.25.254.10
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.3.0 Source distribution

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

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> quit

3.配置数据库一主一从

复制代码
#在slave主机中
[root@mysql-node2 ~]# mysql -uroot -p123456

mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.10',MASTER_USER='zk',MASTER_PASSWORD='123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=657;
Query OK, 0 rows affected, 8 warnings (0.02 sec)

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

mysql> start slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.10
                  Master_User: zk
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 657
               Relay_Log_File: mysql-node2-relay-bin.000002
                Relay_Log_Pos: 328
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes    #数据同步成功
            Slave_SQL_Running: Yes    #通过同步过来的数据做日志回访成功

4.测试

复制代码
[root@mysql-node1 ~]# mysql -uroot -123456			#在master中建立库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database timinglee;
Query OK, 1 row affected (0.00 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| timinglee          |
+--------------------+
5 rows in set (0.00 sec)

[root@mysql-node2 ~]# mysql -uroot -p123456			#在slave主机中可以实现数据同步
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| timinglee          |
+--------------------+
5 rows in set (0.00 sec)

5.向当前一主一从中加入新的数据库

复制代码
#模拟一主一从中已经存在数据情况
[root@mysql-node1 ~]# mysql -uroot -p123456
mysql> CREATE TABLE timinglee.userlist (
    -> name VARCHAR(10) not null,
    -> pass VARCHAR(50) not null
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO timinglee.userlist values ('user1','123');
Query OK, 1 row affected (0.08 sec)

mysql> SELECT * FROM timinglee.userlist;
+-------+------+
| name  | pass |
+-------+------+
| user1 | 123  |
+-------+------+
1 row in set (0.00 sec)

#加入新从库时需要手动备份导入拉平数据
[root@mysql-node1 ~]# mysqldump -uroot -p123456 timinglee > timinglee.sql
[root@mysql-node1 ~]# scp timinglee.sql root@172.25.254.30:/root/

[root@mysql-node3 ~]# mysql -uroot -p123456 -e "create database timinglee;"
[root@mysql-node3 ~]# mysql -uroot -p123456 timinglee < timinglee.sql
[root@mysql-node3 ~]# mysql -uroot -p123456
mysql> select * from timinglee.userlist;
+-------+------+
| name  | pass |
+-------+------+
| user1 | 123  |
+-------+------+
1 row in set (0.01 sec)

6.将新库加入主从结构中

复制代码
#在master(10)中查看日志的id
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |     1404 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set, 1 warning (0.00 sec)

[root@mysql-node3 ~]# mysql -uroot -p123456
mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.10',MASTER_USER='zk',MASTER_PASSWORD='123',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=1404;
Query OK, 0 rows affected, 8 warnings (0.07 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.10
                  Master_User: zk
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 1404
               Relay_Log_File: mysql-node3-relay-bin.000002
                Relay_Log_Pos: 328
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

7.测试一主两从

复制代码
#在master中建立数据
mysql> INSERT INTO  timinglee.userlist values ('user2','123');

#在新加入的slave中查看信息
mysql> select * from timinglee.userlist;
+-------+------+
| name  | pass |
+-------+------+
| user1 | 123  |
| user2 | 123  |
+-------+------+
2 rows in set (0.00 sec)

三、mysql主从架构中的使用技巧及优化

1.延迟复制

#在指定需要延迟同步的slave主机中,如果主机中安装数据库的版本是8以上

测试

#在master主机中对数据进行更改

#在未被延迟的slave数据库中查看数据操作动作立马被同步

#在被设定延迟复制的主机中查看动作还没被同步

#等待延迟时间过后再次查看完成延迟同步

2.慢查询日志

#慢查询日志是否开启

#开启慢查询日志

#慢查询阈值

测试

3.gtid模式

#在master和slave中默认gtid模式是未开启的

#在所有主机中追加参数

复制代码
[root@mysql-node1~3 ~]# vim /etc/my.cnf
gtid_mode=ON
enforce-gtid-consistency=ON

[root@mysql-node1~3 ~]# /etc/init.d/mysqld restart

#在三台主机中分别查看gtid模式是否开启

#在从库中重新连接;

复制代码
mysql> SHOW SLAVE STATUS \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.10
                  Master_User: zk
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 158
               Relay_Log_File: mysql-node2-relay-bin.000002
                Relay_Log_Pos: 375
        Relay_Master_Log_File: mysql-bin.000003
             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: 158
              Relay_Log_Space: 592
              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: c24925f1-12bb-11f1-933a-000c2909b317
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 60
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 10
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                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

GTID 的核心价值:用全局唯一的事务 ID 替代手动指定 binlog 位置,让主从复制的配置、故障恢复、维护更简单可靠

4.多线程回放

#在slave主机中默认回方日志时使用单线程回放

复制代码
#开启多线程回放日志(在slave主中追加)
[root@mysql-node2 ~]# vim /etc/my.cnf
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
relay_log_recovery=ON

[root@mysql-node2 ~]# /etc/init.d/mysqld restart

#查看更改生效信息

5.半同步模式

#在master主机中操作

复制代码
[root@mysql-node1 ~]# vim /etc/my.cnf
rpl_semi_sync_master_enabled=1    #追加到文件中

👆

复制代码
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql>  SET GLOBAL rpl_semi_sync_master_enabled = 1;

mysql> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%';

#在slave主机中操作

复制代码
[root@mysql-node2~3 ~]# vim /etc/my.cnf
rpl_semi_sync_slave_enabled=1    #追加到文件中

👆

复制代码
mysql>  INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;
mysql> STOP SLAVE IO_THREAD;
mysql> START SLAVE IO_THREAD;

SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE '%semi%';

#在master主机中查看

👆

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

测试

#模拟ack故障 在所有slave主机中

#在主库中建立新数据

#故障恢复 在所有slave主机中

#在主库中建立新数据

四、Mysql-MHA高可用集群

一.Mysql-MHA高可用集群环境配置

mha环境检测无法通过的通用解决方案(所有的mysql节点初始化)

复制代码
#重新初始化数据
[root@mysql-node1 ~]# /etc/init.d/mysqld stop
[root@mysql-node1 ~]# rm -rf /data/mysql/*
[root@mysql-node1 ~]# mysqld --initialize --user=mysql

[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql-node1.err'.
 SUCCESS!
 
 [root@mysql-node1 ~]# mysql_secure_installation
 
 [root@mysql-node1 ~]# mysql -uroot -p123456 -e "create user zk@'%' identified with mysql_native_password by '123';"

[root@mysql-node1 ~]# mysql -uroot -p123456 -e "GRANT replication slave ON *.* to zk@'%';"

[root@mysql-node1 ~]# mysql -uroot -p123456 -e "show master status;"
mysql: [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.000002 |      977 |              |                  | 780e975e-19d1-11f1-98e4-000c2909b317:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+


#重新配置主从 在slave主机中

/etc/init.d/mysqld stop

rm -rf /data/mysql/*

mysqld --initialize --user=mysql

/etc/init.d/mysqld start

mysql_secure_installation

[root@mysql-node2 ~]# mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='zk', MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;"

[root@mysql-node2 ~]# mysql -uroot -p123456 -e "start slave;"                                                              
[root@mysql-node2 ~]# mysql -uroot -p123456 -e "show  slave status\G;"

1.在所有主机中安装Mha相应软件

复制代码
[root@mha ~]# unzip MHA-7.zip
[root@mha ~]# cd MHA-7/
[root@mha MHA-7]# dnf install perl perl-DBD-MySQL perl-CPAN  -y
​
复制代码
[root@mha ~]# pwd
/root
[root@mha ~]# tar zxf cpan_plugin.tar.gz

#cpan想快点可以提前导包
复制代码
[root@mha MHA-7]# cpan
Loading internal logger. Log::Log4perl recommended for better logging
​
CPAN.pm requires configuration, but most of it can be done automatically.
If you answer 'no' below, you will enter an interactive dialog for each
configuration option instead.
​
Would you like to configure as much as possible automatically? [yes] yes
​
cpan[1]>  install Config::Tiny
​
cpan[2]> install Log::Dispatch
​
cpan[3]> install Mail::Sender
Specify defaults for Mail::Sender? (y/N) y
Default encoding of message bodies (N)one, (Q)uoted-printable, (B)ase64: n
​
cpan[4]> install Parallel::ForkManager
cpan[5]>exit
​
#验证组建是否安装成功
[root@mha ~]# perl -MConfig::Tiny -e 'print "OK\n"'
OK
[root@mha ~]# perl -MLog::Dispatch -e 'print "OK\n"'
OK
[root@mha ~]# perl -MMail::Sender -e 'print "OK\n"'
Mail::Sender is deprecated and you should look to Email::Sender instead at -e line 0.
OK
[root@mha ~]# perl -MParallel::ForkManager -e 'print "OK\n"'
OK


#在mha节点
[root@mha MHA-7]# rpm -ivh mha4mysql-manager-0.58-0.el7.centos.noarch.rpm mha4mysql-node-0.58-0.el7.centos.noarch.rpm --nodeps

#在所有mysql节点
[root@ ]# rpm -ivh mha4mysql-node-0.58-0.el7.centos.noarch.rpm --nodeps

2.在slave中安装相应软件

复制代码
[root@mha MHA-7]# for i in 10 20 30
> do
> scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.$i:/mnt
> ssh -l root 172.25.254.$i "rpm -ivh /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm --nodeps"
> done
Warning: Permanently added '172.25.254.10' (ED25519) to the list of known hosts.
mha4mysql-node-0.58-0.el7.centos.noarch.rpm                     100%   35KB  16.9MB/s   00:00
Verifying...                          ########################################
准备中...                          ########################################
正在升级/安装...
mha4mysql-node-0.58-0.el7.centos      ########################################
Warning: Permanently added '172.25.254.20' (ED25519) to the list of known hosts.
mha4mysql-node-0.58-0.el7.centos.noarch.rpm                     100%   35KB  28.2MB/s   00:00
Verifying...                          ########################################
准备中...                          ########################################
正在升级/安装...
mha4mysql-node-0.58-0.el7.centos      ########################################
Warning: Permanently added '172.25.254.30' (ED25519) to the list of known hosts.
mha4mysql-node-0.58-0.el7.centos.noarch.rpm                     100%   35KB  20.1MB/s   00:00
Verifying...                          ########################################
准备中...                          ########################################
正在升级/安装...
mha4mysql-node-0.58-0.el7.centos      ########################################
​

3.rhel9中修改MHA-Manager中的检测代码

复制代码
[root@mha MHA-7]# vim /usr/share/perl5/vendor_perl/MHA/NodeUtil.pm
199 #sub parse_mysql_major_version($) {
200 #  my $str = shift;
201 #  my $result = sprintf( '%03d%03d', $str =~ m/(\d+)/g );
202 #  return $result;
203 #}
​
sub parse_mysql_major_version($) {
  my $str = shift;
  my @nums = $str =~ m/(\d+)/g;
  my $result = sprintf( '%03d%03d', $nums[0]//0, $nums[1]//0);
  return $result;
}

4.为MHA建立远程登录用户

复制代码
#在master主机中,建立好后slave会自动同步
mysql> create user root@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.01 sec)
​
mysql> GRANT ALL ON *.* TO root@'%' ;
Query OK, 0 rows affected (0.00 sec)

5.生成MHA-manager的配置文件模板

复制代码
[root@mha mha4mysql-manager-0.58]# mkdir  /etc/masterha/ -p
[root@mha MHA-7]# tar zxf mha4mysql-manager-0.58.tar.gz
[root@mha MHA-7]# cd mha4mysql-manager-0.58
[root@mha mha4mysql-manager-0.58]# mkdir  /etc/masterha/ -p
​
[root@mha mha4mysql-manager-0.58]# cat samples/conf/masterha_default.cnf samples/conf/app1.cnf  > /etc/masterha/app1.cnf

6.修改配置文件

复制代码
[root@mha mha4mysql-manager-0.58]# vim /etc/masterha/app1.cnf
[server default]
user=root
password=123456
ssh_user=root
repl_user=zk
repl_password=123
master_binlog_dir= /data/mysql
remote_workdir=/tmp
secondary_check_script= masterha_secondary_check -s 172.25.254.10 -s 172.25.254.2
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=/etc/masterha
manager_log=/etc/masterha/mha.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
                          

7.检测环境

复制代码
[root@mha mha4mysql-manager-0.58]# masterha_check_ssh  --conf=/etc/masterha/app1.cnf
Fri Feb 27 16:23:20 2026 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Feb 27 16:23:20 2026 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Feb 27 16:23:20 2026 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Feb 27 16:23:20 2026 - [info] Starting SSH connection tests..
Fri Feb 27 16:23:21 2026 - [debug]
Fri Feb 27 16:23:20 2026 - [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)..
Fri Feb 27 16:23:20 2026 - [debug]   ok.
Fri Feb 27 16:23:20 2026 - [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)..
Fri Feb 27 16:23:20 2026 - [debug]   ok.
Fri Feb 27 16:23:21 2026 - [debug]
Fri Feb 27 16:23:20 2026 - [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)..
Fri Feb 27 16:23:21 2026 - [debug]   ok.
Fri Feb 27 16:23:21 2026 - [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)..
Fri Feb 27 16:23:21 2026 - [debug]   ok.
Fri Feb 27 16:23:22 2026 - [debug]
Fri Feb 27 16:23:21 2026 - [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)..
Fri Feb 27 16:23:21 2026 - [debug]   ok.
Fri Feb 27 16:23:21 2026 - [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)..
Fri Feb 27 16:23:21 2026 - [debug]   ok.
Fri Feb 27 16:23:22 2026 - [info] All SSH connection tests passed successfully.
Use of uninitialized value in exit at /usr/bin/masterha_check_ssh line 44.
​
​
​
​
[root@mha mha4mysql-manager-0.58]# masterha_check_repl --conf=/etc/masterha/app1.cnf
Fri Feb 27 16:23:50 2026 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Fri Feb 27 16:23:50 2026 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Fri Feb 27 16:23:50 2026 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Fri Feb 27 16:23:50 2026 - [info] MHA::MasterMonitor version 0.58.
Fri Feb 27 16:23:51 2026 - [info] GTID failover mode = 1
Fri Feb 27 16:23:51 2026 - [info] Dead Servers:
Fri Feb 27 16:23:51 2026 - [info] Alive Servers:
Fri Feb 27 16:23:51 2026 - [info]   172.25.254.10(172.25.254.10:3306)
Fri Feb 27 16:23:51 2026 - [info]   172.25.254.20(172.25.254.20:3306)
Fri Feb 27 16:23:51 2026 - [info]   172.25.254.30(172.25.254.30:3306)
Fri Feb 27 16:23:51 2026 - [info] Alive Slaves:
Fri Feb 27 16:23:51 2026 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Fri Feb 27 16:23:51 2026 - [info]     GTID ON
Fri Feb 27 16:23:51 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Fri Feb 27 16:23:51 2026 - [info]     Primary candidate for the new Master (candidate_master is set)
Fri Feb 27 16:23:51 2026 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Fri Feb 27 16:23:51 2026 - [info]     GTID ON
Fri Feb 27 16:23:51 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Fri Feb 27 16:23:51 2026 - [info]     Not candidate for the new Master (no_master is set)
Fri Feb 27 16:23:51 2026 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)
Fri Feb 27 16:23:51 2026 - [info] Checking slave configurations..
Fri Feb 27 16:23:51 2026 - [info]  read_only=1 is not set on slave 172.25.254.20(172.25.254.20:3306).
Fri Feb 27 16:23:51 2026 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).
Fri Feb 27 16:23:51 2026 - [info] Checking replication filtering settings..
Fri Feb 27 16:23:51 2026 - [info]  binlog_do_db= , binlog_ignore_db=
Fri Feb 27 16:23:51 2026 - [info]  Replication filtering check ok.
Fri Feb 27 16:23:51 2026 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.
Fri Feb 27 16:23:51 2026 - [info] Checking SSH publickey authentication settings on the current master..
Fri Feb 27 16:23:51 2026 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.
Fri Feb 27 16:23:51 2026 - [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)
​
Fri Feb 27 16:23:51 2026 - [info] Checking replication health on 172.25.254.20..
Fri Feb 27 16:23:51 2026 - [info]  ok.
Fri Feb 27 16:23:51 2026 - [info] Checking replication health on 172.25.254.30..
Fri Feb 27 16:23:51 2026 - [info]  ok.
Fri Feb 27 16:23:51 2026 - [warning] master_ip_failover_script is not defined.
Fri Feb 27 16:23:51 2026 - [warning] shutdown_script is not defined.
Fri Feb 27 16:23:51 2026 - [info] Got exit code 0 (Not master dead).
​
MySQL Replication Health is OK.

二.集群切换操作

1.手动切换

master无故障切换
复制代码
#默认状态
[root@mysql-node2 ~]#  mysql -uroot -p123456 -e "show slave status\G;"  | head -n 10
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.10
                  Master_User: lee
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1968
               Relay_Log_File: mysql-node2-relay-bin.000002
                Relay_Log_Pos: 2185

[root@mysql-node3 ~]# mysql -uroot -p123456 -e "show slave status\G;"  | head -n 15
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.10
                  Master_User: lee
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1968
               Relay_Log_File: mysql-node3-relay-bin.000002
                Relay_Log_Pos: 2185
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:


#执行切换,把master切换到20

[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
Sat Mar  7 10:21:01 2026 - [info] MHA::MasterRotate version 0.58.
Sat Mar  7 10:21:01 2026 - [info] Starting online master switch..
Sat Mar  7 10:21:01 2026 - [info]
Sat Mar  7 10:21:01 2026 - [info] * Phase 1: Configuration Check Phase..
Sat Mar  7 10:21:01 2026 - [info]
Sat Mar  7 10:21:01 2026 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Mar  7 10:21:01 2026 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sat Mar  7 10:21:01 2026 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sat Mar  7 10:21:02 2026 - [info] GTID failover mode = 1
Sat Mar  7 10:21:02 2026 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:21:02 2026 - [info] Alive Slaves:
Sat Mar  7 10:21:02 2026 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:21:02 2026 - [info]     GTID ON
Sat Mar  7 10:21:02 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:21:02 2026 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Mar  7 10:21:02 2026 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:21:02 2026 - [info]     GTID ON
Sat Mar  7 10:21:02 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:21:02 2026 - [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     #输入内容
Sat Mar  7 10:21:28 2026 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..
Sat Mar  7 10:21:28 2026 - [info]  ok.
Sat Mar  7 10:21:28 2026 - [info] Checking MHA is not monitoring or doing failover..
Sat Mar  7 10:21:28 2026 - [info] Checking replication health on 172.25.254.20..
Sat Mar  7 10:21:28 2026 - [info]  ok.
Sat Mar  7 10:21:28 2026 - [info] Checking replication health on 172.25.254.30..
Sat Mar  7 10:21:28 2026 - [info]  ok.
Sat Mar  7 10:21:28 2026 - [info] 172.25.254.20 can be new master.
Sat Mar  7 10:21:28 2026 - [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					#输入内容
Sat Mar  7 10:21:34 2026 - [info] Checking whether 172.25.254.20(172.25.254.20:3306) is ok for the new master..
Sat Mar  7 10:21:34 2026 - [info]  ok.
Sat Mar  7 10:21:34 2026 - [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.
Sat Mar  7 10:21:34 2026 - [info] 172.25.254.10(172.25.254.10:3306): Resetting slave pointing to the dummy host.
Sat Mar  7 10:21:34 2026 - [info] ** Phase 1: Configuration Check Phase completed.
Sat Mar  7 10:21:34 2026 - [info]
Sat Mar  7 10:21:34 2026 - [info] * Phase 2: Rejecting updates Phase..
Sat Mar  7 10:21:34 2026 - [info]
Sat Mar  7 10:21:34 2026 - [info] Executing master ip online change script to disable write on the current master:
Sat Mar  7 10:21:34 2026 - [info]   /etc/masterha/scripts/master_ip_online_change --command=stop --orig_master_host=172.25.254.10 --orig_master_ip=172.25.254.10 --orig_master_port=3306 --orig_master_user='root' --new_master_host=172.25.254.20 --new_master_ip=172.25.254.20 --new_master_port=3306 --new_master_user='root' --orig_master_ssh_user=root --new_master_ssh_user=root   --orig_master_is_new_slave --orig_master_password=xxx --new_master_password=xxx



***************************************************************
Disabling the VIP - 172.25.254.100/24 on old master: 172.25.254.10
***************************************************************



Error: ipv4: Address not found.
Sat Mar  7 10:21:34 2026 - [info]  ok.
Sat Mar  7 10:21:34 2026 - [info] Locking all tables on the orig master to reject updates from everybody (including root):
Sat Mar  7 10:21:34 2026 - [info] Executing FLUSH TABLES WITH READ LOCK..
Sat Mar  7 10:21:34 2026 - [info]  ok.
Sat Mar  7 10:21:34 2026 - [info] Orig master binlog:pos is mysql-bin.000002:1968.
Sat Mar  7 10:21:34 2026 - [info]  Waiting to execute all relay logs on 172.25.254.20(172.25.254.20:3306)..
Sat Mar  7 10:21:34 2026 - [info]  master_pos_wait(mysql-bin.000002:1968) completed on 172.25.254.20(172.25.254.20:3306). Executed 0 events.
Sat Mar  7 10:21:34 2026 - [info]   done.
Sat Mar  7 10:21:34 2026 - [info] Getting new master's binlog name and position..
Sat Mar  7 10:21:34 2026 - [info]  mysql-bin.000002:2337
Sat Mar  7 10:21:34 2026 - [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='lee', MASTER_PASSWORD='xxx';
Sat Mar  7 10:21:34 2026 - [info] Executing master ip online change script to allow write on the new master:
Sat Mar  7 10:21:34 2026 - [info]   /etc/masterha/scripts/master_ip_online_change --command=start --orig_master_host=172.25.254.10 --orig_master_ip=172.25.254.10 --orig_master_port=3306 --orig_master_user='root' --new_master_host=172.25.254.20 --new_master_ip=172.25.254.20 --new_master_port=3306 --new_master_user='root' --orig_master_ssh_user=root --new_master_ssh_user=root   --orig_master_is_new_slave --orig_master_password=xxx --new_master_password=xxx



***************************************************************
Enabling the VIP - 172.25.254.100/24 on new master: 172.25.254.20
***************************************************************



Sat Mar  7 10:21:34 2026 - [info]  ok.
Sat Mar  7 10:21:34 2026 - [info]
Sat Mar  7 10:21:34 2026 - [info] * Switching slaves in parallel..
Sat Mar  7 10:21:34 2026 - [info]
Sat Mar  7 10:21:34 2026 - [info] -- Slave switch on host 172.25.254.30(172.25.254.30:3306) started, pid: 1654
Sat Mar  7 10:21:34 2026 - [info]
Sat Mar  7 10:21:35 2026 - [info] Log messages from 172.25.254.30 ...
Sat Mar  7 10:21:35 2026 - [info]
Sat Mar  7 10:21:34 2026 - [info]  Waiting to execute all relay logs on 172.25.254.30(172.25.254.30:3306)..
Sat Mar  7 10:21:34 2026 - [info]  master_pos_wait(mysql-bin.000002:1968) completed on 172.25.254.30(172.25.254.30:3306). Executed 0 events.
Sat Mar  7 10:21:34 2026 - [info]   done.
Sat Mar  7 10:21:34 2026 - [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)..
Sat Mar  7 10:21:34 2026 - [info]  Executed CHANGE MASTER.
Sat Mar  7 10:21:34 2026 - [info]  Slave started.
Sat Mar  7 10:21:35 2026 - [info] End of log messages from 172.25.254.30 ...
Sat Mar  7 10:21:35 2026 - [info]
Sat Mar  7 10:21:35 2026 - [info] -- Slave switch on host 172.25.254.30(172.25.254.30:3306) succeeded.
Sat Mar  7 10:21:35 2026 - [info] Unlocking all tables on the orig master:
Sat Mar  7 10:21:35 2026 - [info] Executing UNLOCK TABLES..
Sat Mar  7 10:21:35 2026 - [info]  ok.
Sat Mar  7 10:21:35 2026 - [info] Starting orig master as a new slave..
Sat Mar  7 10:21:35 2026 - [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)..
Sat Mar  7 10:21:35 2026 - [info]  Executed CHANGE MASTER.
Sat Mar  7 10:21:35 2026 - [info]  Slave started.
Sat Mar  7 10:21:35 2026 - [info] All new slave servers switched successfully.
Sat Mar  7 10:21:35 2026 - [info]
Sat Mar  7 10:21:35 2026 - [info] * Phase 5: New master cleanup phase..
Sat Mar  7 10:21:35 2026 - [info]
Sat Mar  7 10:21:35 2026 - [info]  172.25.254.20: Resetting slave info succeeded.
Sat Mar  7 10:21:35 2026 - [info] Switching master to 172.25.254.20(172.25.254.20:3306) completed successfully.


#查看集群状态
[root@mysql-node1 ~]#  mysql -uroot -p123456 -e "show slave status\G;"  | head -n 15
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.20
                  Master_User: lee
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 2337
               Relay_Log_File: mysql-node1-relay-bin.000002
                Relay_Log_Pos: 742
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:


[root@mysql-node3 ~]# mysql -uroot -p123456 -e "show slave status\G;"  | head -n 15
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.20
                  Master_User: lee
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 2337
               Relay_Log_File: mysql-node3-relay-bin.000002
                Relay_Log_Pos: 742
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
master故障后切换
复制代码
[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.
Sat Mar  7 10:26:01 2026 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Sat Mar  7 10:26:01 2026 - [info] Reading application default configuration from /etc/masterha/app1.cnf..
Sat Mar  7 10:26:01 2026 - [info] Reading server configuration from /etc/masterha/app1.cnf..
Sat Mar  7 10:26:01 2026 - [info] MHA::MasterFailover version 0.58.
Sat Mar  7 10:26:01 2026 - [info] Starting master failover.
Sat Mar  7 10:26:01 2026 - [info]
Sat Mar  7 10:26:01 2026 - [info] * Phase 1: Configuration Check Phase..
Sat Mar  7 10:26:01 2026 - [info]
Sat Mar  7 10:26:02 2026 - [info] GTID failover mode = 1
Sat Mar  7 10:26:02 2026 - [info] Dead Servers:
Sat Mar  7 10:26:02 2026 - [info]   172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:26:02 2026 - [info] Checking master reachability via MySQL(double check)...
Sat Mar  7 10:26:02 2026 - [info]  ok.
Sat Mar  7 10:26:02 2026 - [info] Alive Servers:
Sat Mar  7 10:26:02 2026 - [info]   172.25.254.20(172.25.254.20:3306)
Sat Mar  7 10:26:02 2026 - [info]   172.25.254.30(172.25.254.30:3306)
Sat Mar  7 10:26:02 2026 - [info] Alive Slaves:
Sat Mar  7 10:26:02 2026 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:26:02 2026 - [info]     GTID ON
Sat Mar  7 10:26:02 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:26:02 2026 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Mar  7 10:26:02 2026 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:26:02 2026 - [info]     GTID ON
Sat Mar  7 10:26:02 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:26:02 2026 - [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		#输入内容
Sat Mar  7 10:26:08 2026 - [info] Starting GTID based failover.
Sat Mar  7 10:26:08 2026 - [info]
Sat Mar  7 10:26:08 2026 - [info] ** Phase 1: Configuration Check Phase completed.
Sat Mar  7 10:26:08 2026 - [info]
Sat Mar  7 10:26:08 2026 - [info] * Phase 2: Dead Master Shutdown Phase..
Sat Mar  7 10:26:08 2026 - [info]
Sat Mar  7 10:26:08 2026 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.
Sat Mar  7 10:26:09 2026 - [info] Forcing shutdown so that applications never connect to the current master..
Sat Mar  7 10:26:09 2026 - [info] Executing master IP deactivation script:
Sat Mar  7 10:26:09 2026 - [info]   /etc/masterha/scripts/master_ip_failover --orig_master_host=172.25.254.10 --orig_master_ip=172.25.254.10 --orig_master_port=3306 --command=stopssh --ssh_user=root


IN SCRIPT TEST====/sbin/ip addr del 172.25.254.100/24 dev eth0==/sbin/ip addr add 172.25.254.100/24 dev eth0===

Disabling the VIP on old master: 172.25.254.10
Sat Mar  7 10:26:09 2026 - [info]  done.
Sat Mar  7 10:26:09 2026 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.
Sat Mar  7 10:26:09 2026 - [info] * Phase 2: Dead Master Shutdown Phase completed.
Sat Mar  7 10:26:09 2026 - [info]
Sat Mar  7 10:26:09 2026 - [info] * Phase 3: Master Recovery Phase..
Sat Mar  7 10:26:09 2026 - [info]
Sat Mar  7 10:26:09 2026 - [info] * Phase 3.1: Getting Latest Slaves Phase..
Sat Mar  7 10:26:09 2026 - [info]
Sat Mar  7 10:26:09 2026 - [info] The latest binary log file/position on all slaves is mysql-bin.000002:2295
Sat Mar  7 10:26:09 2026 - [info] Latest slaves (Slaves that received relay log files to the latest):
Sat Mar  7 10:26:09 2026 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:26:09 2026 - [info]     GTID ON
Sat Mar  7 10:26:09 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:26:09 2026 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Mar  7 10:26:09 2026 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:26:09 2026 - [info]     GTID ON
Sat Mar  7 10:26:09 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:26:09 2026 - [info]     Not candidate for the new Master (no_master is set)
Sat Mar  7 10:26:09 2026 - [info] The oldest binary log file/position on all slaves is mysql-bin.000002:2295
Sat Mar  7 10:26:09 2026 - [info] Oldest slaves:
Sat Mar  7 10:26:09 2026 - [info]   172.25.254.20(172.25.254.20:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:26:09 2026 - [info]     GTID ON
Sat Mar  7 10:26:09 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:26:09 2026 - [info]     Primary candidate for the new Master (candidate_master is set)
Sat Mar  7 10:26:09 2026 - [info]   172.25.254.30(172.25.254.30:3306)  Version=8.3.0 (oldest major version between slaves) log-bin:enabled
Sat Mar  7 10:26:09 2026 - [info]     GTID ON
Sat Mar  7 10:26:09 2026 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)
Sat Mar  7 10:26:09 2026 - [info]     Not candidate for the new Master (no_master is set)
Sat Mar  7 10:26:09 2026 - [info]
Sat Mar  7 10:26:09 2026 - [info] * Phase 3.3: Determining New Master Phase..
Sat Mar  7 10:26:09 2026 - [info]
Sat Mar  7 10:26:09 2026 - [info] 172.25.254.20 can be new master.
Sat Mar  7 10:26:09 2026 - [info] New master is 172.25.254.20(172.25.254.20:3306)
Sat Mar  7 10:26:09 2026 - [info] Starting master failover..
Sat Mar  7 10:26:09 2026 - [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			#输入内容
Sat Mar  7 10:26:13 2026 - [info] New master decided manually is 172.25.254.20(172.25.254.20:3306)
Sat Mar  7 10:26:13 2026 - [info]
Sat Mar  7 10:26:13 2026 - [info] * Phase 3.3: New Master Recovery Phase..
Sat Mar  7 10:26:13 2026 - [info]
Sat Mar  7 10:26:13 2026 - [info]  Waiting all logs to be applied..
Sat Mar  7 10:26:13 2026 - [info]   done.
Sat Mar  7 10:26:13 2026 - [info] Getting new master's binlog name and position..
Sat Mar  7 10:26:13 2026 - [info]  mysql-bin.000002:2337
Sat Mar  7 10:26:13 2026 - [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='lee', MASTER_PASSWORD='xxx';
Sat Mar  7 10:26:13 2026 - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: mysql-bin.000002, 2337, 1b73d49c-19c8-11f1-a11b-000c29e84b64:1,
b9652041-19c7-11f1-a4c3-000c29f4a60c:1-7
Sat Mar  7 10:26:13 2026 - [info] Executing master IP activate script:
Sat Mar  7 10:26:13 2026 - [info]   /etc/masterha/scripts/master_ip_failover --command=start --ssh_user=root --orig_master_host=172.25.254.10 --orig_master_ip=172.25.254.10 --orig_master_port=3306 --new_master_host=172.25.254.20 --new_master_ip=172.25.254.20 --new_master_port=3306 --new_master_user='root'   --new_master_password=xxx
Unknown option: new_master_user
Unknown option: new_master_password


IN SCRIPT TEST====/sbin/ip addr del 172.25.254.100/24 dev eth0==/sbin/ip addr add 172.25.254.100/24 dev eth0===

Enabling the VIP - 172.25.254.100/24 on the new master - 172.25.254.20
Sat Mar  7 10:26:13 2026 - [info]  OK.
Sat Mar  7 10:26:13 2026 - [info] Setting read_only=0 on 172.25.254.20(172.25.254.20:3306)..
Sat Mar  7 10:26:13 2026 - [info]  ok.
Sat Mar  7 10:26:13 2026 - [info] ** Finished master recovery successfully.
Sat Mar  7 10:26:13 2026 - [info] * Phase 3: Master Recovery Phase completed.
Sat Mar  7 10:26:13 2026 - [info]
Sat Mar  7 10:26:13 2026 - [info] * Phase 4: Slaves Recovery Phase..
Sat Mar  7 10:26:13 2026 - [info]
Sat Mar  7 10:26:13 2026 - [info]
Sat Mar  7 10:26:13 2026 - [info] * Phase 4.1: Starting Slaves in parallel..
Sat Mar  7 10:26:13 2026 - [info]
Sat Mar  7 10:26:13 2026 - [info] -- Slave recovery on host 172.25.254.30(172.25.254.30:3306) started, pid: 1681. Check tmp log /etc/masterha/172.25.254.30_3306_20260307102601.log if it takes time..
Sat Mar  7 10:26:14 2026 - [info]
Sat Mar  7 10:26:14 2026 - [info] Log messages from 172.25.254.30 ...
Sat Mar  7 10:26:14 2026 - [info]
Sat Mar  7 10:26:13 2026 - [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)..
Sat Mar  7 10:26:13 2026 - [info]  Executed CHANGE MASTER.
Sat Mar  7 10:26:13 2026 - [info]  Slave started.
Sat Mar  7 10:26:13 2026 - [error][/usr/share/perl5/vendor_perl/MHA/Server.pm, ln974] gtid_wait(1b73d49c-19c8-11f1-a11b-000c29e84b64:1,
b9652041-19c7-11f1-a4c3-000c29f4a60c:1-7) returned NULL on 172.25.254.30(172.25.254.30:3306). Maybe SQL thread was aborted?
Sat Mar  7 10:26:14 2026 - [info] End of log messages from 172.25.254.30.
Sat Mar  7 10:26:14 2026 - [error][/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm, ln2045] Master failover to 172.25.254.20(172.25.254.20:3306) done, but recovery on slave partially failed.
Sat Mar  7 10:26:14 2026 - [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)

Master 172.25.254.10(172.25.254.10:3306) is down!

Check MHA Manager logs at mha for details.

Started manual(interactive) failover.
Invalidated master IP address on 172.25.254.10(172.25.254.10:3306)
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.20(172.25.254.20:3306): OK: Activated master IP address.
172.25.254.30(172.25.254.30:3306): ERROR: Failed on waiting gtid exec set on master.
Master failover to 172.25.254.20(172.25.254.20:3306) done, but recovery on slave partially failed.


#查看切换信息
[root@mysql-node3 ~]# mysql -uroot -p123456 -e "show slave status\G;"  | head -n 15
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.20
                  Master_User: lee
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 2337
               Relay_Log_File: mysql-node3-relay-bin.000002
                Relay_Log_Pos: 422
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:


#故障恢复
#当出现故障切换后,mha主机中会出现切换锁文件,当文件存在后不能再次执行切换
[root@mha ~]# ls /etc/masterha/
app1.cnf  app1.failover.complete  mha.log  scripts
				  |
				#锁文件



[root@mha ~]# rm -fr /etc/masterha/app1.failover.complete
[root@mysql-node2 ~]# mysql -uroot -p123456 -e "reset slave;"



[root@mysql-node1 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!

mysql -uroot -p123456 -e "CHANGE MASTER TO MASTER_HOST='172.25.254.20', MASTER_USER='zk', MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;"

[root@mysql-node1 ~]# mysql -uroot -p123456 -e "start slave;" 
[root@mysql-node1 ~]# mysql -uroot -p123456 -e "show slave status\G;"  | head -n 15
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 172.25.254.20
                  Master_User: lee
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 2337
               Relay_Log_File: mysql-node1-relay-bin.000002
                Relay_Log_Pos: 422
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:

2.自动切换

复制代码
#为了方便观察建议开启两个shell
[root@mha ~]# > /etc/masterha/*.log
[root@mha ~]# watch -n 1 cat /etc/masterha/mha.log

#开启自动切换功能
[root@mha ~]# masterha_manager --conf=/etc/masterha/app1.cnf  &
[root@mha ~]# jobs
[1]+  运行中               masterha_manager --conf=/etc/masterha/app1.cnf &

#模拟故障
[root@mysql-node1 ~]# /etc/init.d/mysqld stop

三.vip功能及vip的启动切换

复制代码
[root@mha ~]# unzip MHA-7.zip
[root@mha ~]# ll MHA-7/master_ip_*
-rw-r--r-- 1 root root 2156  1月 14  2021 MHA-7/master_ip_failover
-rw-r--r-- 1 root root 3813  1月 14  2021 MHA-7/master_ip_online_change


[root@mha ~]# mkdir  /etc/masterha/scripts
[root@mha ~]# cp  MHA-7/master_ip_*  /etc/masterha/scripts

[root@mha ~]# vim /etc/masterha/app1.cnf
master_ip_failover_script= /etc/masterha/scripts/master_ip_failover

master_ip_online_change_script= /etc/masterha/scripts/master_ip_online_change

[root@mha ~]# vim /etc/masterha/scripts/master_ip_failover
my $vip = '172.25.254.100/24';

[root@mha ~]# vim /etc/masterha/scripts/master_ip_online_change
my $vip = '172.25.254.100/24';

[root@mysql-node1 ~]# ip a a 172.25.254.100/24 dev eth0

#测试:
[root@mha ~]# masterha_manager  --conf=/etc/masterha/app1.cnf &
[root@mha ~]# jobs
[1]+  运行中               masterha_manager --conf=/etc/masterha/app1.cnf &

#关闭mysql master
[root@mysql-node1 ~]# /etc/init.d/mysqld stop

[root@mysql-node2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:e8:4b:64 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 172.25.254.20/24 brd 172.25.254.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 172.25.254.100/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::f8be:d443:72d7:d336/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

五、MySQL组复制

一.还原mysql所有节点

利用ansible还原所有节点

复制代码
#利用ansible还原所有节点
[root@mha ~]# cat > /etc/yum.repos.d/epel.repo <<EOF
> [epel]
> name = epel
> baseurl = https://mirrors.aliyun.com/epel-archive/9.6/Everything/x86_64/
> gpgcheck = 0
> EOF
​
[root@mha ~]# dnf install ansible -y
​
[root@mha ~]# ansible --version
ansible [core 2.14.18]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.21 (main, Feb 10 2025, 00:00:00) [GCC 11.5.0 20240719 (Red Hat 11.5.0-5)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
​
​
[root@mha ~]# useradd  devops
[root@mha ~]# echo lee | passwd --stdin devops
[root@mha ~]# su - devops
[devops@mha ~]$ mkdir  ansible
​
[devops@mha ansible]$ cat >ansible.cfg <<EOF
[defaults]
inventory=./inventory
remote_user=root
host_key_checking=false
[privilege_escalation]
become=False
EOF
​
[devops@mha ansible]$ ansible mysql -m user -a 'name=devops'
[devops@mha ansible]$  ansible mysql -m shell -a 'echo devops | passwd --stdin devops'
[devops@mha ansible]$ ansible mysql -m shell -a 'echo "devops   ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers'
[devops@mha ansible]$ ansible all -m file -a 'path=/home/devops/.ssh owner=devops group=devops mode="0700" state=directory'
[devops@mha ansible]$ ansible all -m copy -a 'src=/home/devops/.ssh/authorized_keys dest=/home/devops/.ssh/authorized_keys owner=devops group=devops mode='0600''
​
[devops@mha ansible]$ cat >ansible.cfg <<EOF
[defaults]
inventory=./inventory
remote_user=devops
host_key_checking=false
​
[privilege_escalation]
become=True
become_ask_pass=False
become_method=sudo
become_user=root
EOF
​
[devops@mha ansible]$ ansible all -m shell -a 'whoami'
172.25.254.20 | CHANGED | rc=0 >>
root
172.25.254.30 | CHANGED | rc=0 >>
root
172.25.254.10 | CHANGED | rc=0 >>
root
​
[devops@mha ansible]$ vim clear_mysql.yml
- name: reset mysql
  hosts: mysql
  tasks:
  - name: stop mysql
    shell: '/etc/init.d/mysqld stop'
    ignore_errors: yes
​
  - name: delete mysql data
    file:
      path: /data/mysql
      state: absent
​
  - name: crate data directroy
    file:
      path: /data/mysql
      state: directory
      owner: mysql
      group: mysql
​
  - name: initialize mysql
    shell: '/usr/local/mysql/bin/mysqld --initialize --user=mysql'
​
​
[devops@mha ansible]$ ansible-playbook  clear_mysql.yml  -vv | grep password

手动还原方式

复制代码
#所有节点初始化数据
[root@mysql-node1 ~]# /etc/init.d/mysqld stop
[root@mysql-node1 ~]# rm -rf /data/mysql/*
​
[root@mysql-node1 ~]# cat > /etc/my.cnf <<EOF
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
​
#不同主机server-id一定要根据实际情况做相应改变
server-id=10|20|30
log-bin=mysql-bin
​
gtid_mode=ON
enforce-gtid-consistency=ON
default_authentication_plugin=mysql_native_password
log_slave_updates=ON
binlog_format=ROW
binlog_checksum=NONE
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
EOF
​
[root@mysql-node1 ~]# mysqld --user=mysql --initialize

二.部署组复制

复制代码
#设置所有mysql节点的解析
[root@mysql-node1 ~]# cat > /etc/hosts <<EOF
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.10     mysql-node1
172.25.254.20     mysql-node2
172.25.254.30     mysql-node3
EOF
​
[root@mysql-node1 ~]# cat  >> /etc/my.cnf <<EOF
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"   #其他两台主机一定要根据ip进行修改
group_replication_group_seeds="172.25.254.10:33061,172.25.254.20:33061,172.25.254.30:33061"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
EOF
[root@mysql-node1 ~]# /etc/init.d/mysqld start
​
#配置组复制-在首台主机中
[root@mysql-node1 ~]# mysql -uroot -p'lsyVh+etR1ht'
mysql> alter user root@localhost identified   by 'lee';
Query OK, 0 rows affected (0.04 sec)
​
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
​
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'lee';
Query OK, 0 rows affected (0.00 sec)
​
mysql> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> GRANT CONNECTION_ADMIN ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql>  GRANT GROUP_REPLICATION_STREAM ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
​
mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
​
mysql> CHANGE REPLICATION SOURCE TO SOURCE_USER='rpl_user', SOURCE_PASSWORD='lee' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.01 sec)
​
mysql> SHOW PLUGINS;     #查看组复制插件是否激活
| group_replication               | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)
​
mysql> START GROUP_REPLICATION USER='rpl_user', PASSWORD='lee';
Query OK, 0 rows affected (1.10 sec)
​
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.00 sec)
​
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | ac3d6eaf-1a0a-11f1-9efa-000c29f4a60c | mysql-node1 |        3306 | ONLINE       | PRIMARY     | 8.3.0          | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
1 row in set (0.00 sec)
​
​
#配置组复制在其余主机中
[root@mysql-node2 ~]# /etc/init.d/mysqld start
[root@mysql-node2 ~]# mysql -uroot -p'XkP<Uaa:9so5'
mysql> alter user root@localhost identified   by 'lee';
Query OK, 0 rows affected (0.00 sec)
​
mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)
​
mysql> CREATE USER rpl_user@'%' IDENTIFIED BY 'lee';
Query OK, 0 rows affected (0.00 sec)
​
mysql>  GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> GRANT CONNECTION_ADMIN ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> GRANT GROUP_REPLICATION_STREAM ON *.* TO rpl_user@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql>  SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)
​
mysql>  CHANGE REPLICATION SOURCE TO SOURCE_USER='rpl_user',SOURCE_PASSWORD='lee' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
​
mysql> START GROUP_REPLICATION USER='rpl_user', PASSWORD='lee';
ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.            #出现此处报错可以初始化下master
​
mysql> reset master;            #用过此命令解决以上报错
Query OK, 0 rows affected, 1 warning (0.04 sec)
​
mysql> START GROUP_REPLICATION USER='rpl_user', PASSWORD='lee';
Query OK, 0 rows affected (7.94 sec)
​
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | ac3d6eaf-1a0a-11f1-9efa-000c29f4a60c | mysql-node1 |        3306 | ONLINE       | PRIMARY     | 8.3.0          | XCom                       |
| group_replication_applier | e0b37b20-1a0b-11f1-a62c-000c29e84b64 | mysql-node2 |        3306 | ONLINE       | PRIMARY     | 8.3.0          | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
2 rows in set (0.00 sec)
​
#看到主机online表示成功

三台主机部署成功后的效果

三.测试

复制代码
#测试所有节点是否可以执行读写并数据是否同步
#node1中
mysql> create database timinglee;
Query OK, 1 row affected (0.00 sec)
​
mysql> create table timinglee.userlist (
    -> username VARCHAR(10) PRIMARY KEY NOT NULL,
    -> password VARCHAR(50) NOT NULL
    -> );
Query OK, 0 rows affected (0.01 sec)
​
mysql> INSERT INTO timinglee.userlist VALUES ('user1','111');
Query OK, 1 row affected (0.01 sec)
​
#在node2中查看并插入新的数据
mysql> select * from timinglee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 111      |
+----------+----------+
1 row in set (0.00 sec)
​
mysql> insert into timinglee.userlist values ('user2','222');
Query OK, 1 row affected (0.01 sec)
​
mysql> select * from timinglee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 111      |
| user2    | 222      |
+----------+----------+
2 rows in set (0.01 sec)
​
#在node3中查看并插入数据
mysql> select * from timinglee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 111      |
| user2    | 222      |
+----------+----------+
2 rows in set (0.00 sec)
​
​
mysql> insert into timinglee.userlist values ('user3','333');
Query OK, 1 row affected (0.01 sec)
​
mysql> select * from timinglee.userlist;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 111      |
| user2    | 222      |
| user3    | 333      |
+----------+----------+
3 rows in set (0.00 sec)
​
mysql>
​
​
#在node1和2中也可以看到以上数据

六、MySQL router

一.Mysqlrouter软件下载

复制代码
[root@mysqlrouter ~]# wget https://downloads.mysql.com/archives/get/p/41/file/mysql-router-community-8.4.7-1.el9.x86_64.rpm

二.安装mysqlrouter

复制代码
[root@mysqlrouter ~]# dnf install mysql-router-community-8.4.7-1.el9.x86_64.rpm -y

三.mysqlrouter配置文件

复制代码
[root@mysqlrouter ~]# rpm -qc mysql-router-community
/etc/logrotate.d/mysqlrouter                #日志轮询及日志截断策略
/etc/mysqlrouter/mysqlrouter.conf           #主配置文件
​
[root@mysqlrouter ~]# systemctl status mysqlrouter.service      #启动脚本

四.配置mysqlrouter

复制代码
[root@mysqlrouter ~]# vim /etc/mysqlrouter/mysqlrouter.conf
[routing:ro]
bind_address = 0.0.0.0
bind_port = 7001
destinations = 172.25.254.10:3306,172.25.254.20:3306,172.25.254.30:3306
routing_strategy = round-robin
​
​
[routing:rw]
bind_address = 0.0.0.0
bind_port = 7002
destinations = 172.25.254.30:3306,172.25.254.20:3306,172.25.254.10:3306
routing_strategy = first-available
​
[root@mysqlrouter ~]# systemctl enable --now mysqlrouter.service
Created symlink /etc/systemd/system/multi-user.target.wants/mysqlrouter.service → /usr/lib/systemd/system/mysqlrouter.service.
​
[root@mysqlrouter ~]# netstat -antlupe | grep mysql
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      991        176991     39587/mysqlrouter
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      991        176009     39587/mysqlrouter
​

五.测试

复制代码
#在mysql节点的任意主机中添加root远程登录
[root@mysql-node1 ~]# mysql -uroot -plee
​
mysql> CREATE USER root@'%' identified by 'lee';
Query OK, 0 rows affected (0.00 sec)
​
mysql> GRANT ALL ON *.* TO root@'%';
Query OK, 0 rows affected (0.00 sec)
​
mysql> quit
Bye
[root@mysql-node1 ~]# mysql -uroot -plee -h172.25.254.10
​
mysql> quit
Bye
[root@mysql-node1 ~]# mysql -uroot -plee -h172.25.254.20
​
​
mysql> quit
Bye
[root@mysql-node1 ~]# mysql -uroot -plee -h172.25.254.30
​
mysql> quit
​
​
#查看调度效果
[root@mysql-node10 & 20 & 30 ~]# watch -n1 lsof -i :3306
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  9879 mysql   22u  IPv6  56697      0t0  TCP *:mysql (LISTEN)
​
#测试效果
[root@mysql-node1 ~]#  mysql -uroot -plee -h172.25.254.40 -P7002
​
相关推荐
m0_547722922 小时前
乒乓球比赛管理系统
python·mysql
杨云龙UP2 小时前
Oracle与MySQL数据库运行状态快速检查指南
数据库·mysql·oracle
rrrjqy2 小时前
MySQL事务深度解析:从ACID特性到隔离级别实战
数据库·mysql·oracle
Saniffer_SH2 小时前
【高清视频】企业级NVMe SSD (E3.S, U.2)和消费类M.2 SSD拆解分析
服务器·网络·数据库·驱动开发·测试工具·fpga开发·压力测试
顶点多余2 小时前
Mysql数据库基础
linux·数据库·mysql
小吴编程之路2 小时前
MySQL 事务管理核心解析:从 ACID 到 MVCC 深度理解
数据库·mysql
somi72 小时前
Linux系统编程-数据库-SQLite3
linux·数据库·sqlite
不剪发的Tony老师2 小时前
SQLite Release 3.52.0发布,有哪些新功能?
数据库·sqlite
Z1eaf_complete2 小时前
SQL注入绕过详解与防御机制
数据库·sql