preface

The basic operation of myCAT has been written last time, which is expected to be sufficient to meet your actual needs. But in a modern world without clustering, you can’t be sure that your service is highly available.

Therefore, this paper mainly includes myCAT cluster, myCAT + HA load balancing and other operations.


Mycat cluster

1. Install the ZooKeeper cluster

Mycat is a database middleware produced by Ali, which can rely on ZooKeeper for clustering. Therefore, you need to prepare a ZooKeeper cluster. The detailed operations are as follows:

The Zookeeper cluster is created

2. Configure mycat to connect to ZooKeeper

Copy your mycat configuration files (server. XML, schema. XML, rule-xml, sequence_conf.properties) into the zkconf directory of the Mycat configuration file

cp schema.xml server.xml rule.xml sequence_conf.properties zkconf/
Copy the code

3. Initialize myCAT configuration to zK cluster

First find mycat provide initialization data to the zookeeper shell script: / usr/local/mycat/bin/init_zk_data. Sh execute the command

../bin/init_zk_data.sh
Copy the code

-bash:./init_zk_data.sh: /bin/bash^M: bad interpreter: No such file or directory

Solution:

http://8rr.co/v9ms

http://8rr.co/v9mE

4. Connect to ZooKeeper and verify the connection

Connect to the ZooKeeper server and perform this operation on the ZOOKeeper client

ls /mycat/mycat-cluster-1/schema
get /mycat/mycat-cluster-1/schema/dataHost
Copy the code

5. Configure MyCat to support ZK startup

Find the myid.properties file in the conf directory of all mycat files and modify it as follows:

LoadZk = true zkURL = 192.168.56.10:2181192168 56.11:2181192168 56.12:2181 clusterId myid = = mycat - cluster - 1 mycat_01 clusterSize=2 clusterNodes=mycat_01,mycat_02 #server booster ; booster install on db same server,will reset all minCon to 2 type=server boosterDataHosts=dataHost1Copy the code

6. Then restart all myCat

You will find that other mycat that is not configured will automatically find all configuration of server. XML, schema. XML, rule-xml, etc.

The mycat cluster has been set up!!

Mycat +HA implements high availability

The cluster built by myCAT + ZK above can not achieve load balancing and high availability. You also need to use HaProxy + Keepalived to truly be highly available.

The following configuration is done on each server where MyCat resides.

1. Install network daemon service xinetd

yum -y install xinetd

vi /etc/xinetd.conf    Make sure the configuration includes the last line includedir XXXX
Copy the code

Create a mycat_status service

vi /etc/xinetd.d/mycat_status
### Reads as follows:
service mycat_status
{
        flags = REUSE
 socket_type = stream  port = 48700  wait = no  user = root  server = /usr/local/mycat/bin/mycat_status  log_on_failure += USERID  disable = no } Copy the code

Create a status script that checks myCat

vi /usr/local/mycat/bin/mycat_status
Copy the code

To assign operable permissions to a script:

chmod a+x /usr/local/mycat/bin/mycat_status
Copy the code

Script contents to check mycat status:

#! /bin/bash
#/usr/local/mycat/bin/mycat_status
# This script checks if a mycat server is healthy running on localhost. It will
# return:
#
# "HTTP/1.x 200 OK\r" (if mycat is running smoothly) # # "HTTP/1.x 503 Internal Server Error\r" (else) mycat=`/usr/local/mycat/bin/mycat status | grep 'not running' | wc -l` if [ "$mycat" = "0" ]; then /bin/echo -e "HTTP/1.1 200 OK\r\n" else /bin/echo -e "HTTP/1.1 503 Service Unavailable\r\n" fi Copy the code

Register a mycat_status service

vi /etc/services
### reads as follows:
mycat_status    48700/tcp               # mycat_status
Copy the code

Finally, start xinetd:

systemctl restart xinetd
Copy the code

2. Install HaProxy

Yum -y install haproxy

Vi /etc/haproxy. haproxy. CFG Change the configuration to the IP address and port number of the haproxy. CFG file

Maxconn 4096 chroot /usr/local/haproxy daemon

defaults log global option dontlognull retries 3 option redispatch maxconn 2000 timeout connect 5000 timeout client 50000 timeout server 50000

Listen admin_status bind :1080 ##VIP stats uri /stats ## stats auth admin:admin mode HTTP option httplog

