数据库mysql集群主从、高可用MGR、MHA技术详解

一、安装数据库mysql步骤

环境:红帽7.9系统

安装依赖

yum install cmake gcc-c++ openssl-devel  ncurses-devel.x86_64 libtirpc-devel-1.3.3-8.el9_4.x86_64.rpm rpcgen.x86_64 -y

将下载的MySQL软件包解压并cd到mysql的目录下

[root@mysql-node10 ~]# tar zxf mysql-boost-5.7.44.tar.gz

[root@mysql-node10 ~]# cd /root/mysql-5.7.44

源码编译模块

cmake -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=/root/mysql-5.7.44/boost/boost_1_59_0

make -j2 (这里的数字要等于或小于你的虚拟机上的内核数量)

make install

二、部署mysql

切换到mysql软件目录下

[root@pxe ~]# cd /usr/local/mysql/

创建一个数据库的用户

[root@pxe mysql]# useradd -s /sbin/nologin -M mysql

创建mysql的数据目录

[root@pxe mysql]# mkdir /data/mysql -p

给数据目录赋权

[root@pxe mysql]# chown mysql.mysql -R /data/mysql

生成启动脚本

[root@pxe mysql]# cd support-files/

[root@pxe support-files]# cp mysql.server /etc/init.d/mysqld

编辑配置文件

[root@pxe support-files]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql              #指定数据目录

socket=/data/mysql/mysql.sock        #指定套接字

symbolic-links=0                    #数据只能存放到数据目录中,禁止链接到数据目录

添加环境变量

[root@pxe support-files]# vim ~/.bash_profile

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

export PATH

执行脚本

[root@pxe support-files]# source ~/.bash_profile

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

[root@pxe support-files]# mysqld --user mysql --initialize

