Mysql8 high availability dual master + keepalived

1 Overview

Use keepalived to achieve high availability of Mysql database. Keepalived+Mysql dual master to achieve MYSQL-HA. We must ensure that the data of the two Mysql databases are completely consistent. The implementation method is that the two Mysql databases have a master-slave relationship with each other and configure VIP through keepalived. When one of the MySQL databases goes down, the application can automatically switch to another MySQL database to ensure high availability of the system.

2. Experimental environment

operating system Red Hat 4.8.5
MySQL version mysql 8.0.28
Keepalived version keepalibed-2.2.8
Mysql-master-1 192.168.15.94
Mysql-master-2 192.168.15.92
Mysql-VIP 192.168.15.100

3. Related configurations

3.1 Turn off the firewall

Install mysql on both machines and close firewalld.

[root@skymachine ~]# systemctl stop firewalld
[root@skymachine ~]# systemctl disable firewalld
[root@skymachine ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

3.2 Modify master-1 configuration file

To build the MySQL master, you need to configure my.cnf. Add the following content under the [mysqld] section of the master-1 library my.cnf:

vi /etc/my.cnf 

[mysqld]

log-bin=mysql-bin                               #开启主从binlog(二进制日志),主库必须开启
binlog_format=mixed                             #指定二进制日志格式为mixed,即在语句和行两种模式之间自动切换
server-id=1                                     #配置server-id(唯一)标识主机,必须与从库不一致
relay-log=relay-bin                             #指定中继日志文件的名称为relay-bin,用于在主从复制中传输数据
relay-log-index=slave-relay-bin.index           #指定中继日志索引文件的名称,用于记录中继日志文件的位置。
auto_increment_increment=2                      #指定自增长ID的增量为2,用于在主从复制中避免ID冲突。
auto_increment_offset=1                         #指定自增长ID的起始值为1,用于在主从复制中避免ID冲突。

##保存退出,并重启MySQL服务
[root@skymachine ~]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 

3.3 Modify master-2 configuration file

vi /etc/my.cnf 
​
[mysqld]
​
log-bin=mysql-bin                               #开启主从binlog(二进制日志),主库必须开启
binlog_format=mixed                             #指定二进制日志格式为mixed,即在语句和行两种模式之间自动切换
server-id=2                                    #配置server-id(唯一)标识主机,必须与从库不一致
relay-log=relay-bin                             #指定中继日志文件的名称为relay-bin,用于在主从复制中传输数据
relay-log-index=slave-relay-bin.index           #指定中继日志索引文件的名称,用于记录中继日志文件的位置。
auto_increment_increment=2                      #指定自增长ID的增量为2,用于在主从复制中避免ID冲突。
auto_increment_offset=2                         #指定自增长ID的起始值为2,用于在主从复制中避免ID冲突。
​
##保存退出,并重启MySQL服务
[root@skymachine ~]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 

4. Configure dual-master mode

4.1 Create synchronization users on two nodes respectively

Log in to mysql as root user

create user 'test'@'192.168.15.%' identified by 'Test12#$';
​
grant replication slave on *.* to 'test'@'192.168.15.%';
​
flush privileges; 
​
select user,host from mysql.user;

The following are the results of the operation

mysql> create user 'test'@'192.168.15.%' identified by 'Test12#$';
Query OK, 0 rows affected (0.20 sec)
​
mysql> grant replication slave on *.* to 'test'@'192.168.15.%';
Query OK, 0 rows affected (0.03 sec)
​
mysql> flush privileges; 
Query OK, 0 rows affected (0.03 sec)
​
mysql> select user,host from mysql.user;
+------------------+--------------+
| user             | host         |
+------------------+--------------+
| user01           | %            |
| test             | 192.168.15.% |
| mysql.infoschema | localhost    |
| mysql.session    | localhost    |
| mysql.sys        | localhost    |
| root             | localhost    |
+------------------+--------------+
6 rows in set (0.00 sec)

4.2 Configure the master server on master-1

To configure the master server on master-1, you need to first obtain the username, password, ip, port, master_log_file, file, master_log_pos and get_master_public_key of master-2

4.2.1 Obtain master-2 configuration information

In the master-2 server mysql command line, enter

show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      960 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Get file and Position content

4.2.2 Configure the main server

Return to the master-1 server mysql command line and enter the following command

stop slave;
​
reset slave;
​
change master to master_host='192.168.15.92',
master_port=3306,master_user='test',
master_password='Test12#$',
master_log_file='mysql-bin.000003',
master_log_pos=960,
get_master_public_key=1;
​
start slave;
​
show slave status\G;

The following are the running results for reference

mysql> stop slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
​
mysql> reset slave;
Query OK, 0 rows affected, 1 warning (0.07 sec)
​
mysql> change master to master_host='192.168.15.92',
    -> master_port=3306,master_user='test',
    -> master_password='Test12#$',
    -> master_log_file='mysql-bin.000003',
    -> master_log_pos=960,
    -> get_master_public_key=1;
Query OK, 0 rows affected, 10 warnings (0.08 sec)
​
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.04 sec)
​
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.15.92
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 960
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 326
        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: 960
              Relay_Log_Space: 530
              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: 2
                  Master_UUID: 820016c0-131c-11ee-895a-00505682d637
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 1
            Network_Namespace: 
