CentOS7.4上mysql5.7双主keepalived实现高可用

目录

1、环境规划... 1

2、准备rpm包... 1

3、安装mysql 2

4、检查安装... 2

5、配置文件... 2

6、启动mysql 5

7、mysql root用户密码... 5

8、mysql日志... 5

9、详细参见... 5

10、创建mysql复制账户... 6

11、搭建双主... 6

12、下载keepalived源码包... 7

13、安装keepalived依赖包... 7

14、解包和安装... 7

15、设置启动和服务... 7

16、配置文件... 8

17、keepalive检测脚本... 10

18、脚本赋权... 11

19、修改keepalive的PIDfile. 11

20、启动keepalived. 11

21、创建mysql远程登陆用户... 12

22、停selinux. 12

23、开防火墙规则... 13

24、测试vip高可用... 13

 

1、环境规划

master1 : 192.168.30.177 CentOS Linux release 7.4.1708 (Core)

master2 : 192.168.30.178 CentOS Linux release 7.4.1708 (Core)

vip : 192.168.30.179

2、准备rpm包

使用下面这些包,使用iso镜像文件配置本地yum源。

[root@mysql5 soft]# ll mysql*

-rw-r--r--. 1 root root  25402568 Aug 15 16:54 mysql-community-client-5.7.25-1.el7.x86_64.rpm

-rw-r--r--. 1 root root    280904 Aug 15 16:52 mysql-community-common-5.7.25-1.el7.x86_64.rpm

-rw-r--r--. 1 root root   2271668 Aug 15 16:58 mysql-community-libs-5.7.25-1.el7.x86_64.rpm

-rw-r--r--. 1 root root   2116628 Aug 15 19:54 mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm

-rw-r--r--. 1 root root 173130520 Aug  6 15:13 mysql-community-server-5.7.25-1.el7.x86_64.rpm

 

3、安装mysql

[root@mysql5 soft]# yum -y install mysql*.rpm

 

4、检查安装

[root@mysql5 soft]# rpm -aq |grep mysql

mysql-community-libs-5.7.25-1.el7.x86_64

mysql-community-common-5.7.25-1.el7.x86_64

mysql-community-client-5.7.25-1.el7.x86_64

mysql-community-libs-compat-5.7.25-1.el7.x86_64

mysql-community-server-5.7.25-1.el7.x86_64

 

5、配置文件

安装完成后会默认在/etc/下面生成一个/etc/my.cnf文件,如果原来就有这个配置文件的话,会直接被覆盖掉。

master1:192.168.30.177

mysql配置文件:

[root@mysql5 soft]# cat /etc/my.cnf

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

 

[mysqld]

#

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M

#

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin

#

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

 

character_set_server=utf8

lower_case_table_names=1

default_password_lifetime=0

log-bin=mysql-bin

relay-log=mysql-relay-bin

server-id=1

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=test.%

replicate-wild-ignore-table=information_schema.%

 

#×ÔidÆʼֵ

auto_increment_offset=1

 

#ÿ´ÎÔöÖ

auto_increment_increment=2

max_connections=1000

wait_timeout=300

sync_binlog = 100

 

#validate_password = off

 

 

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

 

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

 

master2:192.168.30.178

[root@mysql5 ~]# cat /etc/my.cnf

# For advice on how to change settings please see

# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

 

[mysqld]

#

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M

#

# Remove leading # to turn on a very important data integrity option: logging

# changes to the binary log between backups.

# log_bin

#

# Remove leading # to set options mainly useful for reporting servers.

# The server defaults are faster for transactions and fast SELECTs.

# Adjust sizes as needed, experiment to find the optimal values.

# join_buffer_size = 128M

# sort_buffer_size = 2M

# read_rnd_buffer_size = 2M

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

 

character_set_server=utf8

lower_case_table_names=1

default_password_lifetime=0

log-bin=mysql-bin

relay-log=mysql-relay-bin

server-id=2

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=test.%

replicate-wild-ignore-table=information_schema.%

 

#自增id起始值

auto_increment_offset=2

 

#每次自增数字

auto_increment_increment=2

max_connections=1000

wait_timeout=300

sync_binlog = 100

 

validate_password = off

 

 

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

 

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

