(10) Haproxy + preserved build Mycat cluster to achieve high availability

We can use haproxy + preserved with two Mycat to build a Mycat cluster to achieve high availability. HAProxy

It realizes the cluster high availability and load balancing of MyCat,

The high availability of HAProxy itself can be achieved through Keepalived.


Node description



Internet ip

Intranet ip













Virtual ip assumed



1.ali-shuidi03,ali-shuidi04 MySQL master-slave replication


2. Install mycat in ali-shuidi01 and ali-shuidi02 to separate reading and writing of MySQL and copy the master and slave


3. To install and configure HAProxy, ali-shuidi03 and ali-shuidi04, perform the following operations


3.1 prepare HAProxy installation package and transfer it to / opt directory

3.2 unzip to / usr/local/src

tar -zxvf haproxy-1.5.18.tar.gz -C /usr/local/src


3.3 enter the directory after decompression, check the kernel version, and compile

cd /usr/local/src/haproxy-1.5.18/


[root@shuidi03 haproxy-1.8.5]# uname -r
make TARGET=linux310 PREFIX=/usr/local/haproxy ARCH=x86_64

#Arget = Linux 310, kernel version, use uname -r to view the kernel, such as: 3.10.0-693.2.2.el7.x86_64. At this time, the parameter is linux310;

#ARCH=x86_64, system digits;

#Prefix = / usr / local / haprpxy ා / usr / local / haprpxy, is the installation path of haprpxy.


3.4 install after compilation

make install PREFIX=/usr/local/haproxy

3.5 after installation, create directory and HAProxy configuration file

mkdir -p /usr/data/haproxy/
vim /usr/local/haproxy/haproxy.conf



 log local0
 #log local1 notice
 #log loghost local0 info
 maxconn 4096
 chroot /usr/local/haproxy
 pidfile /usr/data/haproxy/haproxy.pid
 uid 99
 gid 99
 log global
mode tcp
 option abortonclose
 option redispatch
 retries 3
 maxconn 2000
 timeout connect 5000
 timeout client 50000
 timeout server 50000
listen proxy_status 
bind :48066
 mode tcp
 balance roundrobin
 server mycat_1 check inter 10s
 server mycat_2 check inter 10s
frontend admin_stats 
bind :7777
 mode http
 stats enable
 option httplog
 maxconn 10
 stats refresh 30s
 stats uri /admin
 stats auth admin:123123
 stats hide-version
 stats admin if TRUE


3.6 start HAProxy

/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf


3.7 view HAProxy process

[root@shuidi03 haproxy]# ps -ef|grep haproxy
nobody   29577     1  0 23:29 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf
root     31209  3443  0 23:29 pts/2    00:00:00 grep --color=auto haproxy


3.8 open browser access


In the pop-up box, enter the user name: admin Password: 123123. If both the primary and secondary Mycat computers are started, you can see the following figure


3.9 verify load balancing and access Mycat through HAProxy

mysql -umycat -pzan123456 -h -P 48066



4. Configure preserved ali-shuidi03 and ali-shuidi04 to perform the following operations

4.1 prepare the preserved installation package and transfer it to / opt directory


4.2 unzip to / usr/local/src

tar -zxvf keepalived-1.4.2.tar.gz -C /usr/local/src


4.3 install dependent plug-ins

yum install -y gcc openssl-devel popt-devel


4.4 enter the decompressed directory for configuration and compilation

cd /usr/local/src/keepalived-1.4.2


./configure --prefix=/usr/local/keepalived


4.5 compile and install after completion

make && make install


4.6 configuration before operation

cp /usr/local/src/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/src/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

4.7 modify configuration file

vim /etc/keepalived/keepalived.conf


#The modification is as follows

! Configuration File for keepalived

global_defs {
	notification_email {
		shizan666 @163.com
	notification_email_from keepalived @showjoy.com
	smtp_server 127.0 .0 .1
	smtp_connect_timeout 30
	router_id LVS_DEVEL
	vrrp_garp_interval 0
	vrrp_gna_interval 0
vrrp_instance VI_1 {
	#The main machine is equipped with MASTER, and the standby machine is equipped with BACKUP
	state MASTER
	# Network card of the machine
	interface eth0
	virtual_router_id 51
	# The higher the value, the higher the priority
	priority 100
	advert_int 1
	authentication {
		auth_type PASS
		auth_pass 1111
	virtual_ipaddress {
		#Virtual IP
		172.16 .192 .136
virtual_server 172.16 .192 .136 48066 {
	delay_loop 6
	lb_algo rr
	lb_kind NAT
	persistence_timeout 50
	protocol TCP
	real_server 172.16 .192 .134 48066 {
		weight 1
			connect_timeout 3
			retry 3
			delay_before_retry 3
	real_server 172.16 .192 .135 48600 {
		weight 1
			connect_timeout 3
			nb_get_retry 3
			delay_before_retry 3



4.8 start preserved

service keepalived start


Access through virtual ip login

mysql -umycat -pzan123456 -h -P 48066


4.9 for high availability test, close one of mycat, and it can still be accessed through virtual ip (because alicloud test environment cannot generate virtual ip, so this step is not tested)











Tags: mycat MySQL vim Linux

Posted on Sun, 07 Jun 2020 03:12:10 -0700 by Clarkeez