1 row in set, 1 warning (0.01 sec)

When YES appears in the following two configurations, it means the configuration is successful.

Slave_IO_Running:  Yes
Slave_SQL_Running: Yes

4.3 Configure the master server on master-2

To configure the master server on master-2, you need to first obtain the user name, password, ip, port, master_log_file, file, master_log_pos and get_master_public_key of master-1

4.3.1 Obtain master-1 configuration information

At the master-1 server mysql command line, enter

show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Get file and Position content

4.3.2 Configure the main server

Return to the master-2 server mysql command line and enter the following command

stop slave;
​
reset slave;
​
change master to master_host='192.168.15.94',
master_port=3306,master_user='test',
master_password='Test12#$',
master_log_file='mysql-bin.000002',
master_log_pos=157,
get_master_public_key=1;
​
start slave;
​
show slave status\G;

The following are the running results for reference

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.03 sec)
​
mysql> reset slave all;
Query OK, 0 rows affected, 1 warning (0.26 sec)
​
mysql> change master to master_host='192.168.15.94',
    -> master_port=3306,master_user='test',
    -> master_password='Test12#$',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=157,
    -> get_master_public_key=1;
Query OK, 0 rows affected, 10 warnings (0.12 sec)
​
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.05 sec)
​
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: 192.168.15.94
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 157
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 326
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 157
              Relay_Log_Space: 530
              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: 1
                  Master_UUID: b5609587-1307-11ee-96e9-00505682d2a5
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 1
            Network_Namespace: 
1 row in set, 1 warning (0.01 sec)

When YES appears in the following two configurations, it means the configuration is successful.

Slave_IO_Running:  Yes
Slave_SQL_Running: Yes

5. Test dual master synchronization

5.1 Create a synchronization database on the master-1 host

For example, create abcd and create a test table such as t1 in abcd:

mysql> CREATE DATABASE IF NOT EXISTS abcd DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
Query OK, 1 row affected (0.04 sec)
​
mysql> use abcd;
Database changed
​
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| abcd               |
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
​
​
mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.30 sec)
​
​
mysql> show tables;
+----------------+
| Tables_in_abcd |
+----------------+
| t1             |
+----------------+
1 row in set (0.00 sec)
​
mysql> select * from t1;
Empty set (0.01 sec)

5.2 Check whether the changes of master-1 are synchronized on master-2

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| abcd               |
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.01 sec)
​
mysql> use abcd;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
​
Database changed
mysql> show tables;
+----------------+
| Tables_in_abcd |
+----------------+
| t1             |
+----------------+
1 row in set (0.00 sec)

5.3 Insert a piece of data on master-2 to check whether it is updated synchronously on master-1

master-2

mysql> insert into t1 (id,name) values (3,'李四');
Query OK, 1 row affected (0.01 sec)
​
mysql> select * from t1;
+------+--------+
| id   | name   |
+------+--------+
|    3 | 李四   |
+------+--------+
1 row in set (0.00 sec)