6、启动mysql

[root@gfs01 soft]# systemctl start mysqld

[root@gfs01 soft]# systemctl status mysqld

● mysqld.service - MySQL Server

   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

   Active: active (running) since Thu 2019-08-15 17:03:45 CST; 4s ago

     Docs: man:mysqld(8)

           http://dev.mysql.com/doc/refman/en/using-systemd.html

  Process: 2002 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  Process: 1921 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

 Main PID: 2005 (mysqld)

   CGroup: /system.slice/mysqld.service

           └─2005 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

 

Aug 15 17:03:30 gfs01 systemd[1]: Starting MySQL Server...

Aug 15 17:03:45 gfs01 systemd[1]: Started MySQL Server.

 

7、mysql root用户密码

mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。通过下面的方式找到root默认密码,然后登录mysql进行修改:

[root@gfs01 soft]# grep 'temporary password' /var/log/mysqld.log

2019-08-15T09:03:41.058055Z 1 [Note] A temporary password is generated for root@localhost: /Hga(:;0pY8H

 

8、mysql日志

如果启动有错误,根据mysql日志排查

/var/log/mysqld.log

 

9、详细参见

https://blog.csdn.net/kadwf123/article/details/99644608

 

10、创建mysql复制账户

两台主机都创建复制用户:

grant replication slave,replication client on *.* to repl@'%' identified by 'repl4Slave' ;

 

11、搭建双主

master1:192.168.30.177上:

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000004 |      154 |              |                  |                   |

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

1 row in set (0.00 sec)

 

 

stop slave ;

 

change master to

master_host='192.168.30.178',

master_port=3306,

master_user='repl',

master_password='repl4Slave',

master_log_file='mysql-bin.000002',

master_log_pos=462;

 

start slave ;

 

master2上:192.168.30.178上:

 

mysql> show master status

    -> ;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000002 |      462 |              |                  |                   |

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

1 row in set (0.00 sec)

 

stop slave ;

 

change master to

master_host='192.168.30.177',

master_port=3306,

master_user='repl',

master_password='repl4Slave',

master_log_file='mysql-bin.000004',

master_log_pos=154;

 

start slave ;

 

12、下载keepalived源码包

[root@mysql5 soft]# ll keepalived-1.3.5.tar.gz

-rw-r--r--. 1 root root 683183 Mar 20  2017 keepalived-1.3.5.tar.gz

可以通过下面的方式获取:

wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz

 

13、安装keepalived依赖包

本地yum源就有

yum install -y openssl-devel

 

14、解包和安装

[root@mysql5 soft]# tar -xzvf keepalived-1.3.5.tar.gz

[root@mysql5 keepalived-1.3.5]# pwd

/soft/keepalived-1.3.5

[root@mysql5 soft]# cd keepalived-1.3.5

[root@mysql5 keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived

[root@mysql5 keepalived-1.3.5]# make

[root@mysql5 keepalived-1.3.5]# make install

 

15、设置启动和服务

cp /soft/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/

 

cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived/

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

echo "/etc/init.d/keepalived start" >> /etc/rc.local

 

16、配置文件

master1:192.168.30.177

[root@mysql5 keepalived-1.3.5]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

      

global_defs {

        notification_email {

                [email protected]

                [email protected]

        }

              

        notification_email_from [email protected]

                smtp_server 127.0.0.1

                smtp_connect_timeout 30

                router_id MASTER-HA.177

        }

              

        vrrp_script chk_mysql_port {

            #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等

            script "/opt/chk_mysql.sh"  

            #脚本执行间隔,每2s检测一次

            interval 2   

           #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5

            weight -5    

            #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)

            fall 2      

           #检测1次成功就算成功。但不修改优先级

            rise 1                   

        }

              

        vrrp_instance VI_1 {

            state MASTER   

            #指定虚拟ip的网卡接口

            interface ens33    

            mcast_src_ip 192.168.30.177

            #路由器标识,MASTER和BACKUP必须是一致的

            virtual_router_id 51   

            #定义优先级,数字越大,优先级越高。

#在同一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级。

#这样MASTER故障恢复后,就可以将VIP资源再次抢回来

            priority 101           

            advert_int 1        

            authentication {  

                auth_type PASS

                auth_pass 1111    

            }

            virtual_ipaddress {   

                #配置的vip

                192.168.30.179

            }

             

        track_script {              

           chk_mysql_port            

        }

}

 

[root@mysql5 keepalived-1.3.5]#

master2:192.168.30.178

[root@mysql5 ~]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

      

global_defs {

        notification_email {

                [email protected]

                [email protected]

        }

              

        notification_email_from [email protected]

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id MASTER-HA.178

        }

              

        vrrp_script chk_mysql_port {

            script "/opt/chk_mysql.sh"

            interval 2           

            weight -5                

            fall 2                

            rise 1              

        }

              

        vrrp_instance VI_1 {

            state BACKUP

            interface ens33   

            mcast_src_ip 192.168.30.178

            virtual_router_id 51   

            priority 99         

            advert_int 1        

            authentication {  

                auth_type PASS

                auth_pass 1111    

            }

            virtual_ipaddress {   

                192.168.30.179

            }

             

        track_script {              

           chk_mysql_port            

        }

}

 

[root@mysql5 ~]#

 

17、keepalive检测脚本

[root@mysql5 ~]# cat /opt/chk_mysql.sh

#!/bin/bash

MYSQL=/usr/bin/mysql

MYSQL_HOST=localhost

MYSQL_USER=root

MYSQL_PASSWORD=root123

CHECK_TIME=3

 

#mysql  is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0

 

MYSQL_OK=1

 

function check_mysql_helth (){

    $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1

    if [ $? = 0 ] ;then

    MYSQL_OK=1

    else

    MYSQL_OK=0

    fi

    return $MYSQL_OK

}

while [ $CHECK_TIME -ne 0 ]

do

  let "CHECK_TIME -= 1"

  check_mysql_helth

        if [ $MYSQL_OK = 1 ] ; then

            CHECK_TIME=0

            exit 0

        fi

        if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ]

        then

            pkill keepalived

            exit 1

        fi

        sleep 1

done

 

[root@mysql5 ~]#

 

18、脚本赋权

chmod 755 /opt/chk_mysql.sh

 

19、修改keepalive的PIDfile

vi /lib/systemd/system/keepalived.service

#PIDFile=/usr/local/keepalived/var/run/keepalived.pid

PIDFile=/var/run/keepalived.pid

原PIDFile注释掉,然后改为下面这个

 

20、启动keepalived

systemctl start keepalived

[root@mysql5 ~]# 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 Mon 2019-09-02 22:23:49 CST; 1h 33min ago

  Process: 50514 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)

 Main PID: 50515 (keepalived)

   CGroup: /system.slice/keepalived.service

           ├─50515 /usr/local/keepalived/sbin/keepalived -D

           ├─50516 /usr/local/keepalived/sbin/keepalived -D

           └─50517 /usr/local/keepalived/sbin/keepalived -D

 