localhost: 10WPQGxQHTh6(D  207fL4w3Czsp(T

[root@pxe support-files]# cd

将初始密码保存

[root@pxe ~]# vim passwd

启动mysql

[root@pxe ~]# /etc/init.d/mysqld start

Starting MySQL.Logging to '/data/mysql/pxe.err'.

 SUCCESS!

通过命令启动数据库指定级别,这里默认启动2345

[root@pxe ~]# chkconfig mysqld on

[root@pxe ~]# chkconfig --list

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

安全初始化

[root@pxe ~]# mysql_secure_installation

Enter password for user root:  输入初始密码passwd里面保存的

New password:设置新密码

Re-enter new password:再输入一遍新密码

Press y|Y for Yes, any other key for No: no

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

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

Success.

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

Success.

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

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

登录测试

[root@pxe ~]# mysql -uroot -p

Enter password:123

mysql> show databases

    -> ;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| sys                |

+--------------------+

4 rows in set (0.00 sec)

三、mysql的主从复制

3.1无数据添加slave

设置主从id

[root@mysqlnode1 local]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=10

log-bin=mysql-bin

重启数据库

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

Shutting down MySQL. SUCCESS!

Starting MySQL. SUCCESS!

[root@mysql-node2 mysql]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=20

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

Shutting down MySQL.. SUCCESS!

Starting MySQL. SUCCESS!

配置master

[root@mysql-node1 mysql]# mysql -uroot -p

进入数据库配置用户权限

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

mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123';

Query OK, 0 rows affected (0.00 sec)

对用户进行授权

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';

Query OK, 0 rows affected (0.00 sec)

查看master的状态

mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+-------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 |     1151 |              |                  |                   |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql> show master status\G

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

             File: mysql-bin.000001

         Position: 1151

     Binlog_Do_DB:

 Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

配置slave

mysql> change master to master_host='172.25.254.10',master_user='repl',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=1151;——设置master

Query OK, 0 rows affected, 2 warnings (0.03 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

查看slave的状态

mysql>  SHOW SLAVE STATUS\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.25.254.10

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 1151

               Relay_Log_File: mysql-node2-relay-bin.000002

                Relay_Log_Pos: 320

        Relay_Master_Log_File: mysql-bin.000001

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

在master上插入数据测试:

mysql> create database jcl;

Query OK, 1 row affected (0.01 sec)

mysql> create table jcl.userlist(

    -> username varchar(10) not null,

    -> password varchar(50) not null

    -> );

Query OK, 0 rows affected (0.00 sec)

mysql> insert into jcl.userlist values ('jjj1','111');

Query OK, 1 row affected (0.02 sec)

mysql> select * from jcl.userlist

    -> ;

+----------+----------+

| username | password |

+----------+----------+

| jjj1     | 111      |

+----------+----------+

1 row in set (0.00 sec)

在20主机上可以看到数据

mysql> select * from jcl.userlist;

+----------+----------+

| username | password |

+----------+----------+

| jjj1     | 111      |

+----------+----------+

1 row in set (0.00 sec)

注:

在主数据库中添加数据后从数据库上会更新,但是从数据库添加数据主数据库这边是不会看到的。

3.2有数据添加slave

将mysql文件同步到node3上

[root@mysql-node1 mysql]# rsync -al /usr/local/mysql root@172.25.254.30:/usr/local

从master节点备份数据

[root@mysql-node1 mysql]# mysqldump -uroot -p jcl > jcl.sql

Enter password:

[root@mysql-node1 mysql]# scp jcl.sql root@172.25.254.30:/mnt

root@172.25.254.30's password:

jcl.sql                                         100% 1944     3.4MB/s   00:00

在node3上创建用户和数据目录

[root@mysql-node3 local]# useradd -s /sbin/nologin -M mysql

[root@mysql-node3 local]# mkdir -p /data/mysql

[root@mysql-node3 local]# chown -R mysql.mysql /data/mysql/

[root@mysql-node3 local]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=30

[root@mysql-node3 local]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@mysql-node3 local]# vim ~/.bash_profile

具体内容参考以上MySQL的部署

[root@mysql-node3 local]# source ~/.bash_profile

初始化数据库

[root@mysql-node3 local]# mysqld --user=mysql --initialize

[root@mysql-node3 ~]# /etc/init.d/mysqld start

Starting MySQL.Logging to '/data/mysql/mysql-node3.err'.

 SUCCESS!

安全初始化

[root@mysql-node3 ~]# mysql_secure_installation

[root@mysql-node3 ~]# cd /mnt

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

[root@mysql-node3 mnt]# mysql -uroot -p123 -e "create database jcl;"

mysql: [Warning] Using a password on the command line interface can be insecure.

[root@mysql-node3 mnt]# mysql -uroot -p123 jcl < jcl.sql

mysql: [Warning] Using a password on the command line interface can be insecure.

配置slave2的slave功能

[root@mysql-node3 mnt]# mysql -uroot -p123

mysql> change master to master_host='172.25.254.10',master_user='repl',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=1794;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

测试查看master的数据

mysql> select * from jcl.userlist;

+----------+----------+

| username | password |

+----------+----------+

| jjj1     | 111      |

+----------+----------+

1 row in set (0.00 sec)

3.3延迟复制

在slave2上设置延迟复制

mysql> stop slave sql_thread;

Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_delay=60;

Query OK, 0 rows affected (0.00 sec)

mysql> start slave sql_thread;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;

SQL_Delay: 60

在主数据库上删除一条信息

mysql> delete from jcl.userlist where username='jjj1';

Query OK, 1 row affected (0.00 sec)

mysql> select * from jcl.userlist;

+----------+----------+

| username | password |

+----------+----------+

| ccc      | 222      |

+----------+----------+

1 row in set (0.00 sec)

在30从数据库上因为做了延迟,所以还是看的我们删除的信息

mysql> select * from jcl.userlist;

+----------+----------+

| username | password |

+----------+----------+

| jjj1     | 111      |

| ccc      | 222      |

+----------+----------+

2 rows in set (0.01 sec)

3.4慢查询日志

慢查询就是当执行SQL超过long_query_time参数设定的时间阈值的语句。

查看慢查询参数

mysql> SHOW variables  like "slow%";

+---------------------+----------------------------------+

| Variable_name       | Value                            |

+---------------------+----------------------------------+

| slow_launch_time    | 2                                |

| slow_query_log      | OFF                              |

| slow_query_log_file | /data/mysql/mysql-node3-slow.log |

+---------------------+----------------------------------+

3 rows in set (0.00 sec)

开启慢查询日志

mysql> SET GLOBAL slow_query_log=ON;

Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES like "long%";

+-----------------+-----------+

| Variable_name   | Value     |

+-----------------+-----------+

| long_query_time | 10.000000 |

+-----------------+-----------+

1 row in set (0.01 sec)

查看慢查询是否开启

mysql> SHOW VARIABLES like "slow%";

+---------------------+----------------------------------+

| Variable_name       | Value                            |

+---------------------+----------------------------------+

| slow_launch_time    | 2                                |

| slow_query_log      | ON                               |

| slow_query_log_file | /data/mysql/mysql-node3-slow.log |

+---------------------+----------------------------------+

3 rows in set (0.00 sec)

查看慢查询日志

[root@mysql-node3 mnt]# cat  /data/mysql/mysql-node3-slow.log

/usr/local/mysql/bin/mysqld, Version: 5.7.44 (Source distribution). started with:

Tcp port: 3306  Unix socket: /data/mysql/mysql.sock

Time                 Id Command    Argument

慢查询测试:

mysql> select sleep (10);

+------------+

| sleep (10) |

+------------+

|          0 |

+------------+

1 row in set (10.01 sec)

[root@mysql-node1 mysql]# cat  /data/mysql/mysql-node1-slow.log

/usr/local/mysql/bin/mysqld, Version: 5.7.44-log (Source distribution). started with:

Tcp port: 3306  Unix socket: /data/mysql/mysql.sock

Time                 Id Command    Argument

# Time: 2024-08-22T19:06:21.682243Z

# User@Host: root[root] @ localhost []  Id:    14

# Query_time: 10.012568  Lock_time: 0.000000 Rows_sent: 1  Rows_examined: 0

SET timestamp=1724353581;

select sleep (10);

3.5 mysql的并行复制

[root@mysql-node2 mysql]# vim /etc/my.cnf

gtid_mode=ON

enforce-gtid-consistency=ON

slave-parallel-type=LOGICAL_CLOCK #基于组提交

slave-parallel-workers=16 #开启线程数量

master_info_repository=TABLE #master信息在表中记录,默认记录

relay_log_info_repository=TABLE #回放日志信息在表中记>录,默认记录

relay_log_recovery=ON #日志回放恢复功能开启

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

Shutting down MySQL.. SUCCESS!

Starting MySQL.. SUCCESS!

此时sql线程转化为协调线程,16个worker负责处理sql协调线程发送过来的处理请求

3.6数据库主从架构的异步机制原理

三个线程:

当master中录入或更改数据时,通过binlog转发数据更改事件给Dump线程,dump线程再转发给slave数据库中的I/O线程并拷贝到自己的relaylog中,这时候从数据库中的SQL线程会读取relaylog中的数据并同步。

架构缺点:

当master端同步到slave端时网络出现问题,或者master端直接挂掉,二进制日志可能根本没有到达slave,master出现问题slave端接管master,这个过程中数据就丢失了。

3.7半同步模式原理及配置

半同步原理:

1.用户线程写入完成后master中的dump会把日志推送到slave端

2.slave中的io线程接收后保存到relaylog中继日志

3.保存完成后slave向master端返回ack

4.在未接受到slave的ack时master端时不做提交的,一直处于等待当收到ack后提交到存储引擎

5.在5.6版本中用到的时after_commit模式,after_commit模式时先提交在等待ack返回后输出ok

在master上的配置启用半同步模式

[root@mysql-node1 mysql]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=10

log-bin=mysql-bin

gtid_mode=ON

enforce-gtid-consistency=ON

rpl_semi_sync_master_enabled=1  #开启半同步功能

symbolic-links=0

[root@mysql-node1 mysql]# mysql -uroot -p123

安装半同步插件

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

Query OK, 0 rows affected (0.00 sec)

查看插件情况

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

+----------------------+---------------+

| PLUGIN_NAME          | PLUGIN_STATUS |

+----------------------+---------------+

| rpl_semi_sync_master | ACTIVE        |

+----------------------+---------------+

1 row in set (0.00 sec)

打开半同步功能

mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;

Query OK, 0 rows affected (0.00 sec)

查看半同步功能状态

mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';

+-------------------------------------------+------------+

| Variable_name                             | Value      |

+-------------------------------------------+------------+

| rpl_semi_sync_master_enabled              | ON         |

| rpl_semi_sync_master_timeout              | 10000      |

| rpl_semi_sync_master_trace_level          | 32         |

| rpl_semi_sync_master_wait_for_slave_count | 1          |

| rpl_semi_sync_master_wait_no_slave        | ON         |

| rpl_semi_sync_master_wait_point           | AFTER_SYNC |

+-------------------------------------------+------------+

6 rows in set (0.00 sec)

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';

+--------------------------------------------+-------+

| Variable_name                              | Value |

+--------------------------------------------+-------+

| Rpl_semi_sync_master_clients               | 0     |

| Rpl_semi_sync_master_net_avg_wait_time     | 0     |

| Rpl_semi_sync_master_net_wait_time         | 0     |

| Rpl_semi_sync_master_net_waits             | 0     |

| Rpl_semi_sync_master_no_times              | 0     |

| Rpl_semi_sync_master_no_tx                 | 0     |

| Rpl_semi_sync_master_status                | ON    |

| Rpl_semi_sync_master_timefunc_failures     | 0     |

| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |

| Rpl_semi_sync_master_tx_wait_time          | 0     |

| Rpl_semi_sync_master_tx_waits              | 0     |

| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |

| Rpl_semi_sync_master_wait_sessions         | 0     |

| Rpl_semi_sync_master_yes_tx                | 0     |

+--------------------------------------------+-------+

14 rows in set (0.00 sec)

mysql> show plugins;

在slave上的配置开启半同步功能

[root@mysql-node2 mysql]# vim /etc/my.cnf

rpl_semi_sync_master_enabled=1

[root@mysql-node2 mysql]# mysql -uroot -p123

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

Query OK, 0 rows affected (0.01 sec)

mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1;

Query OK, 0 rows affected (0.00 sec)

重启io线程,半同步才能生效

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.00 sec)

mysql> START SLAVE IO_THREAD;

Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'rpl_semi_sync%';

+---------------------------------+-------+

| Variable_name                   | Value |

+---------------------------------+-------+

| rpl_semi_sync_slave_enabled     | ON    |

| rpl_semi_sync_slave_trace_level | 32    |

+---------------------------------+-------+

2 rows in set (0.01 sec)

mysql> SHOW STATUS LIKE 'Rpl_semi_sync%';

+----------------------------+-------+

| Variable_name              | Value |

+----------------------------+-------+

| Rpl_semi_sync_slave_status | ON    |

+----------------------------+-------+

1 row in set (0.00 sec)

测试:

mysql>  SHOW STATUS LIKE 'Rpl_semi_sync%';

+--------------------------------------------+-------+

| Variable_name                              | Value |

+--------------------------------------------+-------+

| Rpl_semi_sync_master_clients               | 2     |

| Rpl_semi_sync_master_net_avg_wait_time     | 0     |

| Rpl_semi_sync_master_net_wait_time         | 0     |

| Rpl_semi_sync_master_net_waits             | 0     |

| Rpl_semi_sync_master_no_times              | 0     |

| Rpl_semi_sync_master_no_tx                 | 0     |

| Rpl_semi_sync_master_status                | ON    |

| Rpl_semi_sync_master_timefunc_failures     | 0     |

| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |

| Rpl_semi_sync_master_tx_wait_time          | 0     |

| Rpl_semi_sync_master_tx_waits              | 0     |

| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |

| Rpl_semi_sync_master_wait_sessions         | 0     |

| Rpl_semi_sync_master_yes_tx                | 0     |

+--------------------------------------------+-------+

14 rows in set (0.00 sec)

模拟故障:

[root@mysql-node2 mysql]# mysql -uroot -p123

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.00 sec)