master-1

mysql> select * from t1;
+------+--------+
| id   | name   |
+------+--------+
|    3 | 李四   |
+------+--------+
1 row in set (0.00 sec)

Querying out the data means that the master-master configuration synchronization is successful!

6. Shut down both the primary and standby databases and then re-enable synchronization.

If both masters are shut down and need to be reconfigured, step 4

7. Configuration of main parameters

7.1 Description of status parameters

Slave_IO_state displays the status of the current IO thread. Generally, it displays the binary log waiting for the main server to be sent. 
Master_log_file displays the binary log of the currently synchronized master server. 
Read_master_log_pos displays the current offset position synchronized to the binary log on the master server. 
Relay_master_log_file The binary log of the current relay log synchronization. 
Relay_log_file displays the currently written relay log. 
Relay_log_pos displays the offset position of the current execution into the relay log. 
Slave_IO_running The running status of the IO thread in the slave server, yes represents the normal 
running status of the sql thread in the slave server Slave_SQL_running, YES represents the normal 
Exec_Master_log_pos represents the offset position of the binary log synchronized to the master server.

7.2 Common commands for starting and stopping slaves

STOP SLAVE IO_THREAD; Stop the IO process 
STOP SLAVE SQL_THREAD; Stop the SQL process 
STOP SLAVE; Stop the IO and SQL processes 
START SLAVE IO_THREAD; Start the IO process START SLAVE 
SQL_THREAD; Start the SQL process 
START SLAVE; Start the IO and SQL processes 
RESET SLAVE; Used to let The slave forgets its replication position in the master's binary log, it deletes the master.info and relay-log.info files, as well as all relay logs, and starts a new relay log when you don't need the master This operation can be performed from slave. 
SHOW SLAVE STATUS; View MySQL synchronization status 
STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1; START SLAVE; Often when mysql master-slave synchronization encounters an error, such as a primary key conflict, etc., then I need to ensure that the data in that row is consistent If you want to temporarily skip this error, you need to use the SQL_SLAVE_SKIP_COUNTER = n command. n means skipping the next n events. CHANGE 
MASTER TO MASTER_HOST='10.1.1.75', MASTER_USER='replication', MASTER_PASSWORD='123456 ', MASTER_LOG_FILE='mysql-bin.000006', MASTER_LOG_POS=106; START SLAVE; Resynchronize from the specified location

8.keepalived installation and deployment

Install keepalived on the master-1 and master-2 servers respectively. The installation steps are the same but the configuration files are different. They are introduced below.

8.1 Install dependency packages and download keepalived

Install the plug-in dependency package, download the keepalived2.2.8 version to the /opt directory and unzip it

yum -y install gcc openssl-devel popt-devel psmisc
​
yum install wget
Is this ok [y/d/N]: y              #安装wget提示,输入y
​
cd /opt/
​
wget --no-check-certificate https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
​
ll
​
tar -zxvf keepalived-2.2.8.tar.gz 

The following are the running results for reference

[root@skymachine ~]# yum -y install gcc openssl-devel popt-devel psmisc
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                                                                                                 | 1.8 kB  00:00:00     
docker-ce-stable                                                                                                                                     | 1.4 kB  00:00:00     
epel                                                                                                                                                 | 1.8 kB  00:00:00     
extras                                                                                                                                               | 1.4 kB  00:00:00     
...中间省略安装过程代码
作为依赖被安装:
  cpp.x86_64 0:4.8.5-44.el7                  glibc-devel.x86_64 0:2.17-326.el7_9    glibc-headers.x86_64 0:2.17-326.el7_9     kernel-headers.x86_64 0:3.10.0-1160.90.1.el7  
  keyutils-libs-devel.x86_64 0:1.5.8-3.el7   krb5-devel.x86_64 0:1.15.1-55.el7_9    libcom_err-devel.x86_64 0:1.42.9-19.el7   libkadm5.x86_64 0:1.15.1-55.el7_9             
  libmpc.x86_64 0:1.0.1-3.el7                libselinux-devel.x86_64 0:2.5-15.el7   libsepol-devel.x86_64 0:2.5-10.el7        libverto-devel.x86_64 0:0.2.5-4.el7           
  mpfr.x86_64 0:3.1.1-4.el7                  pcre-devel.x86_64 0:8.32-17.el7        zlib-devel.x86_64 0:1.2.7-21.el7_9       