Sep 02 23:09:29 mysql5.7-keepalive2 Keepalived_vrrp[50517]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 23:09:34 mysql5.7-keepalive2 Keepalived_vrrp[50517]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 23:09:34 mysql5.7-keepalive2 Keepalived_vrrp[50517]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 fo...0.179

Sep 02 23:09:34 mysql5.7-keepalive2 Keepalived_vrrp[50517]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 23:09:34 mysql5.7-keepalive2 Keepalived_vrrp[50517]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 23:09:34 mysql5.7-keepalive2 Keepalived_vrrp[50517]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 23:09:34 mysql5.7-keepalive2 Keepalived_vrrp[50517]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 23:10:38 mysql5.7-keepalive2 Keepalived_vrrp[50517]: VRRP_Instance(VI_1) Received advert with higher priority 101, ours 99

Sep 02 23:10:38 mysql5.7-keepalive2 Keepalived_vrrp[50517]: VRRP_Instance(VI_1) Entering BACKUP STATE

Sep 02 23:10:38 mysql5.7-keepalive2 Keepalived_vrrp[50517]: VRRP_Instance(VI_1) removing protocol VIPs.

Hint: Some lines were ellipsized, use -l to show in full.

[root@mysql5 ~]#

 

21、创建mysql远程登陆用户

grant all on *.* to root@'192.168.30.%' identified by "root123";

