keepalived + mysql 双主

在node2,node3上安装mysql并配置主主复制
初始化mysql
[root@node2 mysqlkeepalived]# mysqld --defaults-file=/home/mysqlkeepalived/my.cnf --basedir=/home/mysqldir --datadir=/home/mysqlkeepalived/data --user=mysql --initialize
[root@node2 mysqlkeepalived]# ls
data  my.cnf  mysql-bin.000001  mysql-bin.index  mysql-error.log  mysql-slow.log

[root@node2 mysqlkeepalived]# mysqld --defaults-file=/home/mysqlkeepalived/my.cnf --user=mysql --datadir=/home/mysqlkeepalived/data  &
[1] 3490
[root@node2 mysqlkeepalived]# 
复制配置省略


在node2,node3上安装keepalived
[root@node2 mysqlkeepalived]# yum install keepalived
已加载插件:fastestmirror
base                                                                                                                                                                                      | 3.6 kB  00:00:00     
extras                                                                                                                                                                                    | 3.4 kB  00:00:00     
updates                                                                                                                                                                                   | 3.4 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 keepalived.x86_64.0.1.3.5-6.el7 将被 安装
--> 正在处理依赖关系 ipset-libs,它被软件包 keepalived-1.3.5-6.el7.x86_64 需要
--> 正在处理依赖关系 libnetsnmpmibs.so.31()(64bit),它被软件包 keepalived-1.3.5-6.el7.x86_64 需要
--> 正在处理依赖关系 libnetsnmpagent.so.31()(64bit),它被软件包 keepalived-1.3.5-6.el7.x86_64 需要
--> 正在处理依赖关系 libnetsnmp.so.31()(64bit),它被软件包 keepalived-1.3.5-6.el7.x86_64 需要
--> 正在检查事务
---> 软件包 ipset-libs.x86_64.0.6.29-1.el7 将被 安装
---> 软件包 net-snmp-agent-libs.x86_64.1.5.7.2-33.el7_5.2 将被 安装
--> 正在处理依赖关系 libsensors.so.4()(64bit),它被软件包 1:net-snmp-agent-libs-5.7.2-33.el7_5.2.x86_64 需要
---> 软件包 net-snmp-libs.x86_64.1.5.7.2-33.el7_5.2 将被 安装
--> 正在检查事务
---> 软件包 lm_sensors-libs.x86_64.0.3.4.0-4.20160601gitf9185e5.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

=================================================================================================================================================================================================================
 Package                                               架构                                     版本                                                             源                                         大小
=================================================================================================================================================================================================================
正在安装:
 keepalived                                            x86_64                                   1.3.5-6.el7                                                      base                                      329 k
为依赖而安装:
 ipset-libs                                            x86_64                                   6.29-1.el7                                                       base                                       54 k
 lm_sensors-libs                                       x86_64                                   3.4.0-4.20160601gitf9185e5.el7                                   base                                       41 k
 net-snmp-agent-libs                                   x86_64                                   1:5.7.2-33.el7_5.2                                               updates                                   705 k
 net-snmp-libs                                         x86_64                                   1:5.7.2-33.el7_5.2                                               updates                                   749 k

事务概要
=================================================================================================================================================================================================================
安装  1 软件包 (+4 依赖软件包)

总下载量:1.8 M
安装大小:6.3 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): keepalived-1.3.5-6.el7.x86_64.rpm                                                                                                                                                  | 329 kB  00:00:00     
(2/5): lm_sensors-libs-3.4.0-4.20160601gitf9185e5.el7.x86_64.rpm                                                                                                                          |  41 kB  00:00:00     
(3/5): ipset-libs-6.29-1.el7.x86_64.rpm                                                                                                                                                   |  54 kB  00:00:00     
(4/5): net-snmp-agent-libs-5.7.2-33.el7_5.2.x86_64.rpm                                                                                                                                    | 705 kB  00:00:00     
(5/5): net-snmp-libs-5.7.2-33.el7_5.2.x86_64.rpm                                                                                                                                          | 749 kB  00:00:00     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                                             1.7 MB/s | 1.8 MB  00:00:01     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : 1:net-snmp-libs-5.7.2-33.el7_5.2.x86_64                                                                                                                                                      1/5 
  正在安装    : lm_sensors-libs-3.4.0-4.20160601gitf9185e5.el7.x86_64                                                                                                                                        2/5 
  正在安装    : 1:net-snmp-agent-libs-5.7.2-33.el7_5.2.x86_64                                                                                                                                                3/5 
  正在安装    : ipset-libs-6.29-1.el7.x86_64                                                                                                                                                                 4/5 
  正在安装    : keepalived-1.3.5-6.el7.x86_64                                                                                                                                                                5/5 
  验证中      : 1:net-snmp-agent-libs-5.7.2-33.el7_5.2.x86_64                                                                                                                                                1/5 
  验证中      : ipset-libs-6.29-1.el7.x86_64                                                                                                                                                                 2/5 
  验证中      : lm_sensors-libs-3.4.0-4.20160601gitf9185e5.el7.x86_64                                                                                                                                        3/5 
  验证中      : keepalived-1.3.5-6.el7.x86_64                                                                                                                                                                4/5 
  验证中      : 1:net-snmp-libs-5.7.2-33.el7_5.2.x86_64                                                                                                                                                      5/5 