[root@mysql-node3 mysql]# mysql -uroot -p123

mysql> STOP SLAVE IO_THREAD;

Query OK, 0 rows affected (0.00 sec)

在master上插入数据

mysql> insert  into jcl.userlist values ('lll','333');

Query OK, 1 row affected (10.02 sec)  ——超时10s

mysql> SHOW STATUS LIKE 'Rpl_semi%';

+--------------------------------------------+-------+

| Variable_name                              | Value |

+--------------------------------------------+-------+

| Rpl_semi_sync_master_clients               | 0     |

| Rpl_semi_sync_master_net_avg_wait_time     | 0     |

| Rpl_semi_sync_master_net_wait_time         | 0     |

| Rpl_semi_sync_master_net_waits             | 0     |

| Rpl_semi_sync_master_no_times              | 1     |

| Rpl_semi_sync_master_no_tx                 | 1     |

| Rpl_semi_sync_master_status                | OFF   |

| Rpl_semi_sync_master_timefunc_failures     | 0     |

| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |

| Rpl_semi_sync_master_tx_wait_time          | 0     |

| Rpl_semi_sync_master_tx_waits              | 0     |

| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |

| Rpl_semi_sync_master_wait_sessions         | 0     |

| Rpl_semi_sync_master_yes_tx                | 0     |

+--------------------------------------------+-------+

14 rows in set (0.01 sec)

把slave开启

mysql> start SLAVE IO_THREAD;

Query OK, 0 rows affected (0.01 sec)

再在master上插入数据会发现插入很快

mysql> insert  into jcl.userlist values ('jjj2','1212');

Query OK, 1 row affected (0.00 sec)

mysql> select * from jcl.userlist

    -> ;

+----------+----------+

| username | password |

+----------+----------+

| ccc      | 222      |

| lll      | 333      |

| jjj2     | 1212     |

+----------+----------+

3 rows in set (0.01 sec)

在slave上查看数据信息会发现实现数据同步

mysql> select * from jcl.userlist;

+----------+----------+

| username | password |

+----------+----------+

| ccc      | 222      |

| lll      | 333      |

| jjj2     | 1212     |

+----------+----------+

3 rows in set (0.00 sec)

3.8gtid模式

当激活GITD之后 ,当master出现问题后,slave2和master的数据最接近,会被作为新的master slave1指向新的master,但是他不会去检测新的master的pos id,只需要继续读取自己gtid_next即可,这样就会大大减少数据同步的延迟。

在master端和slave端开启gtid模式

[root@mysql-node1 mysql]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=10

log-bin=mysql-bin

gtid_mode=ON

enforce-gtid-consistency=ON

symbolic-links=0

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

Shutting down MySQL.. SUCCESS!

Starting MySQL.. SUCCESS!

停止slave端

[root@mysql-node2 mysql]# mysql -p

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

开启slave端的gtid

mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='repl',

    -> MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.07 sec)

mysql> show slave status\G;

 Auto_Position: 1

四 、mysql高可用之组复制 (MGR)

4.1组复制流程

首先我们将多个节点共同组成一个复制组,在执行读写(RW)事务的时候,需要通过一致性协议层 (Consensus 层)的同意,也就是读写事务想要进行提交,必须要经过组里“大多数人”(对应 Node 节 点)的同意,大多数指的是同意的节点数量需要大于 (N/2+1),这样才可以进行提交,而不是原发起 方一个说了算。而针对只读(RO)事务则不需要经过组内同意,直接 提交 即可

4.2 组复制单主和多主模式

single-primary mode(单写或单主模式)

单写模式 group 内只有一台节点可写可读,其他节点只可以读。当主服务器失败时,会自动选择新的主 服务器

multi-primary mode(多写或多主模式)

组内的所有机器都是 primary 节点,同时可以进行读写操作,并且数据是最终一致的。

4.3.实现mysql组复制

先关闭mysql

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

Shutting down MySQL............ SUCCESS!

将数据文件内容清空

[root@mysql-node1 ~]# rm -rf /data/mysql/*

编辑配置文件添加参数

[root@mysql-node1 ~]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

socket=/data/mysql/mysql.sock

symbolic-links=0

server-id=10——配置server唯一标识号

gtid_mode=ON——启用全局事件标识

enforce-gtid-consistency=ON——强制gtid一致

disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"——禁用指定存储 引擎

master_info_repository=TABLE——复制事件数据到表中而不记录在数据目录中

relay_log_info_repository=TABLE

binlog_checksum=NONE——禁止对二进制日志校验

log_slave_updates=ON——打开数据库中继

log_bin=binlog——重新指定log名称

binlog_format=ROW——使用行日志格式

plugin_load_add='group_replication.so'——加载组复制插件

transaction_write_set_extraction=XXHASH64——把每个事件编码为加密散列

group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"——通知插件正 式加入或创建的组名,名称为uuid格式

group_replication_start_on_boot=off——在server启动时不自动启动组复 制

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——不随系统自启而启动,只在初始成员主机中手动开启, 需要在两种情况下做设定:1.初始化建组时 2.关闭并重新启动整个组时

group_replication_single_primary_mode=OFF——使用多主模式

group_replication_enforce_update_everywhere_checks=ON——组同步中有任何改变 检测更新

group_replication_allow_local_disjoint_gtids_join=1——放弃自己信息以 master事件为主

开始重新初始化

[root@mysql-node1 ~]# mysqld --user=mysql --initialize

开启数据库

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

Starting MySQL.Logging to '/data/mysql/mysql-node1.err'.

 SUCCESS!

使用初始密码登录数据库

[root@mysql-node1 ~]# mysql -uroot -p'X4g/a_0P&4z7'

修改密码

mysql> alter user root@localhost identified by '123';

Query OK, 0 rows affected (0.01 sec)

mysql> SET SQL_LOG_BIN=0;

创建可以远程登录的用户并授权

Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER 'repl'@'%' IDENTIFIED BY '123';

Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';

