Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Building load balance of MySQL5.7+keepalived+LVS slave database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Database architecture: one master and three slaves

Master:192.168.8.57

Slave1:192.168.8.58

Slave2:192.168.8.59

Slave3:192.168.8.61

Manager:192.168.8.60

Toolkit:

Mha4mysql-manager-0.58.tar.gz

Mha4mysql-node-0.58.tar.gz

Keepalived-1.4.5.tar.gz

In the database architecture of this project, master and slave1 use MHA and keepalived to achieve automatic failover, slave2 and slave3 use keepalived and LVS to achieve load balancing. This paper focuses on the construction process of load balancing.

First, install ipvsadm

Install on slave2 and slave3

Mount / dev/cdrom / mediayum install-y ipvsadm

Second, install keepalived

Tar zxvf keepalived-1.4.5.tar.gzcd keepalived-1.4.5./configure-- prefix=/usr/local/keepalived/make & & make install

Set the boot to start automatically

Systemctl enable keepalived

Change the location of the keepalived log

The default location of keepalived log is / var/log/messages

Keep the log separately.

Modify vi / usr/local/keepalived/etc/sysconfig/keepalived

Modify KEEPALIVED_OPTIONS= "- D" to: KEEPALIVED_OPTIONS= "- D-d-S 0"

Vi / usr/local/keepalived/etc/sysconfig/keepalived# Options for keepalived. See `keepalived-- help' output and keepalived (8) and# keepalived.conf (5) man pages for a list of all options. Here are the most# common ones: # #-vrrp-P Only run with VRRP subsystem.#-check-C Only run with Health-checker subsystem.#-dont-release-vrrp-V Dont remove VRRP VIPs & VROUTEs on daemon stop.#-dont-release-ipvs-I Dont remove IPVS topology on daemon stop.#-dump-conf-d Dump the configuration data.#- -log-detail-D Detailed log messages.#-- log-facility-S 0-7 Set local syslog facility (default=LOG_DAEMON) # KEEPALIVED_OPTIONS= "- D-d-S 0"

Restart the service

Systemctl restart rsyslog

IV. Modify the keepalived configuration file

192.168.8.59

Vi / etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {router_id 859notification_email {zdd5503@163.com} notification_email_from zdd5503@163.comsmtp_server stmp.163.comsmtp_connect_timeout 30} vrrp_instance v_mysql_slave_wgpt1 {state MASTERinterface enp0s3virtual_router_id 98priority 100advert_int 1nopreemptauthentication {auth_type PASSauth_pass steven} virtual_ipaddress {192.168.8.98/24}} virtual_server 192.168.8.98 3306 {delay_loop 2lb_algo rrlb_kind DRpersistence_timeout 0protocol TCPreal_server 192. 168.8.59 3306 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3306}} real_server 192.168.8.61 3306 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3306}

192.168.8.61

Vi / etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_defs {router_id 861notification_email {zdd5503@163.com} notification_email_from zdd5503@163.comsmtp_server stmp.163.comsmtp_connect_timeout 30} vrrp_instance v_mysql_slave_wgpt1 {state BACKUPinterface enp0s3virtual_router_id 98priority 80advert_int 1nopreemptauthentication {auth_type PASSauth_pass steven} virtual_ipaddress {192.168.8.98/24}} virtual_server 192.168.8.98 3306 {delay_loop 2lb_algo rrlb_kind DRpersistence_timeout 0protocol TCPreal_server 192. 168.8.59 3306 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3306}} real_server 192.168.8.61 3306 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 3306}

Systemctl daemon-reload

Systemctl start keepalived

Systemctl enable keepalived

Ps-ef | grep keepalived

Ip a

5. Lo:0 binds VIP address and suppresses ARP broadcast

192.168.8.59

Vi / etc/rc.d/init.d/lvsmysql.sh

#! / bin/bash#. / etc/rc.d/init.d/functionsVIP1=$1case "$2" instart) echo "Start LVS of MySQL Slave REALServer" / sbin/ifconfig lo:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up/sbin/route add-host $VIP1 dev lo:0echo "1" > / proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" > / proc/sys/net/ipv4/conf/lo/arp_announceecho "1" > / proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > / proc/sys/net/ipv4/conf/all/arp_announcesysctl-p > / dev/null 2 > & 1 ; stop) / sbin/ifconfig lo:0 down/sbin/route del $VIP1 > / dev/null 2 > & 1echo "Stoped LVS of MySQL Slave Directorserver" echo "0" > / proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" > / proc/sys/net/ipv4/conf/lo/arp_announceecho "0" > / proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" > / proc/sys/net/ipv4/conf/all/arp_announce Status) isLoOn= `/ sbin/ifconfig lo:0 | grep "$VIP" `isRoOn=` / bin/netstat-rn | grep "$VIP" `if ["$isLoON" = "- a" $isRoOn "= ="]; thenecho" LVS-DR real server has run yet. "elseecho" LVS-DR real server is running. "fiexit 3ash *) echo" Usage: $0 {start | stop | status} "exit 1esacexit 0

192.168.8.61

Vi / etc/rc.d/init.d/lvsmysql.sh

#! / bin/bash#. / etc/rc.d/init.d/functionsVIP1=$1case "$2" instart) echo "Start LVS of MySQL Slave REALServer" / sbin/ifconfig lo:0 $VIP1 broadcast $VIP1 netmask 255.255.255.255 up/sbin/route add-host $VIP1 dev lo:0echo "1" > / proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" > / proc/sys/net/ipv4/conf/lo/arp_announceecho "1" > / proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" > / proc/sys/net/ipv4/conf/all/arp_announcesysctl-p > / dev/null 2 > & 1 ; stop) / sbin/ifconfig lo:0 down/sbin/route del $VIP1 > / dev/null 2 > & 1echo "Stoped LVS of MySQL Slave Directorserver" echo "0" > / proc/sys/net/ipv4/conf/lo/arp_ignoreecho "0" > / proc/sys/net/ipv4/conf/lo/arp_announceecho "0" > / proc/sys/net/ipv4/conf/all/arp_ignoreecho "0" > / proc/sys/net/ipv4/conf/all/arp_announce Status) isLoOn= `/ sbin/ifconfig lo:0 | grep "$VIP" `isRoOn=` / bin/netstat-rn | grep "$VIP" `if ["$isLoON" = "- a" $isRoOn "= ="]; thenecho" LVS-DR real server has run yet. "elseecho" LVS-DR real server is running. "fiexit 3ash *) echo" Usage: $0 {start | stop | status} "exit 1esacexit 0

6. Test load balancing

/ etc/rc.d/init.d/lvsmysql.sh 192.168.8.59 start

/ etc/rc.d/init.d/lvsmysql.sh 192.168.8.59 stop

/ etc/rc.d/init.d/lvsmysql.sh 192.168.8.61 start

/ etc/rc.d/init.d/lvsmysql.sh 192.168.8.61 stop

Echo "/ etc/rc.d/init.d/lvsmysql.sh 192.168.1.65 start" >

/ etc/rc.d/rc.local

Echo "ipvsadm-- set 15 5 15" > > / etc/rc.d/rc.local

Ifconfig

Ipvsadm-L

Mysql-uroot-pmysql-h292.168.8.59-e "select @ @ hostname;"

Mysql-uroot-pmysql-h292.168.61-e "select @ @ hostname

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report