已安装:
  keepalived.x86_64 0:1.3.5-6.el7                                                                                                                                                                                

作为依赖被安装:
  ipset-libs.x86_64 0:6.29-1.el7         lm_sensors-libs.x86_64 0:3.4.0-4.20160601gitf9185e5.el7         net-snmp-agent-libs.x86_64 1:5.7.2-33.el7_5.2         net-snmp-libs.x86_64 1:5.7.2-33.el7_5.2        

完毕!
[root@node2 mysqlkeepalived]# rpm -qa |grep keepalived
keepalived-1.3.5-6.el7.x86_64
[root@node2 mysqlkeepalived]# 


mysql状态检测脚本:
[root@node2 keepalived]# cat checkmysql.sh 
#!/bin/bash
mysqlstr=/usr/bin/mysql
host=192.168.88.18
user=root
password=root001
port=3381
mysql_status=1
$mysqlstr -h $host -u $user -p$password -P$port -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
    echo "mysql_status=1"
    exit 0
else
systemctl stop keepalived.service
fi


keepalived的配置文件
vrrp_script chk_mysql
{
        #执行脚本的路径
        script "/etc/keepalived/checkmysql.sh"
        #调用脚本两次之间的间隔,默认为1秒
        interval 10
}
vrrp_instance VI_1
{
    #状态只有MASTER和BACKUP两种,并且要大写,MASTER为工作状态,BACKUP是备用状态。
    state BACKUP
    nopreempt
    interface eth0
    #虚拟路由标识,同一个vrrp_instance的MASTER和BACKUP的vitrual_router_id 是一致的。
    virtual_router_id 110
    #优先级,同一个vrrp_instance的MASTER优先级必须比BACKUP高。
    priority 101 //backup比该值小
    #MASTER 与BACKUP 负载均衡器之间同步检查的时间间隔,单位为秒。
    advert_int 5
    #验证authentication。包含验证类型和验证密码。类型主要有PASS、AH 两种,通常使用的类型为PASS,
    #auth_pass 1111   据说AH 使用时有问题。验证密码为明文,同一vrrp 实例MASTER 与BACKUP 
    #使用相同的密码才能正常通信。
    #mcast_src_ip 192.168.80.110
    authentication
    {
        auth_type PASS
        auth_pass 1111
    }
    #虚拟ip地址,可以有多个地址,每个地址占一行,不需要子网掩码,同时这个ip 必须与我们在lvs 
    #客户端设定的vip 相一致!
    virtual_ipaddress
    {
        192.168.88.223
    }
    track_script
    {
        #调用检测脚本
        chk_mysql
        #chk_http_port
    }
}

vrrp_script chk_mysql
{
        script "/etc/keepalived/checkmysql.sh"
        interval 10
}
vrrp_instance VI_1
{

    state BACKUP
    nopreempt
    interface eth0

    virtual_router_id 110

    priority 90 
    advert_int 5
   
    authentication
    {
        auth_type PASS
        auth_pass 1111
    }
   
    virtual_ipaddress
    {
        192.168.88.223
    }
    track_script
    {
        
        chk_mysql
    }
}


在node2,node3上启动keepalived
[root@node2 keepalived]# systemctl start keepalived.service
[root@node2 keepalived]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2018-08-18 00:11:15 CST; 2s ago
  Process: 3912 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3913 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─3913 /usr/sbin/keepalived -D
           ├─3914 /usr/sbin/keepalived -D
           └─3915 /usr/sbin/keepalived -D