Query OK, 0 rows affected (0.01 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 MASTER TO MASTER_USER='jcl', MASTER_PASSWORD='123' FOR CHANNEL 'group_replication_recovery';

Query OK, 0 rows affected, 2 warnings (0.01 sec)

指定初始成员只在第一台主机中执行

mysql> set global group_replication_bootstrap_group=on;

Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;

Query OK, 0 rows affected, 1 warning (2.11 sec)

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

在20和30上关闭数据库并删除数据文件重新初始化,开启服务

编辑配置文件同10上只更改

group_replication_local_address="172.25.254.20 & 30:33061"

登录数据库更改密码

配置mysql

同上,只不做mysql> set global group_replication_bootstrap_group=on;

[root@mysql-node1&2&3 ~]# vim /etc/hosts

172.25.254.10      mysql-node1

172.25.254.20      mysql-node2

172.25.254.30      mysql-node3

添加完组成员可以看到在三台主机都有组成员表且各节点成员状态都是online

mysql> SELECT * FROM performance_schema.replication_group_members;

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

| group_replication_applier | 4870f873-62dc-11ef-8849-000c291d64ff | mysql-node1 |        3306 | ONLINE       |

| group_replication_applier | 6f447777-62dd-11ef-93e5-000c298ab160 | mysql-node3 |        3306 | ONLINE       |

| group_replication_applier | fe6ba8d0-62dc-11ef-8ad9-000c2972a3ac | mysql-node2 |        3306 | ONLINE       |

+---------------------------+--------------------------------------+-------------+-------------+--------------+

3 rows in set (0.00 sec)

测试:

在10上创建数据库jcl

在20上也可以创建表userlist

在30上插入数据

数据添加结束后三台主机上都可以看到相同的数据。

五、mysql-router(mysql路由)

MySQL Router :

        一个对应用程序透明的InnoDB Cluster连接路由服务,提供负载均衡、应用连接故障转移和客户端路 由。 利用路由器的连接路由特性,用户可以编写应用程序来连接到路由器,并令路由器使用相应的路由策略 来处理连接,使其连接到正确的MySQL数据库服务器。

Mysql route的部署方式

这里没有重新部署独立一台的router,是直接在10上安装相应的软件包的

关闭10上的数据库

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

查看是否关闭

[root@mysql-node1 ~]# netstat -atunlpe | grep mysql

将mysqlrouter的软件包拖进家目录下

[root@mysql-node1 ~]# ls

mysql-router-community-8.4.0-1.el7.x86_64.rpm

安装软件

[root@mysql-node1 ~]# rpm -ivh mysql-router-community-8.4.0-1.el7.x86_64.rpm

warning: mysql-router-community-8.4.0-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID a8d3785c: NOKEY

Preparing...                          ################################# [100%]

Updating / installing...

   1:mysql-router-community-8.4.0-1.el################################# [100%]

查看安装软件后的文件

[root@mysql-node1 ~]# rpm -ql mysql-router-community

编辑配置文件添加路由语句块

[root@mysql-node1 ~]# 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 #表示轮询登录

开启服务

[root@mysql-node1 ~]# systemctl start mysqlrouter.service

在其他两台20和30主机上创建远程登录用户

[root@mysql-node2 &3 ~]# mysql -p123

mysql> create user root@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)

mysql> grant all on root.* to root@'%';

Query OK, 0 rows affected (0.00 sec)

测试连接10两次分别调度到20 30上:

[root@mysql-node1 ~]# mysql -uroot -p123 -h 172.25.254.10 -P 7001

六、mysql高可用之MHA

6.1介绍

为什么要用MHA?

        Master的单点故障问题

什么是 MHA?

MHA(Master High Availability)是一套优秀的MySQL高可用环境下故障切换和主从复制的软件。
MHA 的出现就是解决MySQL 单点的问题。
MySQL故障切换过程中,MHA能做到0-30秒内自动完成故障切换操作。
MHA能在故障切换的过程中最大程度上保证数据的一致性,以达到真正意义上的高可用。

MHA 的组成:

MHA由两部分组成:MHAManager (管理节点) MHA Node (数据库节点),
MHA Manager 可以单独部署在一台独立的机器上管理多个master-slave集群,也可以部署在一台 slave 节点上。
MHA Manager 会定时探测集群中的 master 节点。
当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 master, 然后将所有其他的 slave 重新指向新的 master。

MHA 的特点:

自动故障切换过程中,MHA从宕机的主服务器上保存二进制日志,最大程度的保证数据不丢失
使用半同步复制,可以大大降低数据丢失的风险,如果只有一个slave已经收到了最新的二进制日 志,MHA可以将最新的二进制日志应用于其他所有的slave服务器上,因此可以保证所有节点的数 据一致性
目前MHA支持一主多从架构,最少三台服务,即一主两从

故障切换备选主库的算法:

1.一般判断从库的是从(position/GTID)判断优劣,数据有差异,最接近于master的slave,成为备选 主。
2.数据一致的情况下,按照配置文件顺序,选择备选主库。

3.设定有权重(candidate_master=1),按照权重强制指定备选主。

(1)默认情况下如果一个slave落后master 100M的relay logs的话,即使有权重,也会失效。

(2)如果check_repl_delay=0的话,即使落后很多日志,也强制选择其为备选主。

MHA工作原理:

目前MHA主要支持一主多从的架构,要搭建MHA,要求一个复制集群必须最少有3台数据库服务器, 一主二从,即一台充当Master,台充当备用Master,另一台充当从库。
MHA Node 运行在每台 MySQL 服务器上
MHAManager 会定时探测集群中的master 节点
当master 出现故障时,它可以自动将最新数据的slave 提升为新的master
然后将所有其他的slave 重新指向新的master,VIP自动漂移到新的master。
整个故障转移过程对应用程序完全透明

6.2环境配置

先做免密登录

[root@mysql-mha ~]# ssh-keygen——生成密钥

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Created directory '/root/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

SHA256:hJ8rwXhopRk0vubAr/cSdpokxmnLoP7vVpJ87Vny0i4 root@mysql-mha

The key's randomart image is:

+---[RSA 2048]----+

|    o            |

|   o . .         |

|    o o .        |

| .   X o .       |

| .o.X = S        |

|. **+=.+ + .     |

|.= =o== o *      |

|. o.=. . E o     |

|..oo+=.   +.     |

+----[SHA256]-----+

四台主机都要添加域名

[root@mysql-mha ~]# vim /etc/hosts

172.25.254.50      mysql-mha

172.25.254.10      mysql-node1

172.25.254.20      mysql-node2

172.25.254.30      mysql-node3

将公钥分发