​
作为依赖被升级:
  e2fsprogs.x86_64 0:1.42.9-19.el7        e2fsprogs-libs.x86_64 0:1.42.9-19.el7      glibc.x86_64 0:2.17-326.el7_9              glibc-common.x86_64 0:2.17-326.el7_9     
  krb5-libs.x86_64 0:1.15.1-55.el7_9      libcom_err.x86_64 0:1.42.9-19.el7          libgcc.x86_64 0:4.8.5-44.el7               libgomp.x86_64 0:4.8.5-44.el7            
  libss.x86_64 0:1.42.9-19.el7            openssl.x86_64 1:1.0.2k-26.el7_9           openssl-libs.x86_64 1:1.0.2k-26.el7_9      zlib.x86_64 0:1.2.7-21.el7_9             
​
完毕!
​
[root@skymachine opt]# yum install wget
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 wget.x86_64.0.1.14-18.el7_6.1 将被 安装
--> 解决依赖关系完成
​
依赖关系解决
​
============================================================================================================================================================================
 Package                               架构                                    版本                                             源                                     大小
============================================================================================================================================================================
正在安装:
 wget                                  x86_64                                  1.14-18.el7_6.1                                  base                                  547 k
​
事务概要
============================================================================================================================================================================
安装  1 软件包
​
总下载量:547 k
安装大小:2.0 M
Is this ok [y/d/N]: y
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm                                                                                                                      | 547 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : wget-1.14-18.el7_6.1.x86_64                                                                                                                             1/1 
  验证中      : wget-1.14-18.el7_6.1.x86_64                                                                                                                             1/1 
​
已安装:
  wget.x86_64 0:1.14-18.el7_6.1                                                                                                                                             
​
完毕!
[root@skymachine ~]# cd /opt/
​
[root@skymachine opt]# wget --no-check-certificate https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
​
--2023-06-27 09:32:19--  https://www.keepalived.org/software/keepalived-2.2.8.tar.gz
正在解析主机 www.keepalived.org (www.keepalived.org)... 91.121.30.175, 2001:41d0:1:71af::1
正在连接 www.keepalived.org (www.keepalived.org)|91.121.30.175|:443... 已连接。
警告: 无法验证 www.keepalived.org 的由 “/C=US/O=Let's Encrypt/CN=R3” 颁发的证书:
  颁发的证书已经过期。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1202602 (1.1M) [application/octet-stream]
正在保存至: “keepalived-2.2.8.tar.gz”
​
100%[==================================================================================================================================>] 1,202,602    868KB/s 用时 1.4s   
​
2023-06-27 09:32:21 (868 KB/s) - 已保存 “keepalived-2.2.8.tar.gz” [1202602/1202602])
​
[root@skymachine opt]# ll
总用量 1180
-rw-r--r--  1 root root 1202602 5月  31 18:37 keepalived-2.2.8.tar.gz
drwxr-xr-x  3 root root      75 6月  25 13:49 software
​
[root@skymachine opt]# tar -zxvf keepalived-2.2.8.tar.gz 
keepalived-2.2.8/
keepalived-2.2.8/tools/
keepalived-2.2.8/tools/timed_reload
keepalived-2.2.8/Dockerfile.in
keepalived-2.2.8/Makefile.in
keepalived-2.2.8/build-aux/
...中间省略安装过程代码
keepalived-2.2.8/keepalived/check/check_file.c
keepalived-2.2.8/keepalived/check/check_tcp.c
keepalived-2.2.8/keepalived/check/libipvs.c
keepalived-2.2.8/configure.ac
keepalived-2.2.8/aclocal.m4

8.2 Configure keepalived and compile

cd keepalived-2.2.8
​
 ./configure --prefix=/opt/keepalived-2.2.8
