Linux Keepalived + Mysql

Ka1:

vrrp_script check_mysql {
    script "/data/check_mysql.sh"
    interval 1
    weight -30
    fall 3
    rise 5
    timeout 2
}



vrrp_instance MYSQL_1 {
    state MASTER
    interface eth0
    virtual_router_id 99
    #nopreempt
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.40/24 dev eth0 label eth0:2
    }

    track_interface {
        eth0
    }

    track_script {
        check_mysql
    }
}

ka2:

vrrp_instance MYSQL_1 {
    state BACKUP
    interface eth0
    virtual_router_id 99
    #nopreempt
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        10.0.0.40/24 dev eth0 label eth0:2
    }

    track_interface {
        eth0
    }
}

并在ka1和ka2配置mysql主主复制

1> 当刚开始ka1,ka2和mysql都活动时

[23:25:08 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 10.0.0.40 172.17.0.1 


[23:04:45 root@ka2 ~]#hostname -I
10.0.0.33 10.0.0.30 172.17.0.1 

[23:25:56 root@rs1 ~]#mysql -urepluser -p123456 -h10.0.0.40 -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+
| @@hostname    |
+---------------+
| ka1.noise.org |
+---------------+

2》 当ka1的mysql停止时

[23:25:24 root@ka1 ~]#systemctl stop mysql
[23:26:58 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 


[23:10:27 root@ka2 ~]#hostname -I
10.0.0.33 10.0.0.30 10.0.0.40 172.17.0.1 

[23:27:12 root@rs1 ~]#mysql -urepluser -p123456 -h10.0.0.40 -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------+
| @@hostname     |
+----------------+
| ka2.noisedu.cn |
+----------------+

3》 当ka1的mysql重新启动时

[23:27:02 root@ka1 ~]#systemctl restart mysql
[23:27:34 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:37 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:38 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:38 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:38 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:40 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:40 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:41 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:41 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 172.17.0.1 
[23:27:42 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 10.0.0.40 172.17.0.1 
[23:27:42 root@ka1 ~]#hostname -I
10.0.0.34 10.0.0.20 10.0.0.40 172.17.0.1 


[23:27:06 root@ka2 ~]#hostname -I
10.0.0.33 10.0.0.30 172.17.0.1 


[23:27:12 root@rs1 ~]#mysql -urepluser -p123456 -h10.0.0.40 -e 'select @@hostname'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------+
| @@hostname    |
+---------------+
| ka1.noise.org |
+---------------+

 

posted @ 2021-09-06 23:33  每天都在学习的自己  阅读(76)  评论(0)    收藏  举报