flush privileges;

 

22、停selinux

vi /etc/selinux/config

SELINUX=disabled

 

23、开防火墙规则

开启防火墙的状态下需要执行下面三条规则:

#开启mysql的3306端口

firewall-cmd --zone=public --add-port=3306/tcp –permanent

#允许keepalived组播地址通信

firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.30.0/24" destination address="224.0.0.18" accept'

#允许VRRP(虚拟路由器冗余协)通信

firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.30.0/24 protocol value=vrrp accept'

 

24、测试vip高可用

正常情况下vip在master1上:

[root@mysql5 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

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

    link/ether 00:0c:29:8b:41:06 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.177/24 brd 192.168.30.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.30.179/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::58:dc06:f8c4:84b3/64 scope link

       valid_lft forever preferred_lft forever

[root@mysql5 ~]#

 

在master2上

[root@mysql5 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

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

    link/ether 00:0c:29:27:e2:10 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.178/24 brd 192.168.30.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::8d0:5ce9:59e6:cbb8/64 scope link

       valid_lft forever preferred_lft forever

[root@mysql5 ~]#

 

在master1上停mysql,此时keepalive检测到3306端口没有了就会切换,然后master1的keepalive就会自杀。

[root@mysql5 ~]# systemctl stop mysqld

可以立马看下keepalived,已经停掉了:

[root@mysql5 ~]# systemctl status keepalived

● keepalived.service - LVS and VRRP High Availability Monitor

   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)

   Active: inactive (dead)

 

Sep 02 22:35:29 mysql5.7-keepalive1 Keepalived_vrrp[52352]: Sending gra...

Sep 02 22:35:29 mysql5.7-keepalive1 Keepalived_vrrp[52352]: Sending gra...

Sep 02 22:35:29 mysql5.7-keepalive1 Keepalived_vrrp[52352]: Sending gra...

Sep 02 22:35:29 mysql5.7-keepalive1 Keepalived_vrrp[52352]: Sending gra...

Sep 02 22:40:09 mysql5.7-keepalive1 Keepalived[52350]: Stopping

Sep 02 22:40:09 mysql5.7-keepalive1 systemd[1]: Stopping LVS and VRRP H...

Sep 02 22:40:09 mysql5.7-keepalive1 Keepalived_vrrp[52352]: VRRP_Instan...

Sep 02 22:40:09 mysql5.7-keepalive1 Keepalived_vrrp[52352]: VRRP_Instan...

Sep 02 22:40:10 mysql5.7-keepalive1 Keepalived[52350]: Stopped Keepaliv...

Sep 02 22:40:10 mysql5.7-keepalive1 systemd[1]: Stopped LVS and VRRP Hi...

Hint: Some lines were ellipsized, use -l to show in full.

 

在master1上再次查看,就没有vip为192.168.30.179的ip了。

[root@mysql5 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

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

    link/ether 00:0c:29:8b:41:06 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.177/24 brd 192.168.30.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::58:dc06:f8c4:84b3/64 scope link

       valid_lft forever preferred_lft forever

[root@mysql5 ~]#

 

 

而在master2上可以看到vip已经起来了192.168.30.179

[root@mysql5 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

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

    link/ether 00:0c:29:27:e2:10 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.178/24 brd 192.168.30.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.30.179/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::8d0:5ce9:59e6:cbb8/64 scope link

       valid_lft forever preferred_lft forever

 

此时通过vip连接上数据库,插入一条数据:   

[root@mysql5 ~]# mysql -uroot -proot123 -h192.168.30.179

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 20

Server version: 5.7.25-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

 

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> use wufan ;

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> select * from tmp_list_001 ;

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

| id   | name  |

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

|    1 | wufan |

|    2 | haha  |

|    3 | xiao  |

|    4 | liu   |

|    5 | yang  |

|    6 | fei   |

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

6 rows in set (0.00 sec)

 

mysql> insert into tmp_list_001 values(7,'he');

Query OK, 1 row affected (0.01 sec)

 

mysql> select * from tmp_list_001 ;

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

| id   | name  |

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

|    1 | wufan |

|    2 | haha  |

|    3 | xiao  |

|    4 | liu   |

|    5 | yang  |

|    6 | fei   |

|    7 | he    |

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

7 rows in set (0.00 sec)

 

mysql>

 

我们把master1上的mysql起起来,并且将keepalive也起起来。

起mysql

[root@mysql5 ~]# systemctl start mysqld

起keepalived

[root@mysql5 ~]# systemctl start keepalived

查询mysql状态

[root@mysql5 ~]# systemctl status mysqld

● mysqld.service - MySQL Server

   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)

   Active: active (running) since Mon 2019-09-02 22:49:32 CST; 9s ago

     Docs: man:mysqld(8)

           http://dev.mysql.com/doc/refman/en/using-systemd.html

  Process: 53441 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)

  Process: 53423 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)

 Main PID: 53444 (mysqld)

   CGroup: /system.slice/mysqld.service

           └─53444 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

 