Listen allmycat_service bind :8096 ## Mode TCP option tcplog option HTTPCHK OPTIONS * HTTP/1.1\r\nHost:\ WWW balance roundrobin server mycat_10 192.168.56.10:8066 Check port 48700 INTER 5S Rise 2 Fall 3 Server myCAT_12 192.168.56.12:8066 Check port 48700 Inter 5s rise 2 fall 3 timeout server 20000

Listen allmycat_admin bind :8097 ## forward to mycat port 9066, Mode TCP option tcplog option HTTPCHK OPTIONS * HTTP/1.1\r\nHost:\ WWW balance roundrobin server Mycat_10 192.168.56.10:9066 Check Port 48700 Inter 5s Rise 2 Fall 3 Server mycat_12 192.168.56.12:9066 Check port 48700 inter 5s rise 2 fall 3 timeout server 20000

Copy the code

Start the haproxy:

haproxy -f /etc/haproxy/haproxy.cfg
Copy the code

Verify: netstat -ntlp Checks whether ports 8096 and 8097 are started successfully.

Mycat browser visit http://192.168.56.12:1080/stats to view the status is UP (with respect to ok) is as follows:


3, install Keepalived

Yum -y install keepalived

Add virtual IP www.cnblogs.com/qlong8807/p…

Modifying a Configuration File

Vi/etc/keepalived/keepalived. Conf # # # content is as follows: # # # # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

! Configuration File for keepalived ##global_defs {

Keepalived comes with mail reminders that require the Sendmail service to be enabled. Independent monitoring or third-party SMTP is recommended

Router_id liuyazhuang135 ## Identifies the note for this node, usually hostname

# #}

Keepalived executes scripts periodically and analyzes the results of script execution, dynamically adjusting the priority of VRrP_instance.

If the script execution result is 0 and the weight configuration value is greater than 0, the priority is increased accordingly.

If the script execution result is non-zero and the weight configuration value is less than 0, the priority is reduced accordingly.

In other cases, keep the original priority, that is, the value of priority in the configuration file.

Vrrp_script chk_haproxy {script "/etc/keepalived/haproxy_check.sh" ## Check haproxy status script path interval 2 ## check interval weight 2 ## If the condition is true, the weight +2}

Define a virtual route. VI_1 is the identifier of the virtual route

Copy the code

Vrrp_instance VI_1 {state MASTER ## set the primary and secondary devices to BACKUP. ## Set the primary and secondary devices to BACKUP. Eth3 virtual_router_id 51 ## Eth3 virtual_router_id 51 ## The last segment of the optional IP address is used, ## the same VRID is a group, which will determine the multicast MAC address priority 150 ## node priority, value range 0-254, Nopreempt # nopreempt # nopreempt # nopreempt # nopreempt # nopreempt # nopreempt Authentication {auth_type PASS auth_pass 1111 ## } ## add track_script block to instance configuration block track_script {chk_haproxy ## check if HAProxy service exists} ## virtual IP pool, Virtual_ipaddress {192.168.56.88 ## virtual IP address}}

Add a script to check if HaProxy is alive

vi /etc/keepalived/haproxy_check.sh 
Copy the code

Note that the START_HAPROXY path should correspond to your own installation path:

#! /bin/bash
START_HAPROXY="/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg"
STOP_KEEPALIVED="/etc/init.d/keepalived stop"
LOG_FILE="/var/log/keepalived-haproxy-state.log"
HAPS=`ps -C haproxy --no-header |wc -l`
date "+%Y-%m-%d %H:%M:%S" >> $LOG_FILE echo "check haproxy status" >> $LOG_FILE if [ $HAPS -eq 0 ]; thenecho $START_HAPROXY >> $LOG_FILE $START_HAPROXY >> $LOG_FILE2 > &1 sleep 3 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ]; thenecho "start haproxy failed, killall keepalived" >> $LOG_FILE killall keepalived fi fi Copy the code

Grant permissions to scripts

chmod a+x /etc/keepalived/haproxy_check.sh
Copy the code

Starting keepalived

service keepalived start
Copy the code

Verify that the operation is successful.

You can run service Keepalived stop on one primary server to see if the IP addr of the other backup server has been added to the virtual IP address

Refer to the article

Mycat – Implementation of High Availability Load Balancing Cluster (HAProxy + Keepalived + Mycat)

Mycat cluster deployment (based on HAProxy + Mycat

Mycat — Mycat cluster optimization

SQL statement not supported by MyCat

Mycat distributed transaction implementation

If you want to know more about Java, you can follow the public account [Love Coding]


This article is formatted using MDNICE