8月 18 00:11:15 node2 Keepalived_vrrp[3915]: Opening file '/etc/keepalived/keepalived.conf'.
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: VRRP parsed invalid IP {. skipping IP...
8月 18 00:11:15 node2 Keepalived_vrrp[3915]:      { no match, ignoring...
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: VRRP_Instance(VI_1) removing protocol VIPs.
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: Using LinkWatch kernel netlink reflector...
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: VRRP_Instance(VI_1) Entering BACKUP STATE
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
8月 18 00:11:15 node2 Keepalived_vrrp[3915]: VRRP_Script(chk_mysql) succeeded
[root@node2 keepalived]# 
[root@node3 keepalived]# systemctl status keepalived.service
* keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@node3 keepalived]# systemctl start keepalived.service
[root@node3 keepalived]# 
[root@node3 keepalived]# 
[root@node3 keepalived]# systemctl status keepalived.service
* keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 1 2018-08-18 00:10:08 CST; 2s ago
  Process: 3665 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 3666 (keepalived)
   CGroup: /system.slice/keepalived.service
           |-3666 /usr/sbin/keepalived -D
           |-3667 /usr/sbin/keepalived -D
           `-3668 /usr/sbin/keepalived -D

日志
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: VRRP parsed invalid IP {. skipping IP...
8? 18 00:10:08 node3 Keepalived_vrrp[3668]:      { no match, ignoring...
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: VRRP_Instance(VI_1) removing protocol VIPs.
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: Using LinkWatch kernel netlink reflector...
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: VRRP_Instance(VI_1) Entering BACKUP STATE
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
8? 18 00:10:08 node3 Keepalived_vrrp[3668]: VRRP_Script(chk_mysql) succeeded
8? 18 00:10:08 node3 Keepalived_healthcheckers[3667]: Opening file '/etc/keepalived/keepalived.conf'.
vip已经挂载优先级高的节点上
[root@node3 keepalived]# ip addr show
1: lo: 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:cd:d4:6d brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.19/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.88.223/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fecd:d46d/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:b7:c9:1b:76 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever


测试从node2 切换到node3
[root@node2 ~]# ip addr show
1: lo: 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:82:9a:e9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.18/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.88.223/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe82:9ae9/64 scope link 
       valid_lft forever preferred_lft forever
[root@node2 ~]# 
停止node2上的mysql
[root@node2 ~]# mysqladmin -uroot -proot001 -P3381 -S /home/mysqlkeepalived/mysql.sock shutdown
[root@node2 ~]# 
[root@node2 ~]# 
node2上的vip已经卸载
[root@node2 ~]# ip addr show
1: lo: 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:82:9a:e9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.18/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fe82:9ae9/64 scope link 
       valid_lft forever preferred_lft forever
[root@node2 ~]#

[root@node3 ~]# 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 1 2018-08-18 12:01:34 CST; 12min ago
  Process: 4877 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 4878 (keepalived)
   CGroup: /system.slice/keepalived.service
           |-4878 /usr/sbin/keepalived -D
           |-4879 /usr/sbin/keepalived -D
           `-4880 /usr/sbin/keepalived -D

8? 18 12:01:34 node3 Keepalived_vrrp[4880]: Opening file '/etc/keepalived/keepalived.conf'.
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP parsed invalid IP {. skipping IP...
8? 18 12:01:34 node3 Keepalived_vrrp[4880]:      { no match, ignoring...
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) removing protocol VIPs.
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: Using LinkWatch kernel netlink reflector...
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) Entering BACKUP STATE
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
8? 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP_Script(chk_mysql) succeeded
[root@node3 ~]# tail -f /var/log/messages
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: Opening file '/etc/keepalived/keepalived.conf'.
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: WARNING - default user 'keepalived_script' for script execution does not exist - please create.
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP parsed invalid IP {. skipping IP...
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]:     { no match, ignoring...
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: SECURITY VIOLATION - scripts are being executed but script_security not enabled.
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) removing protocol VIPs.
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: Using LinkWatch kernel netlink reflector...
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) Entering BACKUP STATE
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Aug 18 12:01:34 node3 Keepalived_vrrp[4880]: VRRP_Script(chk_mysql) succeeded

keepalived的日志
Aug 18 12:14:38 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) Transition to MASTER STATE
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) Entering MASTER STATE
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) setting protocol VIPs.
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.88.223
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:43 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:48 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:48 node3 Keepalived_vrrp[4880]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.88.223
Aug 18 12:14:48 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:48 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:48 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
Aug 18 12:14:48 node3 Keepalived_vrrp[4880]: Sending gratuitous ARP on eth0 for 192.168.88.223
vip已经挂载到node3上
[root@node3 ~]# ip addr show
1: lo: 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
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:cd:d4:6d brd ff:ff:ff:ff:ff:ff
    inet 192.168.88.19/24 brd 192.168.88.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.88.223/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fecd:d46d/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: mtu 1500 qdisc noqueue state DOWN 
    link/ether 02:42:b7:c9:1b:76 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
使用vip登入mysql
[root@node3 ~]#  mysql -uroot -p -P3381 -h 192.168.88.223
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 93
Server version: 5.7.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> 

你可能感兴趣的:(mysql)