Sep 02 22:49:32 mysql5.7-keepalive1 systemd[1]: Starting MySQL Server...

Sep 02 22:49:32 mysql5.7-keepalive1 systemd[1]: Started MySQL Server.

 

查询keepalived状态

[root@mysql5 ~]# 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 Mon 2019-09-02 22:49:34 CST; 14s ago

  Process: 53480 ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)

 Main PID: 53481 (keepalived)

   CGroup: /system.slice/keepalived.service

           ├─53481 /usr/local/keepalived/sbin/keepalived -D

           ├─53482 /usr/local/keepalived/sbin/keepalived -D

           └─53483 /usr/local/keepalived/sbin/keepalived -D

 

Sep 02 22:49:37 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:37 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:37 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:37 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:42 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:42 mysql5.7-keepalive1 Keepalived_vrrp[53483]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33....179

Sep 02 22:49:42 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:42 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:42 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Sep 02 22:49:42 mysql5.7-keepalive1 Keepalived_vrrp[53483]: Sending gratuitous ARP on ens33 for 192.168.30.179

Hint: Some lines were ellipsized, use -l to show in full.

[root@mysql5 ~]# netstat -ntlp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1000/sshd           

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1236/master        

tcp6       0      0 :::22                   :::*                    LISTEN      1000/sshd          

tcp6       0      0 ::1:25                  :::*                    LISTEN      1236/master        

tcp6       0      0 :::3306                 :::*                    LISTEN      53444/mysqld       

[root@mysql5 ~]#

 

在master1上查询vip,发现已经回来了。

[root@mysql5 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

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

    link/ether 00:0c:29:8b:41:06 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.177/24 brd 192.168.30.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet 192.168.30.179/32 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::58:dc06:f8c4:84b3/64 scope link

       valid_lft forever preferred_lft forever

[root@mysql5 ~]#

 

在master2上查询vip,发现已经没有了。

[root@mysql5 ~]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

       valid_lft forever preferred_lft forever

    inet6 ::1/128 scope host

       valid_lft forever preferred_lft forever

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

    link/ether 00:0c:29:27:e2:10 brd ff:ff:ff:ff:ff:ff

    inet 192.168.30.178/24 brd 192.168.30.255 scope global ens33

       valid_lft forever preferred_lft forever

    inet6 fe80::8d0:5ce9:59e6:cbb8/64 scope link

       valid_lft forever preferred_lft forever

[root@mysql5 ~]#

 

 

此时在通过vip连接mysql查询刚才插入的那条数据,发现还是在:

[root@mysql5 ~]# mysql -uroot -proot123 -h192.168.30.179

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 5

Server version: 5.7.25-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

 

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> use wufan;

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> select * from tmp_list_001 ;

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

| id   | name  |

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

|    1 | wufan |

|    2 | haha  |

|    3 | xiao  |

|    4 | liu   |

|    5 | yang  |

|    6 | fei   |

|    7 | he    |

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

7 rows in set (0.00 sec)

 

mysql>

按照前面的keepalive检测脚本,在检测到mysql端口不在的时候,会将keepalived的vip资源切换到backup主机上,但是master1上的keepalived并没有死掉,在master1上把mysql起来以后,master1会将vip资源在抢占到master1上。

 

发布了177 篇原创文章 · 获赞 43 · 访问量 46万+

猜你喜欢

转载自blog.csdn.net/kadwf123/article/details/100305572