​
make && make install

The following are the running results for reference

[root@skymachine opt]# cd keepalived-2.2.8
​
[root@skymachine keepalived-2.2.8]#   ./configure --prefix=/opt/keepalived-2.2.8
​
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether make supports nested variables... (cached) yes
​
...中间省略安装过程代码
​
Strict config checks     : No
Build documentation      : No
Default runtime options  : -D
​
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
​
[root@skymachine keepalived-2.2.8]#  make && make install
Making all in lib
make[1]: 进入目录“/opt/keepalived-2.2.8/lib”
make  all-am
make[2]: 进入目录“/opt/keepalived-2.2.8/lib”
  CC       memory.o
  CC       utils.o
utils.c: 在函数‘dump_buffer’中:
utils.c:106:5: 警告:无法优化可能无穷的循环 [-Wunsafe-loop-optimizations]
     for (j = i - 15; j <= i; j++)
     ^
     
...中间省略安装过程代码
​
make[3]: 对“install-data-hook”无需做任何事。
make[3]: 离开目录“/opt/keepalived-2.2.8”
make[2]: 离开目录“/opt/keepalived-2.2.8”
make[1]: 离开目录“/opt/keepalived-2.2.8”

8.3 Copy the files to the corresponding directory

[root@128 keepalived-2.2.7]# mkdir /etc/keepalived
[root@128 keepalived-2.2.7]# cp keepalived/etc/keepalived/keepalived.conf.sample /etc/keepalived/keepalived.conf
[root@128 keepalived-2.2.7]# cp keepalived/etc/init.d/keepalived /etc/init.d/
[root@128 keepalived-2.2.7]# cp keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@128 keepalived-2.2.7]# cp bin/keepalived /usr/sbin/

8.4 Create a new shutdown.sh file

vi /etc/keepalived/keepalived.conf
The content is
#!/bin/bash
#该脚本是在mysql服务出现异常时,将keepalived应用停止,从而使虚拟vip主机自动连接到另一台mysql上
killall keepalived

Save and exit, set execution permission to executable

[root@skymachine keepalived]# chmod +x /etc/keepalived/shutdown.sh

8.5 Check the network card name with ifconfig

The name of the local network card is ens192

