MySQL 双主复制+Keepalived高可用

本文档通过看书总结,并个人测试后总结而得,有任何冒犯,请提出改正。

一、主从搭建

##前期准备

系统:CentOS 6.8
数据库:MySQL 8.0.23
关闭iptables和selinux、配置/etc/hosts文件
节点信息:
172.16.117.13 MySQL-1
172.16.117.14 MySQL-2
172.16.117.15 vip

1.1、准备参数文件

##MySQL-1配置/etc/my.cnf

[root@MySQL-1 ~]# cat /etc/my.cnf
######主从参数配置#########

[mysqld]
server-id=200 ##每个节点的server-id设置不同
max_allowed_packet = 16M
basedir=/usr
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
innodb_flush_log_at_trx_commit =1 ##设置为1立即写入日志文件并刷新,安全。设置为2性能好
innodb_buffer_pool_size=512M
log-bin = mysql-bin
log_slave_updates= ON
sync_binlog=1
binlog_format = row
gtid_mode = on
enforce_gtid_consistency = ON
relay-log-info-repository=TABLE
master-info-repository=TABLE
relay_log_recovery=1

##MySQL-2配置/etc/my.cnf

[root@MySQL-2 yum.repos.d]# cat /etc/my.cnf
######主从参数配置#########

[mysqld]
server-id=201 ##每个节点的server-id设置不同
max_allowed_packet = 16M
basedir=/usr
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
innodb_flush_log_at_trx_commit =1 ##设置为1立即写入日志文件并刷新,安全。设置为2性能好
innodb_buffer_pool_size=512M
log-bin = mysql-bin
log_slave_updates= ON
sync_binlog=1
binlog_format = row
gtid_mode = on
enforce_gtid_consistency = ON
relay-log-info-repository=TABLE
master-info-repository=TABLE
relay_log_recovery=1

1.2、启动数据库

##MySQL-1启动数据库

[root@MySQL-1 ~]# /etc/init.d/mysqld  restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
[root@MySQL-1 ~]# mysql -uroot -proot@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

##MySQL-2启动数据库

[root@MySQL-2 ~]# mysql -uroot -proot@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.23 MySQL Community Server - GPL

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

注意⚠️:实验为测试环境,并没有进行参数文件优化,见谅。

1.3、创建复制账号

##MySQL-1创建复制账号

mysql> create user rep@'172.16.117.14' identified with mysql_native_password by 'rep@123';
Query OK, 0 rows affected (0.02 sec)

mysql> grant replication slave on *.* to rep@'172.16.117.14';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

##MySQL-2创建复制账号

mysql> create user rep@'172.16.117.13' identified with mysql_native_password by 'rep@123'; 
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to rep@'172.16.117.13';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

1.4、配置同步

#MySQL-2上配置同步

mysql> change master to master_host='172.16.117.13',master_user='rep',master_password='rep@123',master_port=3306,master_auto_position=1;
Query OK, 0 rows affected, 8 warnings (0.02 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 master to send event
                  Master_Host: 172.16.117.13
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 4229
               Relay_Log_File: MySQL-2-relay-bin.000002
                Relay_Log_Pos: 1110
        Relay_Master_Log_File: mysql-bin.000001
             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: 4229
              Relay_Log_Space: 1321
              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: 200
                  Master_UUID: 5b3c474a-77b4-11eb-b0de-001c4210266b
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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: 5b3c474a-77b4-11eb-b0de-001c4210266b:11-13
            Executed_Gtid_Set: 58914456-77b4-11eb-a10f-001c424c1a1f:1-8,
5b3c474a-77b4-11eb-b0de-001c4210266b:1-13
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.01 sec)

ERROR: 
No query specified

##MySQL-1上配置同步

