资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

keepalivedMysql双主热备配置的示例分析-创新互联

这篇文章主要介绍keepalived Mysql双主热备配置的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

目前创新互联建站已为上千的企业提供了网站建设、域名、虚拟空间、网站改版维护、企业网站设计、玉树网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

基于keepalived双主热备:
一.环境:
OS:CentOS 6.5 X64
DB Version:Percona Mysql 5.7.15-9-log
路径:/app/mysql57
数据文件路径:/data/mysql57/data
DB1:192.168.213.4
DB2:192.168.213.5


二.安装keepalived包(两台主机都需操作)
1.下载keepalived包
cd /usr/local/tools
wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz


2.安装所需包
yum -y install openssl-devel


3.安装keepalived
mkdir -pv /app/keepalived
cd /usr/local/tools
tar xf keepalived-1.2.23.tar.gz
cd keepalived-1.2.23
./configure --prefix=/app/keepalived/
make
make install


4.配置keepalived
cp /app/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
cp /app/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
cp /app/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /app/keepalived/sbin/keepalived /usr/sbin/


db01 keepalived配置
cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
   router_id HA_MYSQL
}
vrrp_script chk_mysql {
script "/etc/init.d/mysql57 status"
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0    !虚拟IP对应接口,也就是业务接口对应的网卡编号
    virtual_router_id 51
    priority 105    !权重
    advert_int 1
    nopreempt    !自己上线后不抢占当前 master 的位置,即使其权重比自己低
    virtual_ipaddress {
        192.168.213.100    !VIP
    }
    track_script {
        chk_mysql    !启用头部定义的检查MySQL状态的脚本
    }
}
virtual_server 192.168.213.100 3306 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    persistence_timeout 60
    protocol TCP    sorry_server 192.168.213.5 3306    ! real_server 挂掉后临时使用 sorry_server 顶上
    real_server 192.168.213.4 3306 {
        weight 1
        TCP_CHECK {
            connect_port    3306
            connect_timeout 3
        }
    }
}




db02 keepalived配置
[root@ORADB-213-5 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
   router_id HA_MYSQL
}
vrrp_script chk_mysql {
script "/etc/init.d/mysql57 status"
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0    !虚拟IP对应接口,也就是业务接口对应的网卡编号
    virtual_router_id 51
    priority 100    !权重
    advert_int 1
    nopreempt    !自己上线后不抢占当前 master 的位置,即使其权重比自己低
    virtual_ipaddress {
        192.168.213.100    !VIP
    }
    track_script {
        chk_mysql    !启用头部定义的检查MySQL状态的脚本
    }
}
virtual_server 192.168.213.100 3306 {
    delay_loop 3
    lb_algo rr
    lb_kind NAT
    persistence_timeout 60
    protocol TCP    sorry_server 192.168.213.4 3306    ! real_server 挂掉后临时使用 sorry_server 顶上
    real_server 192.168.213.5 3306 {
        weight 1
        TCP_CHECK {
            connect_port    3306
            connect_timeout 3
        }
    }
}


5.两台DB启动keepalived
/etc/init.d/keepalived start


查看VIP
[root@ORA11G-213-4 keepalived-1.2.23]# ip addr
1: lo:  mtu 16436 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:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:c7:d5:27 brd ff:ff:ff:ff:ff:ff
    inet 192.168.213.4/24 brd 192.168.213.255 scope global eth0
    inet 192.168.213.100/32 scope global eth0
    inet6 fe80::20c:29ff:fec7:d527/64 scope link 
       valid_lft forever preferred_lft forever
  
测试
[root@ORA11G-213-4 keepalived-1.2.23]# mysql -unetdata -pnetdata -h 192.168.213.100
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 2881
Server version: 5.7.15-9-log Source distribution


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


netdata@192.168.213.100 23:41:  [(none)]> system ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:C7:D5:27  
          inet addr:192.168.213.4  Bcast:192.168.213.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fec7:d527/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:156643 errors:0 dropped:0 overruns:0 frame:0
          TX packets:191173 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:39459924 (37.6 MiB)  TX bytes:31911844 (30.4 MiB)


lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:17378 errors:0 dropped:0 overruns:0 frame:0
          TX packets:17378 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:1002566 (979.0 KiB)  TX bytes:1002566 (979.0 KiB)
 
停止掉db01
[root@ORA11G-213-4 keepalived-1.2.23]# /etc/init.d/mysql57 stop
Shutting down MySQL (Percona Server)............. SUCCESS! 


查看db02
[root@ORADB-213-5 ~]# ip addr
1: lo:  mtu 16436 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:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:63:54:39 brd ff:ff:ff:ff:ff:ff
    inet 192.168.213.5/24 brd 192.168.213.255 scope global eth0
    inet 192.168.213.100/32 scope global eth0
    inet6 fe80::20c:29ff:fe63:5439/64 scope link 
       valid_lft forever preferred_lft forever

以上是“keepalived Mysql双主热备配置的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联-成都网站建设公司行业资讯频道!


分享名称:keepalivedMysql双主热备配置的示例分析-创新互联
URL标题:http://cdkjz.cn/article/cssdpg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220