[root@skymachine keepalived-2.2.8]# ifconfig
ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.15.94  netmask 255.255.255.0  broadcast 192.168.15.255
        inet6 fe80::5952:cea:7d3a:9438  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::e689:8ec0:4cf9:2338  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::ce71:1610:b52d:de15  prefixlen 64  scopeid 0x20<link>
        ether 00:50:56:82:d1:76  txqueuelen 1000  (Ethernet)
        RX packets 737719  bytes 964242277 (919.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 398867  bytes 29004910 (27.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
​
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 58  bytes 5076 (4.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 58  bytes 5076 (4.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

8.6 Modify the master-1 server keepalived configuration file

Rename the original keepalived.conf to keepalived_bak.conf, and then create a new keepalived.conf configuration file

cd /etc/keepalived/
[root@skymachine keepalived]# ll
总用量 4
-rw-r--r-- 1 root root 3550 6月  27 09:34 keepalived.conf
[root@skymachine keepalived]# mv keepalived.conf  keepalived_bak.conf
[root@skymachine keepalived]# ll
总用量 4
-rw-r--r-- 1 root root 3550 6月  27 09:34 keepalived_bak.conf
[root@skymachine keepalived]#  vi /etc/keepalived/keepalived.conf

Copy the following content into

! Configuration File for keepalived
​
#主要配置故障发生时的通知对象及机器标识
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MYSQL-1                   #主机标识符,唯一即可
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
​
#用来定义对外提供服务的VIP区域及相关属性
vrrp_instance VI_1 {
    state BACKUP                     #表示keepalived角色,都是设成BACKUP则以优先级为主要参考
    interface ens192                 #指定HA监听的网络接口,刚才ifconfig查看的接口名称
    virtual_router_id 151            #虚拟路由标识,取值0-255,master-1和master-2保持一致
    priority 100                     #优先级,用来选举master,取值范围1-255
    advert_int 1                     #发VRRP包时间间隔,即多久进行一次master选举
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {              #虚拟出来的地址
        192.168.15.100
    }
}
​
#虚拟服务器定义
virtual_server 192.168.15.100 3306 { #虚拟出来的地址加端口
    delay_loop 2                     #设置运行情况检查时间,单位为秒
    lb_algo rr                       #设置后端调度器算法,rr为轮询算法
    lb_kind DR                       #设置LVS实现负载均衡的机制,有DR、NAT、TUN三种模式可选
    persistence_timeout 50           #会话保持时间,单位为秒
    protocol TCP                     #指定转发协议,有 TCP和UDP可选
​
        real_server 192.168.15.94 3306 {          #实际本地ip+3306端口
       weight=5                      #表示服务器的权重值。权重值越高,服务器在负载均衡中被选中的概率就越大
        #当该ip 端口连接异常时,执行该脚本
        notify_down /etc/keepalived/shutdown.sh   #检查mysql服务down掉后执行的脚本
        TCP_CHECK {
            #实际物理机ip地址
            connect_ip 192.168.15.94
            #实际物理机port端口
            connect_port 3306
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
​
        }
    }
}
​

8.7 Modify the master-2 server keepalived configuration file

Rename the original keepalived.conf to keepalived_bak.conf, and then create a new keepalived.conf configuration file

cd /etc/keepalived/
[root@skymachine keepalived]# ll
总用量 4
-rw-r--r-- 1 root root 3550 6月  27 09:34 keepalived.conf
[root@skymachine keepalived]# mv keepalived.conf  keepalived_bak.conf
[root@skymachine keepalived]# ll
总用量 4
-rw-r--r-- 1 root root 3550 6月  27 09:34 keepalived_bak.conf
[root@skymachine keepalived]#  vi /etc/keepalived/keepalived.conf

Copy the following content into it. The differences from the configuration of master-1 are the four configurations of router_id, priority, real_server, and connect_ip.

! Configuration File for keepalived
​
#主要配置故障发生时的通知对象及机器标识
global_defs {
   notification_email {
     [email protected]
     [email protected]
     [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MYSQL-2                   #主机标识符,唯一即可
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
​
#用来定义对外提供服务的VIP区域及相关属性
vrrp_instance VI_1 {
    state BACKUP                     #表示keepalived角色,都是设成BACKUP则以优先级为主要参考
    interface ens192                 #指定HA监听的网络接口,刚才ifconfig查看的接口名称
    virtual_router_id 151            #虚拟路由标识,取值0-255,master-1和master-2保持一致
    priority 40                      #优先级,用来选举master,取值范围1-255
    advert_int 1                     #发VRRP包时间间隔,即多久进行一次master选举
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {              #虚拟出来的地址
        192.168.15.100
    }
}
​
#虚拟服务器定义
virtual_server 192.168.15.100 3306 { #虚拟出来的地址加端口
    delay_loop 2                     #设置运行情况检查时间,单位为秒
    lb_algo rr                       #设置后端调度器算法,rr为轮询算法
    lb_kind DR                       #设置LVS实现负载均衡的机制,有DR、NAT、TUN三种模式可选
    persistence_timeout 50           #会话保持时间,单位为秒
    protocol TCP                     #指定转发协议,有 TCP和UDP可选
​
        real_server 192.168.15.92 3306 {          #实际本地ip+3306端口
       weight=5                      #表示服务器的权重值。权重值越高,服务器在负载均衡中被选中的概率就越大
        #当该ip 端口连接异常时,执行该脚本
        notify_down /etc/keepalived/shutdown.sh   #检查mysql服务down掉后执行的脚本
        TCP_CHECK {
            #实际物理机ip地址
            connect_ip 192.168.15.92
            #实际物理机port端口
            connect_port 3306
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
​
        }
    }
}
​

8.8 Start the keepalived service

Start the master-1 and master-2 server keepalived services

[root@skymachine keepalived]# systemctl start keepalived
[root@skymachine keepalived]# systemctl status keepalived
systemctl enable keepalived #开机启动,根据需求设置

After startup, the keepalived status is: active (running), which is normal.

The following are the running results for reference

[root@skymachine keepalived]# systemctl start keepalived
[root@skymachine keepalived]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2023-06-27 09:53:23 CST; 4s ago
     Docs: man:keepalived(8)
           man:keepalived.conf(5)
           man:genhash(1)
           https://keepalived.org
  Process: 20817 ExecStart=/opt/keepalived-2.2.8/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 20818 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─20818 /opt/keepalived-2.2.8/sbin/keepalived -D
           ├─20819 /opt/keepalived-2.2.8/sbin/keepalived -D
           └─20820 /opt/keepalived-2.2.8/sbin/keepalived -D
​
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: (VI_1) received lower priority (40) advert from 192.168.15.92 - discarding
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: (VI_1) Receive advertisement timeout
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: (VI_1) Entering MASTER STATE
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: (VI_1) setting VIPs.
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: (VI_1) Sending/queueing gratuitous ARPs on ens192 for 192.168.15.100
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 09:53:26 skymachine Keepalived_vrrp[20820]: Sending gratuitous ARP on ens192 for 192.168.15.100

8.9 Configure virtual IP login user

Create new users on both servers to verify whether the keepalived service is configured successfully

useradd -m keepalived
​
passwd keepalived
​
Qwe135.

The following are the running results for reference

[root@skymachine ~]# useradd -m keepalived
[root@skymachine ~]# passwd keepalived
更改用户 keepalived 的密码 。
新的 密码:
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。

8.10 Test the keepalived service

After startup, it is equivalent to virtualizing a vip 192.168.15.100. Use the ssh tool to connect to the server. Enter the ip, username and password. Log in to the virtual ip and connect to it. Use ifconfig to check that the actual physical server used by the virtual vip is master-1. (192.168.15.94) server.

[root@skymachine keepalived]# systemctl stop keepalived

Stop the keepalived application of the master-1 (192.168.15.94) server. The 192.168.15.100 server will be disconnected. Reconnect. Check the 192.168.15.100 service ifconfig again. You can see that the 192.168.15.100 server automatically drifts the physical machine IP to the master. -2(192.168.15.92) on the server

Run systemctl stop keepalived on the 94 server
[root@skymachine keepalived]# systemctl stop keepalived

9. Mysql dual master dual active + keepalived high availability overall test

9.1 Start the service (it does not need to be started again)

First, start all the mysql and keepalived applications on the two servers master-1 and master-2, then create a new user and configure the permissions to access the external network.

mysql> CREATE DATABASE IF NOT EXISTS mydb DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
Query OK, 1 row affected (0.10 sec)
​
mysql> create user 'user01'@'%' identified by 'Mysql12#$';
Query OK, 0 rows affected (0.19 sec)
​
mysql> grant all privileges on `mydb`.* to 'user01'@'%' ;
Query OK, 0 rows affected (0.02 sec)
​
mysql> flush privileges; 
Query OK, 0 rows affected (0.02 sec)
​
mysql> select user,host from mysql.user;
+------------------+--------------+
| user             | host         |
+------------------+--------------+
| user01           | %            |
| test             | 192.168.15.% |
| mysql.infoschema | localhost    |
| mysql.session    | localhost    |
| mysql.sys        | localhost    |
| root             | localhost    |
+------------------+--------------+
6 rows in set (0.00 sec)

9.2 Connect to the keepalived virtual server

Use the mysql connection tool to connect to the keepalived virtual server 192.168.15.100

9.3 Create test data 

Create a new table in the 192.168.15.100 database mydb test library and insert some data into the table

drop table ceshi1;
​
CREATE TABLE ceshi1(
    ID int,
    NAME VARCHAR(255),
    subject VARCHAR(18),
    score int);
insert into ceshi1  values(1,'张三','数学',90);
insert into ceshi1  values(2,'张三','语文',70);
​
select * from ceshi1;

9.4 Check the synchronization status of master-1 and master-2

At this time, you can view the master-1 and master-2 databases, and the data has been synchronized.

9.5 Check the actual physical machine IP of 100 servers

Use the ifconfig command to check that the actual physical machine used is 192.168.15.94, so the master-1 (192.168.15.94) server mysql is the main database.

9.6 Stop the physical machine mysql service

At this time, manually stop the master-1 server mysql. Keepalived detects that the 192.168.15.94 service 3306 port connection fails. The /etc/keepalived/shutdown.sh script will be executed to end the 192.168.15.94 server keepalived application.

[root@skymachine ~]# service mysql stop
Shutting down MySQL............. SUCCESS! 

9.7 Check the execution status of drifting IP

At this time, connect to the 192.168.15.100 service and check ifconfig. It is found that the physical machine has been actually moved from master-1 (192.168.15.94) to the master-2 (192.168.15.92) server.

9.8 Insert data on the new primary server

Then use the mysql connection tool to connect to the mysql of 192.168.15.100, insert a piece of data, and test whether the data is stored in the master-2 (192.168.15.92) server mysql.

insert into ceshi1 values(6,'李四','英语',94);

9.9 View new master server data

Check the master-2 server mysql data. The data has been synchronized, indicating that keepalived has successfully established high availability. When the master-1 server mysql has a problem, keepalived automatically drifts the IP to the physical machine master-2 server, so that the master-2 server mysql becomes the master. database.

9.10 Restart the master-1 service and check the data synchronization status

At this time, start the master-1 (192.168.15.94) server mysql and keepalived applications.

service mysql start
systemctl start keepalived
systemctl status keepalived

 The following are the running results for reference

[root@skymachine ~]# service mysql start
Starting MySQL... SUCCESS! 
[root@skymachine ~]# systemctl start keepalived
[root@skymachine ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2023-06-27 20:24:07 CST; 7s ago
     Docs: man:keepalived(8)
           man:keepalived.conf(5)
           man:genhash(1)
           https://keepalived.org
  Process: 23869 ExecStart=/opt/keepalived-2.2.8/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 23870 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─23870 /opt/keepalived-2.2.8/sbin/keepalived -D
           ├─23871 /opt/keepalived-2.2.8/sbin/keepalived -D
           └─23872 /opt/keepalived-2.2.8/sbin/keepalived -D
​
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: (VI_1) received lower priority (40) advert from 192.168.15.92 - discarding
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: (VI_1) Receive advertisement timeout
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: (VI_1) Entering MASTER STATE
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: (VI_1) setting VIPs.
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: (VI_1) Sending/queueing gratuitous ARPs on ens192 for 192.168.15.100
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: Sending gratuitous ARP on ens192 for 192.168.15.100
6月 27 20:24:10 skymachine Keepalived_vrrp[23872]: Sending gratuitous ARP on ens192 for 192.168.15.100

Check the master-1 database ceshi1 table data and the data has been synchronized successfully. 

At this point, the high-availability deployment and testing of mysql dual-master dual-active + keepalived is completed.

10. Summary

1. When using keepalived as a high-availability solution, it is best to set both nodes to BACKUP mode to avoid conflicts caused by accidental preemption of the same data in the two nodes;

2. Set the auto_increment_increment (self-increment step size) and auto_increment_offset (word increment starting value) of the two nodes to different values. The purpose is to avoid that when the master node unexpectedly goes down, some binlogs may not be copied to the master node in time. It is applied on the slave, which will cause the self-increment value of the newly written data on the slave to conflict with the original master, so it is staggered from the beginning;

3. The slave node server configuration should not be too poor, otherwise it is more likely to cause replication delays. As a hot standby node slave server, the hardware configuration cannot be lower than that of the master node;

If you are very sensitive to latency, you can consider using the MariaDB branch version. Multi-thread replication can greatly reduce replication latency.

Personal organization, non-professionals

Reference article

1. Mysql+Keepalived realizes high availability of mysql database_keepalived database_KaiA8's blog-CSDN blog

2. Mysql cluster’s mysql dual-master dual-active + keepalived to achieve high availability_mysql+keepalived setup tutorial_Tiandaohouqin-L’s blog-CSDN blog

Guess you like

Origin blog.csdn.net/m0_61388193/article/details/131430091