mysql> change master to master_host='172.16.117.14' ,master_user='rep',master_password='rep@123',master_port=3306,master_auto_position=1;
Query OK, 0 rows affected, 8 warnings (0.02 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.117.14
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 4977
               Relay_Log_File: MySQL-1-relay-bin.000002
                Relay_Log_Pos: 1157
        Relay_Master_Log_File: mysql-bin.000001
             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: 4977
              Relay_Log_Space: 1368
              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: 201
                  Master_UUID: 58914456-77b4-11eb-a10f-001c424c1a1f
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave 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: 58914456-77b4-11eb-a10f-001c424c1a1f:6-8
            Executed_Gtid_Set: 58914456-77b4-11eb-a10f-001c424c1a1f:1-8,
5b3c474a-77b4-11eb-b0de-001c4210266b:1-13
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set, 1 warning (0.01 sec)

ERROR: 
No query specified

1.5、测试同步

##MySQL-1上创建测试库和测试表,MySQL-2上查看
##MySQL-2上创建测试库和测试表,MySQL-1上查看
MySQL-1创建

mysql> show variables like '%hostname%';
+---------------+---------+
| Variable_name | Value   |
+---------------+---------+
| hostname      | MySQL-1 |
+---------------+---------+
1 row in set (0.02 sec)

mysql> create database ccola;
Query OK, 1 row affected (0.01 sec)

mysql> use ccola;
Database changed
mysql> create table ccola (id int primary key,name varchar(20));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into ccola values (1,'ccola');
Query OK, 1 row affected (0.02 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

MySQL-2查看并创建

mysql> show variables like '%hostname%';
+---------------+---------+
| Variable_name | Value   |
+---------------+---------+
| hostname      | MySQL-2 |
+---------------+---------+
1 row in set (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| ccola              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use ccola;
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> insert into ccola values (2,'feifei');
Query OK, 1 row affected (0.01 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

MySQL-1查看

mysql> select database();
+------------+
| database() |
+------------+
| ccola      |
+------------+
1 row in set (0.00 sec)

mysql> select * from ccola;
+----+--------+
| id | name   |
+----+--------+
|  1 | ccola  |
|  2 | feifei |
+----+--------+
2 rows in set (0.00 sec)

至此,双主同步搭建成功

二、配置Keepalived实现高可用

2.1、安装Keepalived软件包

[root@MySQL-1 ~]# yum install -y keepalived
[root@MySQL-2 ~]# yum install -y keepalived

2.2、准备mysql.sh脚本

[root@MySQL-1 ~]# vi /etc/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
[root@MySQL-1 ~]# chmod +x !$
[root@MySQL-2 ~]# vi /etc/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
[root@MySQL-2 ~]#  chmod +x !$

2.3、准备Keepalived配置文件

##MySQL-1配置Keepalived

[root@MySQL-1 ~]# vi /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

vrrp_instance VI_1 {
    state BACKUP            ##两台都设置成BACKUP
    interface eth0
    virtual_router_id 51    ##主备相同
    priority 100            ##优先级,172.16.117.13优先级高,172.16.117.14这台设置成90
    advert_int 5
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.117.15/24     ##VIP
    }
}
virtual_server 172.16.117.15 3306 {
    delay_loop 2
    persistence_timeout 50 ##同一个IP的连接50秒内被分配到同一台真实服务器
    protocol TCP
    real_server 172.16.117.13 3306 { ##检测本地MySQL,备机也要写检测本地MySQL
        weight 3
        notify_down /etc/keepalived/mysql.sh ##当MySQL服务down时,执行此脚本,杀死keepalived实现切换
        TCP_CHECK {
            connect_timeout 3    ##连接超时
            nb_get_retry 3      ##重试次数
            delay_before_retry 3 ##重试间隔时间
        }
    }

##MySQL-2配置Keepalived

[root@MySQL-2 ~]# vi /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

vrrp_instance VI_1 {
    state BACKUP            ##两台都设置成BACKUP
    interface eth0
    virtual_router_id 51    ##主备相同
    priority 90            ##优先级,172.16.117.13优先级高,172.16.117.14这台设置成90
    advert_int 5
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.117.15/24     ##VIP
    }
}
virtual_server 172.16.117.15 3306 {
    delay_loop 2
    persistence_timeout 50 ##同一个IP的连接50秒内被分配到同一台真实服务器
    protocol TCP
    real_server 172.16.117.14 3306 { ##检测本地MySQL,备机也要写检测本地MySQL
        weight 3
        notify_down /etc/keepalived/mysql.sh ##当MySQL服务down时,执行此脚本,杀死keepalived实现切换
        TCP_CHECK {
            connect_timeout 3    ##连接超时
            nb_get_retry 3      ##重试次数
            delay_before_retry 3 ##重试间隔时间
        }
    }

2.4、启动两个节点的Keepalived进程

##MySQL-1启动Keepalived

[root@MySQL-1 ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[root@MySQL-1 ~]# ps -ef | grep keepalived | grep -v grep
root     9041     1  0 07:30 ?        00:00:00 /usr/sbin/keepalived -D
root     9042  9041  0 07:30 ?        00:00:00 /usr/sbin/keepalived -D
root     9043  9041  0 07:30 ?        00:00:00 /usr/sbin/keepalived -D

##MySQL-2启动Keepalived进程

[root@MySQL-2 ~]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[root@MySQL-2 ~]# ps -ef | grep keepalived | grep -v grep 
root     8829     1  0 07:30 ?        00:00:00 /usr/sbin/keepalived -D
root     8830  8829  0 07:30 ?        00:00:00 /usr/sbin/keepalived -D
root     8831  8829  0 07:30 ?        00:00:00 /usr/sbin/keepalived -D

2.5、查看Keepalived状态以及VIP绑定情况

##MySQL-1查看启动日志
MySQL-1的优先级高,keepalived的状态为MASTER,并且发布的广播协议。172.16.117.15(VIP)已经在本台机器上,其他机器不能再使用了

[root@MySQL-1 ~]# tail -50 /var/log/messages
..
..
Feb 26 07:31:02 MySQL-1 Keepalived_vrrp[9043]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 26 07:31:07 MySQL-1 Keepalived_vrrp[9043]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 26 07:31:07 MySQL-1 Keepalived_vrrp[9043]: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 26 07:31:07 MySQL-1 Keepalived_vrrp[9043]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.117.15
Feb 26 07:31:07 MySQL-1 Keepalived_healthcheckers[9042]: Netlink reflector reports IP 172.16.117.15 added
Feb 26 07:31:12 MySQL-1 Keepalived_vrrp[9043]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.117.15

##MySQL-1上查看IP,查看VIP绑定情况
##VIP在MySQL-1上

[root@MySQL-1 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1c:42:10:26:6b brd ff:ff:ff:ff:ff:ff
    inet 172.16.117.13/24 brd 172.16.117.255 scope global eth0
    inet 172.16.117.15/32 scope global eth0
    inet6 fe80::21c:42ff:fe10:266b/64 scope link 
       valid_lft forever preferred_lft forever

##MySQL-2查看启动日志
MySQL-2Keepalived的状态是backup

[root@MySQL-2 ~]# tail -50 /var/log/messages
Feb 26 07:30:51 MySQL-2 Keepalived_vrrp[8831]: Configuration is using : 60758 Bytes
Feb 26 07:30:51 MySQL-2 Keepalived_vrrp[8831]: Using LinkWatch kernel netlink reflector...
Feb 26 07:30:51 MySQL-2 Keepalived_vrrp[8831]: VRRP_Instance(VI_1) Entering BACKUP STATE
Feb 26 07:30:51 MySQL-2 Keepalived_vrrp[8831]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Netlink reflector reports IP 172.16.117.14 added
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Netlink reflector reports IP fe80::21c:42ff:fe4c:1a1f added
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Registering Kernel netlink reflector
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Registering Kernel netlink command channel
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Opening file '/etc/keepalived/keepalived.conf'.
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Configuration is using : 9502 Bytes
Feb 26 07:30:51 MySQL-2 kernel: IPVS: Registered protocols (TCP, UDP, SCTP, AH, ESP)
Feb 26 07:30:51 MySQL-2 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes)
Feb 26 07:30:51 MySQL-2 kernel: IPVS: ipvs loaded.
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: IPVS: Scheduler or persistence engine not found
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: IPVS: Service not defined
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Using LinkWatch kernel netlink reflector...
Feb 26 07:30:51 MySQL-2 Keepalived_healthcheckers[8830]: Activating healthchecker for service [172.16.117.14]:3306
Feb 26 07:30:51 MySQL-2 kernel: IPVS: Scheduler module ip_vs_ not found

##MySQL-2查看IP,查看VIP绑定情况
MySQL-2没有VIP

[root@MySQL-2 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1c:42:4c:1a:1f brd ff:ff:ff:ff:ff:ff
    inet 172.16.117.14/24 brd 172.16.117.255 scope global eth0
    inet6 fe80::21c:42ff:fe4c:1a1f/64 scope link 
       valid_lft forever preferred_lft forever

2.6、测试使用VIP连接MySQL-1

##在MySQL-1上创建远程连接账号并授予相应权限

mysql> show variables like 'hostname';
+---------------+---------+
| Variable_name | Value   |
+---------------+---------+
| hostname      | MySQL-1 |
+---------------+---------+
1 row in set (0.01 sec)
mysql> create user root@'%' identified by 'root@123';
Query OK, 0 rows affected (0.02 sec)

mysql> grant all on *.* to root@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

##在MySQL-2上使用vip连接MySQL-1,正常连接

[root@MySQL-2 ~]# mysql -uroot -p'root@123' -h 172.16.117.15
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 934
Server version: 8.0.23 MySQL Community Server - GPL

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \s;
--------------
mysql  Ver 8.0.23 for Linux on x86_64 (MySQL Community Server - GPL)

Connection id:          934
Current database:
Current user:           root@MySQL-2
SSL:                    Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
Current pager:          stdout
Using outfile:          ''
Using delimiter:        ;
Server version:         8.0.23 MySQL Community Server - GPL
Protocol version:       10
Connection:             172.16.117.15 via TCP/IP
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
TCP port:               3306
Binary data as:         Hexadecimal
Uptime:                 2 hours 5 min 2 sec

**Threads: 6  Questions: 133  Slow queries: 0  Opens: 256  Flush tables: 3  Open tables: 175  Queries per second avg: 0.017
--------------**

ERROR: 
No query specified

mysql> 

2.7、模拟MySQL-1宕机时的故障切换

##MySQL-1关闭MySQL服务

[root@MySQL-1 ~]# /etc/init.d/mysqld stop 
Stopping mysqld:                                           [  OK  ]

##MySQL-1查看/var/log/messages日志输出
这时候会发现,Keepalived服务呗stop,并且移除了VIP

[root@MySQL-1 ~]# tail -50 /var/log/messages
Feb 26 08:05:32 MySQL-1 Keepalived_healthcheckers[9104]: Lost quorum 1-0=1 > 0 for VS [172.16.117.15]:3306
Feb 26 08:05:32 MySQL-1 Keepalived[9103]: Stopping Keepalived v1.2.13 (03/19,2015)
Feb 26 08:05:32 MySQL-1 Keepalived_vrrp[9105]: VRRP_Instance(VI_1) sending 0 priority
Feb 26 08:05:32 MySQL-1 Keepalived_vrrp[9105]: VRRP_Instance(VI_1) removing protocol VIPs.
Feb 26 08:05:32 MySQL-1 Keepalived_healthcheckers[9104]: Netlink reflector reports IP 172.16.117.15 removed
Feb 26 08:05:32 MySQL-1 Keepalived_healthcheckers[9104]: IPVS: No such service

##MySQL-2上查看/var/log/messages日志输出
此时发现MySQL-2伤的Keepalived状态变成了MASTER

[root@MySQL-2 ~]# tail -30 /var/log/messages
Feb 26 08:05:33 MySQL-2 Keepalived_vrrp[8919]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 26 08:05:38 MySQL-2 Keepalived_vrrp[8919]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 26 08:05:38 MySQL-2 Keepalived_vrrp[8919]: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 26 08:05:38 MySQL-2 Keepalived_vrrp[8919]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.117.15
Feb 26 08:05:38 MySQL-2 Keepalived_healthcheckers[8918]: Netlink reflector reports IP 172.16.117.15 added
Feb 26 08:05:43 MySQL-2 Keepalived_vrrp[8919]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 172.16.117.15

##MySQL-2查看IP,查看VIP绑定情况
此处发现VIP已经转移到MySQL-2上面

[root@MySQL-2 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:1c:42:4c:1a:1f brd ff:ff:ff:ff:ff:ff
    inet 172.16.117.14/24 brd 172.16.117.255 scope global eth0
    inet 172.16.117.15/24 scope global secondary eth0
    inet6 fe80::21c:42ff:fe4c:1a1f/64 scope link 
       valid_lft forever preferred_lft forever

##此过程不影响客户端连接状态,客户端可以重连,继续使用VIP连接。
Master to Master + Keepalived架构比较简单,主节点出现故障后,利用Keepalived的高可用机制可以快速的切换到另一个节点,原来的Backup Master变成了主节点。Keepalived架构在设置两个节点状态的时候都要设置成Backup而且是noprermpt(非抢占)模式,通过优先级的高低来决定谁是主库,避免发生冲突现象。另外,在服务器异常判断时,可以修改判断脚本,通过对第三方节点补充检测来决定是否进行切换,这样可以降低“脑裂”的风险。Backup Master的配置要尽量和Master保持一致,不可以降低太多性能标准

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值