[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.10

[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.20

[root@mysql-mha ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.25.254.30

[root@mysql-mha ~]# vim /etc/ssh/sshd_config

UseDNS no

[root@mysql-mha ~]# cd ./.ssh/

[root@mysql-mha .ssh]# ls

id_rsa  id_rsa.pub  known_hosts

私钥分发

[root@mysql-mha .ssh]# scp id_rsa root@172.25.254.10:/root/.ssh/id_rsa

id_rsa                                                   100% 1675     2.1MB/s   00:00

[root@mysql-mha .ssh]# scp id_rsa root@172.25.254.20:/root/.ssh/id_rsa

id_rsa                                                   100% 1675     2.0MB/s   00:00

[root@mysql-mha .ssh]# scp id_rsa root@172.25.254.30:/root/.ssh/id_rsa

id_rsa                                                   100% 1675     2.2MB/s   00:00

设置半同步的主从模式

master

[root@mysql-node1 mysql]# /etc/init.d/mysqld stop

Shutting down MySQL............ SUCCESS!

[root@mysql-node1 mysql]# rm -fr /data/mysql/

[root@mysql-node1 mysql]# ls

[root@mysql-node1 mysql]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
server-id=10
log-bin=mysql-bin
gtid-mode=ON
log_slave_updates=ON
enforce-gtid-consistency=ON
symbolic-links=0

[root@mysql-node1 mysql]# mysqld --user=mysql --initialize

[root@mysql-node1 mysql]# /etc/init.d/mysqld start

Starting MySQL.Logging to '/data/mysql/mysql-node1.err'.

 SUCCESS!

[root@mysql-node1 mysql]# mysql -uroot -p'c+=zM6<AD=&s'

mysql> alter user

    -> root@localhost identified by '123';

Query OK, 0 rows affected (0.01 sec)

mysql> CREATE USER 'jcl'@'%' IDENTIFIED BY '123';

Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO jcl@'%';

Query OK, 0 rows affected (0.00 sec)

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

Query OK, 0 rows affected (0.01 sec)

mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;

Query OK, 0 rows affected (0.00 sec)

mysql> create user root@'%' identified by '123';

Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to root@'%';

Query OK, 0 rows affected (0.00 sec)

slave

[root@mysql-node2 ~]# vim /etc/my.cnf

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
server-id=10
log-bin=mysql-bin
gtid-mode=ON
log_slave_updates=ON
enforce-gtid-consistency=ON
symbolic-links=0

将数据库重新初始化,确保数据一致性且为空

[root@mysql-node2 ~]# /etc/init.d/mysqld stop——先将数据库关闭

Shutting down MySQL............ SUCCESS!

[root@mysql-node2 ~]# rm -fr /data/mysql/*——删除数据文件

[root@mysql-node2 ~]# mysqld --user mysql --initialize——初始化

[root@mysql-node2 ~]# /etc/init.d/mysqld start——开启数据库

Starting MySQL.Logging to '/data/mysql/mysql-node2.err'.

 SUCCESS!

[root@mysql-node2 ~]# mysql -p'l1yx:lPEo7zv'——用初始化密码登录mysql

mysql> alter user root@localhost identified by '123';——更改密码

Query OK, 0 rows affected (0.00 sec)

mysql>  CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='jcl',MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;——加入master

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;——开启slave

Query OK, 0 rows affected (0.00 sec)

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

Query OK, 0 rows affected (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 (0.00 sec)

mysql> START SLAVE IO_THREAD;

Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.25.254.10

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 839

               Relay_Log_File: mysql-node2-relay-bin.000003

                Relay_Log_Pos: 454

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

6.3安装MHA所需要的软件

mha上安装软件

[root@mysql-mha ~]# ls

anaconda-ks.cfg  Documents  initial-setup-ks.cfg  MHA-7.zip  Pictures  Templates

Desktop          Downloads  ks.cfg                Music      Public    Videos

[root@mysql-mha ~]# unzip MHA-7.zip

[root@mysql-mha ~]# cd MHA-7/

[root@mysql-mha MHA-7]# yum install *.rpm -y

[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.10:/mnt

mha4mysql-node-0.58-0.el7.centos.noarch.rpm              100%   35KB  19.9MB/s   00:00

[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.20:/mnt

mha4mysql-node-0.58-0.el7.centos.noarch.rpm              100%   35KB  11.5MB/s   00:00

[root@mysql-mha MHA-7]# scp mha4mysql-node-0.58-0.el7.centos.noarch.rpm root@172.25.254.30:/mnt

mha4mysql-node-0.58-0.el7.centos.noarch.rpm              100%   35KB  27.7MB/s   00:00

[root@mysql-node1 & 2 & 3]# yum install /mnt/mha4mysql-node-0.58-0.el7.centos.noarch.rpm -y

6.4编辑mha的配置文件

[root@mysql-mha MHA-7]# 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

[root@mysql-mha conf]# vim /etc/masterha/app1.cnf

[server default]

user=root——mysql管理员用户,因为需要做自动化配置

password=123——mysql密码

ssh_user=root——ssh远程登陆用户

repl_user=jcl——mysql主从复制中负责认证的用户
repl_password=123——mysql主从复制中负责认证的用户密码

master_binlog_dir= /data/mysql——二进制日志目录

remote_workdir=/tmp——远程工作目录

secondary_check_script= masterha_secondary_check -s 172.25.254.10 -s 172.25.254.11——此参数使为了提供冗余检测,方式是mha主机网络自身的问题无法连接数据库节点,应为集群之外的主机

ping_interval=3——每隔3秒检测一次

# master_ip_failover_script= /script/masterha/master_ip_failover——发生故障后调用的脚本,用来迁移vip

# shutdown_script= /script/masterha/power_manager——电源管理脚本

# report_script= /script/masterha/send_report——当发生故障后用此脚本发邮件或者告警通知

# master_ip_online_change_script= /script/masterha/master_ip_online_change——在线切换时调用的vip迁移脚本,手动

[server default]

manager_workdir=/var/log/masterha/app1——mha工作目录

manager_log=/var/log/masterha/app1/manager.log——mha日志

[server1]

hostname=172.25.254.10

candidate_master=1——可能作为master的主机

check_repl_delay=0——这个参数对于设置了candidate_master=1的主机非常有用,这个候选主在切换的过程中一定是新的master

[server2]

hostname=172.25.254.20

candidate_master=1——可能作为master的主机

check_repl_delay=0

[server3]

hostname=172.25.254.30

no_master=1——不会作为master的主机

6.5检测

检测免密

[root@mysql-mha conf]# masterha_check_ssh  --conf=/etc/masterha/app1.cnf

Sun Aug 25 22:39:10 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Sun Aug 25 22:39:10 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Sun Aug 25 22:39:10 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Sun Aug 25 22:39:10 2024 - [info] Starting SSH connection tests..

Sun Aug 25 22:39:11 2024 - [debug]

Sun Aug 25 22:39:10 2024 - [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)..

Warning: Permanently added '172.25.254.20' (ECDSA) to the list of known hosts.

Sun Aug 25 22:39:10 2024 - [debug]   ok.

Sun Aug 25 22:39:10 2024 - [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)..

Sun Aug 25 22:39:10 2024 - [debug]   ok.

Sun Aug 25 22:39:11 2024 - [debug]

Sun Aug 25 22:39:10 2024 - [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)..

Sun Aug 25 22:39:10 2024 - [debug]   ok.

Sun Aug 25 22:39:10 2024 - [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)..

Warning: Permanently added '172.25.254.30' (ECDSA) to the list of known hosts.

Sun Aug 25 22:39:11 2024 - [debug]   ok.

Sun Aug 25 22:39:12 2024 - [debug]

Sun Aug 25 22:39:11 2024 - [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)..

Warning: Permanently added '172.25.254.10' (ECDSA) to the list of known hosts.

Sun Aug 25 22:39:11 2024 - [debug]   ok.

Sun Aug 25 22:39:11 2024 - [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)..

Warning: Permanently added '172.25.254.20' (ECDSA) to the list of known hosts.

Sun Aug 25 22:39:11 2024 - [debug]   ok.

Sun Aug 25 22:39:12 2024 - [info] All SSH connection tests passed successfully.

检测一主两从

[root@mysql-mha conf]# masterha_check_repl --conf=/etc/masterha/app1.cnf

Sun Aug 25 22:41:21 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Sun Aug 25 22:41:21 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Sun Aug 25 22:41:21 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Sun Aug 25 22:41:21 2024 - [info] MHA::MasterMonitor version 0.58.

Sun Aug 25 22:41:22 2024 - [info] GTID failover mode = 1

Sun Aug 25 22:41:22 2024 - [info] Dead Servers:

Sun Aug 25 22:41:22 2024 - [info] Alive Servers:

Sun Aug 25 22:41:22 2024 - [info]   172.25.254.10(172.25.254.10:3306)

Sun Aug 25 22:41:22 2024 - [info]   172.25.254.20(172.25.254.20:3306)

Sun Aug 25 22:41:22 2024 - [info]   172.25.254.30(172.25.254.30:3306)

Sun Aug 25 22:41:22 2024 - [info] Alive Slaves:

Sun Aug 25 22:41:22 2024 - [info]   172.25.254.20(172.25.254.20:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Sun Aug 25 22:41:22 2024 - [info]     GTID ON

Sun Aug 25 22:41:22 2024 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)

Sun Aug 25 22:41:22 2024 - [info]     Primary candidate for the new Master (candidate_master is set)

Sun Aug 25 22:41:22 2024 - [info]   172.25.254.30(172.25.254.30:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Sun Aug 25 22:41:22 2024 - [info]     GTID ON

Sun Aug 25 22:41:22 2024 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)

Sun Aug 25 22:41:22 2024 - [info]     Not candidate for the new Master (no_master is set)

Sun Aug 25 22:41:22 2024 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)

Sun Aug 25 22:41:22 2024 - [info] Checking slave configurations..

Sun Aug 25 22:41:22 2024 - [info]  read_only=1 is not set on slave 172.25.254.20(172.25.254.20:3306).

Sun Aug 25 22:41:22 2024 - [info]  read_only=1 is not set on slave 172.25.254.30(172.25.254.30:3306).

Sun Aug 25 22:41:22 2024 - [info] Checking replication filtering settings..

Sun Aug 25 22:41:22 2024 - [info]  binlog_do_db= , binlog_ignore_db=

Sun Aug 25 22:41:22 2024 - [info]  Replication filtering check ok.

Sun Aug 25 22:41:22 2024 - [info] GTID (with auto-pos) is supported. Skipping all SSH and Node package checking.

Sun Aug 25 22:41:22 2024 - [info] Checking SSH publickey authentication settings on the current master..

Sun Aug 25 22:41:22 2024 - [info] HealthCheck: SSH to 172.25.254.10 is reachable.

Sun Aug 25 22:41:22 2024 - [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)

Sun Aug 25 22:41:22 2024 - [info] Checking replication health on 172.25.254.20..

Sun Aug 25 22:41:22 2024 - [info]  ok.

Sun Aug 25 22:41:22 2024 - [info] Checking replication health on 172.25.254.30..

Sun Aug 25 22:41:22 2024 - [info]  ok.

Sun Aug 25 22:41:22 2024 - [warning] master_ip_failover_script is not defined.

Sun Aug 25 22:41:22 2024 - [warning] shutdown_script is not defined.

Sun Aug 25 22:41:22 2024 - [info] Got exit code 0 (Not master dead).

MySQL Replication Health is OK.

6.6MHA的切换方式

6.6.1无故障手动切换

[root@mysql-mha conf]# 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 26 03:05:30 2024 - [info] MHA::MasterRotate version 0.58.

Mon Aug 26 03:05:30 2024 - [info] Starting online master switch..

Mon Aug 26 03:05:30 2024 - [info]

Mon Aug 26 03:05:30 2024 - [info] * Phase 1: Configuration Check Phase..

Mon Aug 26 03:05:30 2024 - [info]

Mon Aug 26 03:05:30 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Mon Aug 26 03:05:30 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Mon Aug 26 03:05:30 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Mon Aug 26 03:05:31 2024 - [info] GTID failover mode = 1

Mon Aug 26 03:05:31 2024 - [info] Current Alive Master: 172.25.254.10(172.25.254.10:3306)

Mon Aug 26 03:05:31 2024 - [info] Alive Slaves:

Mon Aug 26 03:05:31 2024 - [info]   172.25.254.20(172.25.254.20:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:05:31 2024 - [info]     GTID ON

Mon Aug 26 03:05:31 2024 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)

Mon Aug 26 03:05:31 2024 - [info]     Primary candidate for the new Master (candidate_master is set)

Mon Aug 26 03:05:31 2024 - [info]   172.25.254.30(172.25.254.30:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:05:31 2024 - [info]     GTID ON

Mon Aug 26 03:05:31 2024 - [info]     Replicating from 172.25.254.10(172.25.254.10:3306)

Mon Aug 26 03:05:31 2024 - [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 26 03:05:34 2024 - [info] Executing FLUSH NO_WRITE_TO_BINLOG TABLES. This may take long time..

Mon Aug 26 03:05:34 2024 - [info]  ok.

Mon Aug 26 03:05:34 2024 - [info] Checking MHA is not monitoring or doing failover..

Mon Aug 26 03:05:34 2024 - [info] Checking replication health on 172.25.254.20..

Mon Aug 26 03:05:34 2024 - [info]  ok.

Mon Aug 26 03:05:34 2024 - [info] Checking replication health on 172.25.254.30..

Mon Aug 26 03:05:34 2024 - [info]  ok.

Mon Aug 26 03:05:34 2024 - [info] 172.25.254.20 can be new master.

Mon Aug 26 03:05:34 2024 - [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 26 03:05:35 2024 - [info] Checking whether 172.25.254.20(172.25.254.20:3306) is ok for the new master..

Mon Aug 26 03:05:35 2024 - [info]  ok.

Mon Aug 26 03:05:35 2024 - [info] ** Phase 1: Configuration Check Phase completed.

Mon Aug 26 03:05:35 2024 - [info]

Mon Aug 26 03:05:35 2024 - [info] * Phase 2: Rejecting updates Phase..

Mon Aug 26 03:05:35 2024 - [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 26 03:05:36 2024 - [info] Locking all tables on the orig master to reject updates from everybody (including root):

Mon Aug 26 03:05:36 2024 - [info] Executing FLUSH TABLES WITH READ LOCK..

Mon Aug 26 03:05:36 2024 - [info]  ok.

Mon Aug 26 03:05:36 2024 - [info] Orig master binlog:pos is mysql-bin.000004:194.

Mon Aug 26 03:05:36 2024 - [info]  Waiting to execute all relay logs on 172.25.254.20(172.25.254.20:3306)..

Mon Aug 26 03:05:36 2024 - [info]  master_pos_wait(mysql-bin.000004:194) completed on 172.25.254.20(172.25.254.20:3306). Executed 0 events.

Mon Aug 26 03:05:36 2024 - [info]   done.

Mon Aug 26 03:05:36 2024 - [info] Getting new master's binlog name and position..

Mon Aug 26 03:05:36 2024 - [info]  mysql-bin.000004:194

Mon Aug 26 03:05:36 2024 - [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='jcl', MASTER_PASSWORD='xxx';

Mon Aug 26 03:05:36 2024 - [info]

Mon Aug 26 03:05:36 2024 - [info] * Switching slaves in parallel..

Mon Aug 26 03:05:36 2024 - [info]

Mon Aug 26 03:05:36 2024 - [info] -- Slave switch on host 172.25.254.30(172.25.254.30:3306) started, pid: 72716

Mon Aug 26 03:05:36 2024 - [info]

Mon Aug 26 03:05:38 2024 - [info] Log messages from 172.25.254.30 ...

Mon Aug 26 03:05:38 2024 - [info]

Mon Aug 26 03:05:36 2024 - [info]  Waiting to execute all relay logs on 172.25.254.30(172.25.254.30:3306)..

Mon Aug 26 03:05:36 2024 - [info]  master_pos_wait(mysql-bin.000004:194) completed on 172.25.254.30(172.25.254.30:3306). Executed 0 events.

Mon Aug 26 03:05:36 2024 - [info]   done.

Mon Aug 26 03:05:36 2024 - [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 26 03:05:36 2024 - [info]  Executed CHANGE MASTER.

Mon Aug 26 03:05:37 2024 - [info]  Slave started.

Mon Aug 26 03:05:38 2024 - [info] End of log messages from 172.25.254.30 ...

Mon Aug 26 03:05:38 2024 - [info]

Mon Aug 26 03:05:38 2024 - [info] -- Slave switch on host 172.25.254.30(172.25.254.30:3306) succeeded.

Mon Aug 26 03:05:38 2024 - [info] Unlocking all tables on the orig master:

Mon Aug 26 03:05:38 2024 - [info] Executing UNLOCK TABLES..

Mon Aug 26 03:05:38 2024 - [info]  ok.

Mon Aug 26 03:05:38 2024 - [info] Starting orig master as a new slave..

Mon Aug 26 03:05:38 2024 - [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 26 03:05:38 2024 - [info]  Executed CHANGE MASTER.

Mon Aug 26 03:05:39 2024 - [info]  Slave started.

Mon Aug 26 03:05:39 2024 - [info] All new slave servers switched successfully.

Mon Aug 26 03:05:39 2024 - [info]

Mon Aug 26 03:05:39 2024 - [info] * Phase 5: New master cleanup phase..

Mon Aug 26 03:05:39 2024 - [info]

Mon Aug 26 03:05:39 2024 - [info]  172.25.254.20: Resetting slave info succeeded.

Mon Aug 26 03:05:39 2024 - [info] Switching master to 172.25.254.20(172.25.254.20:3306) completed successfully.

可以看到master切换到20上:

6.6.2故障手动切换

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

Shutting down MySQL............ SUCCESS!

[root@mysql-mha conf]# masterha_master_switch \

--master_state=dead \

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

--dead_master_host=172.25.254.20 \

--dead_master_port=3306 \

--new_master_host=172.25.254.10 \

--new_master_port=3306 \

--ignore_last_failover

--dead_master_ip=<dead_master_ip> is not set. Using 172.25.254.20.

--dead_master_port=<dead_master_port> is not set. Using 3306.

Mon Aug 26 03:10:10 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Mon Aug 26 03:10:10 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Mon Aug 26 03:10:10 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Mon Aug 26 03:10:10 2024 - [info] MHA::MasterFailover version 0.58.

Mon Aug 26 03:10:10 2024 - [info] Starting master failover.

Mon Aug 26 03:10:10 2024 - [info]

Mon Aug 26 03:10:10 2024 - [info] * Phase 1: Configuration Check Phase..

Mon Aug 26 03:10:10 2024 - [info]

Mon Aug 26 03:10:11 2024 - [info] GTID failover mode = 1

Mon Aug 26 03:10:11 2024 - [info] Dead Servers:

Mon Aug 26 03:10:11 2024 - [info]   172.25.254.20(172.25.254.20:3306)

Mon Aug 26 03:10:11 2024 - [info] Checking master reachability via MySQL(double check)...

Mon Aug 26 03:10:11 2024 - [info]  ok.

Mon Aug 26 03:10:11 2024 - [info] Alive Servers:

Mon Aug 26 03:10:11 2024 - [info]   172.25.254.10(172.25.254.10:3306)

Mon Aug 26 03:10:11 2024 - [info]   172.25.254.30(172.25.254.30:3306)

Mon Aug 26 03:10:11 2024 - [info] Alive Slaves:

Mon Aug 26 03:10:11 2024 - [info]   172.25.254.10(172.25.254.10:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:10:11 2024 - [info]     GTID ON

Mon Aug 26 03:10:11 2024 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)

Mon Aug 26 03:10:11 2024 - [info]     Primary candidate for the new Master (candidate_master is set)

Mon Aug 26 03:10:11 2024 - [info]   172.25.254.30(172.25.254.30:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:10:11 2024 - [info]     GTID ON

Mon Aug 26 03:10:11 2024 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)

Mon Aug 26 03:10:11 2024 - [info]     Not candidate for the new Master (no_master is set)

Master 172.25.254.20(172.25.254.20:3306) is dead. Proceed? (yes/NO): yes

Mon Aug 26 03:10:14 2024 - [info] Starting GTID based failover.

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] ** Phase 1: Configuration Check Phase completed.

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] * Phase 2: Dead Master Shutdown Phase..

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] HealthCheck: SSH to 172.25.254.20 is reachable.

Mon Aug 26 03:10:14 2024 - [info] Forcing shutdown so that applications never connect to the current master..

Mon Aug 26 03:10:14 2024 - [warning] master_ip_failover_script is not set. Skipping invalidating dead master IP address.

Mon Aug 26 03:10:14 2024 - [warning] shutdown_script is not set. Skipping explicit shutting down of the dead master.

Mon Aug 26 03:10:14 2024 - [info] * Phase 2: Dead Master Shutdown Phase completed.

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] * Phase 3: Master Recovery Phase..

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] * Phase 3.1: Getting Latest Slaves Phase..

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] The latest binary log file/position on all slaves is mysql-bin.000004:194

Mon Aug 26 03:10:14 2024 - [info] Retrieved Gtid Set: 347da1c1-6303-11ef-93da-000c2972a3ac:1

Mon Aug 26 03:10:14 2024 - [info] Latest slaves (Slaves that received relay log files to the latest):

Mon Aug 26 03:10:14 2024 - [info]   172.25.254.10(172.25.254.10:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:10:14 2024 - [info]     GTID ON

Mon Aug 26 03:10:14 2024 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)

Mon Aug 26 03:10:14 2024 - [info]     Primary candidate for the new Master (candidate_master is set)

Mon Aug 26 03:10:14 2024 - [info]   172.25.254.30(172.25.254.30:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:10:14 2024 - [info]     GTID ON

Mon Aug 26 03:10:14 2024 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)

Mon Aug 26 03:10:14 2024 - [info]     Not candidate for the new Master (no_master is set)

Mon Aug 26 03:10:14 2024 - [info] The oldest binary log file/position on all slaves is mysql-bin.000004:194

Mon Aug 26 03:10:14 2024 - [info] Retrieved Gtid Set: 347da1c1-6303-11ef-93da-000c2972a3ac:1

Mon Aug 26 03:10:14 2024 - [info] Oldest slaves:

Mon Aug 26 03:10:14 2024 - [info]   172.25.254.10(172.25.254.10:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:10:14 2024 - [info]     GTID ON

Mon Aug 26 03:10:14 2024 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)

Mon Aug 26 03:10:14 2024 - [info]     Primary candidate for the new Master (candidate_master is set)

Mon Aug 26 03:10:14 2024 - [info]   172.25.254.30(172.25.254.30:3306)  Version=5.7.44-log (oldest major version between slaves) log-bin:enabled

Mon Aug 26 03:10:14 2024 - [info]     GTID ON

Mon Aug 26 03:10:14 2024 - [info]     Replicating from 172.25.254.20(172.25.254.20:3306)

Mon Aug 26 03:10:14 2024 - [info]     Not candidate for the new Master (no_master is set)

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] * Phase 3.3: Determining New Master Phase..

Mon Aug 26 03:10:14 2024 - [info]

Mon Aug 26 03:10:14 2024 - [info] 172.25.254.10 can be new master.

Mon Aug 26 03:10:14 2024 - [info] New master is 172.25.254.10(172.25.254.10:3306)

Mon Aug 26 03:10:14 2024 - [info] Starting master failover..

Mon Aug 26 03:10:14 2024 - [info]

From:

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)

To:

172.25.254.10(172.25.254.10:3306) (new master)

 +--172.25.254.30(172.25.254.30:3306)

Starting master switch from 172.25.254.20(172.25.254.20:3306) to 172.25.254.10(172.25.254.10:3306)? (yes/NO): yes

Mon Aug 26 03:10:16 2024 - [info] New master decided manually is 172.25.254.10(172.25.254.10:3306)

Mon Aug 26 03:10:16 2024 - [info]

Mon Aug 26 03:10:16 2024 - [info] * Phase 3.3: New Master Recovery Phase..

Mon Aug 26 03:10:16 2024 - [info]

Mon Aug 26 03:10:16 2024 - [info]  Waiting all logs to be applied..

Mon Aug 26 03:10:16 2024 - [info]   done.

Mon Aug 26 03:10:16 2024 - [info] Getting new master's binlog name and position..

Mon Aug 26 03:10:16 2024 - [info]  mysql-bin.000004:438

Mon Aug 26 03:10:16 2024 - [info]  All other slaves should start replication from here. Statement should be: CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='jcl', MASTER_PASSWORD='xxx';

Mon Aug 26 03:10:16 2024 - [info] Master Recovery succeeded. File:Pos:Exec_Gtid_Set: mysql-bin.000004, 438, 347da1c1-6303-11ef-93da-000c2972a3ac:1,

b1367cca-6302-11ef-a3aa-000c291d64ff:1-6

Mon Aug 26 03:10:16 2024 - [warning] master_ip_failover_script is not set. Skipping taking over new master IP address.

Mon Aug 26 03:10:16 2024 - [info] Setting read_only=0 on 172.25.254.10(172.25.254.10:3306)..

Mon Aug 26 03:10:16 2024 - [info]  ok.

Mon Aug 26 03:10:16 2024 - [info] ** Finished master recovery successfully.

Mon Aug 26 03:10:16 2024 - [info] * Phase 3: Master Recovery Phase completed.

Mon Aug 26 03:10:16 2024 - [info]

Mon Aug 26 03:10:16 2024 - [info] * Phase 4: Slaves Recovery Phase..

Mon Aug 26 03:10:16 2024 - [info]

Mon Aug 26 03:10:16 2024 - [info]

Mon Aug 26 03:10:16 2024 - [info] * Phase 4.1: Starting Slaves in parallel..

Mon Aug 26 03:10:16 2024 - [info]

Mon Aug 26 03:10:16 2024 - [info] -- Slave recovery on host 172.25.254.30(172.25.254.30:3306) started, pid: 73590. Check tmp log /var/log/masterha/app1/172.25.254.30_3306_20240826031010.log if it takes time..

Mon Aug 26 03:10:18 2024 - [info]

Mon Aug 26 03:10:18 2024 - [info] Log messages from 172.25.254.30 ...

Mon Aug 26 03:10:18 2024 - [info]

Mon Aug 26 03:10:16 2024 - [info]  Resetting slave 172.25.254.30(172.25.254.30:3306) and starting replication from the new master 172.25.254.10(172.25.254.10:3306)..

Mon Aug 26 03:10:16 2024 - [info]  Executed CHANGE MASTER.

Mon Aug 26 03:10:17 2024 - [info]  Slave started.

Mon Aug 26 03:10:17 2024 - [info]  gtid_wait(347da1c1-6303-11ef-93da-000c2972a3ac:1,

b1367cca-6302-11ef-a3aa-000c291d64ff:1-6) completed on 172.25.254.30(172.25.254.30:3306). Executed 0 events.

Mon Aug 26 03:10:18 2024 - [info] End of log messages from 172.25.254.30.

Mon Aug 26 03:10:18 2024 - [info] -- Slave on host 172.25.254.30(172.25.254.30:3306) started.

Mon Aug 26 03:10:18 2024 - [info] All new slave servers recovered successfully.

Mon Aug 26 03:10:18 2024 - [info]

Mon Aug 26 03:10:18 2024 - [info] * Phase 5: New master cleanup phase..

Mon Aug 26 03:10:18 2024 - [info]

Mon Aug 26 03:10:18 2024 - [info] Resetting slave info on the new master..

Mon Aug 26 03:10:18 2024 - [info]  172.25.254.10: Resetting slave info succeeded.

Mon Aug 26 03:10:18 2024 - [info] Master failover to 172.25.254.10(172.25.254.10:3306) completed successfully.

Mon Aug 26 03:10:18 2024 - [info]

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

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

Master 172.25.254.20(172.25.254.20:3306) is down!

Check MHA Manager logs at mysql-mha for details.

Started manual(interactive) failover.

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

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

172.25.254.30(172.25.254.30:3306): OK: Slave started, replicating from 172.25.254.10(172.25.254.10:3306)

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

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

切换到10上

恢复故障20自动成为salve

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

Starting MySQL. SUCCESS!

[root@mysql-node2 ~]# mysql -p123

mysql> stop slave ;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='jcl',MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;

Query OK, 0 rows affected (0.01 sec)

mysql> show  slave status\G;

6.6.3自动检测切换

将master停掉模拟故障

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

Shutting down MySQL........... SUCCESS!

[root@mysql-mha masterha]# cd /var/log/masterha/app1

[root@mysql-mha app1]# ls

app1.failover.complete

将锁文件删除否者自动切换master切换不了

[root@mysql-mha app1]# rm app1.failover.complete

[root@mysql-mha masterha]# masterha_manager --conf=/etc/masterha/app1.cnf

Mon Aug 26 03:18:36 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Mon Aug 26 03:18:36 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Mon Aug 26 03:18:36 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Mon Aug 26 03:18:48 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Mon Aug 26 03:18:48 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Mon Aug 26 03:18:48 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

在20上可以看到master信息

mysql> show  master status\G;

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

             File: mysql-bin.000005

         Position: 194

     Binlog_Do_DB:

 Binlog_Ignore_DB:

Executed_Gtid_Set: 347da1c1-6303-11ef-93da-000c2972a3ac:1,

b1367cca-6302-11ef-a3aa-000c291d64ff:1-6

1 row in set (0.00 sec)

ERROR:

No query specified

恢复故障

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

Starting MySQL. SUCCESS!

[root@mysql-node1 ~]# mysql -p123

mysql> stop slave;

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.20', MASTER_USER='jcl',MASTER_PASSWORD='123', MASTER_AUTO_POSITION=1;

Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.25.254.20

                  Master_User: jcl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 194

               Relay_Log_File: mysql-node1-relay-bin.000002

                Relay_Log_Pos: 367

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

6.6.4为MHA添加VIP功能

MHA主机上

启用VIP

[root@mysql-mha ~]# ls

anaconda-ks.cfg  Downloads             master_ip_failover  Music     Templates

Desktop          initial-setup-ks.cfg  MHA-7               Pictures  Videos

Documents        ks.cfg                MHA-7.zip           Public

[root@mysql-mha ~]# ls

anaconda-ks.cfg  Downloads             master_ip_failover       MHA-7.zip  Public

Desktop          initial-setup-ks.cfg  master_ip_online_change  Music      Templates

Documents        ks.cfg                MHA-7                    Pictures   Videos

[root@mysql-mha ~]# cp master_ip_* /usr/local/bin/

[root@mysql-mha ~]# chmod +x /usr/local/bin/master_ip_*

[root@mysql-mha ~]# vim /usr/local/bin/master_ip_failover

my $vip = '172.25.254.100/24';

[root@mysql-mha ~]# vim /usr/local/bin/master_ip_online_change

my $vip = '172.25.254.100/24';

[root@mysql-mha ~]# vim /etc/masterha/app1.cnf

 master_ip_failover_script= /usr/local/bin/master_ip_failover
# shutdown_script= /script/masterha/power_manager
# report_script= /script/masterha/send_report
 master_ip_online_change_script= /usr/local/bin/master_ip_online_change
 

master主机上

master主机配置VIP

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

[root@mysql-node2 ~]# ip a

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:0c:29:72:a3:ac brd ff:ff:ff:ff:ff:ff

    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

测试:

模拟故障

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

Shutting down MySQL............ SUCCESS!

启动自动检测,master可以切换但是VIP不会跳动,因为我们配置文件中配了手动跳转

[root@mysql-mha ~]# masterha_manager --conf=/etc/masterha/app1.cnf

Mon Aug 26 04:23:27 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Mon Aug 26 04:23:27 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Mon Aug 26 04:23:27 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

Mon Aug 26 04:24:36 2024 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.

Mon Aug 26 04:24:36 2024 - [info] Reading application default configuration from /etc/masterha/app1.cnf..

Mon Aug 26 04:24:36 2024 - [info] Reading server configuration from /etc/masterha/app1.cnf..

恢复故障

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

Starting MySQL. SUCCESS!

[root@mysql-node2 ~]# mysql -p123

mysql> CHANGE MASTER TO MASTER_HOST='172.25.254.10', MASTER_USER='jcl',MASTER_PASSWORD='lee', MASTER_AUTO_POSITION=1;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

将20重新配置成master

手动切换master

[root@mysql-mha ~]# masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=172.25.254.10 --new_master_port=3306 --orig_master_is_new_slave --running_updates_limit=10000

可以查看到VIP跳转到10master上

[root@mysql-node1 ~]# ip a

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

    link/ether 00:0c:29:1d:64:ff brd ff:ff:ff:ff:ff:ff

    inet 172.25.254.10/24 brd 172.25.254.255 scope global noprefixroute eth0

       valid_lft forever preferred_lft forever

    inet 172.25.254.11/32 scope global eth0

       valid_lft forever preferred_lft forever

    inet 172.25.254.100/24 scope global secondary eth0

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值