MYSQL读写分离集群搭建(五)
前面完成了mysql集群的安装,接着完成了mycat数据库中间件的安装,
这次开始haproxy安装
一、配置Mycat状态检查服务(在mycat节点主机mymd01、mymd02上配置)
1、安装xinetd
#yum -y install xinetd
2、增加 MyCat 存活状态检测服务配置
#touch /etc/xinetd.d/mycat_status
#vi /etc/xinetd.d/mycat_status
增加以下内容:
service mycat_status
{
flags = REUSE
socket_type = stream
port = 43306
wait = no
user = root
server =/usr/local/bin/mycat_status
log_on_failure += USERID
disable = no
}
3、添加 /usr/local/bin/mycat_status 服务脚本
#vi /usr/local/bin/mycat_status
#!/bin/bash
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
给脚本赋予可执行权限
#chmod a+x /usr/local/bin/mycat_status
4、在 /etc/services 中加入 mycat_status 服务
#vi /etc/services
在末尾加入:
mycat_status 43306/tcp # mycat_status
保存后重启 xinetd 服务
#systemctl restart xinetd
验证 mycat_status 服务是否成功启动
#netstat -antup | grep 43306
二、HAproxy安装
安装yum库里面的1.8版本
#yum list|grep haproxy
haproxy.x86_64 1.5.18-9.el7 base
haproxy18.x86_64 1.8.23-3.el7 epel
pcp-pmda-haproxy.x86_64 4.3.2-13.el7_9 updates
安装1.8版本的
#yum -y install haproxy18
#systemctl enable haproxy18
验证安装的版本
#haproxy18 -v
三、HAproxy配置
vi /etc/haproxy18/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy18
pidfile /var/run/haproxy18.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy18/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
#option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen admin_stats
bind :48800
stats uri /admin-status
stats auth admin:admin
mode http
option httplog
listen mycat_servers
bind :3307
mode tcp
option tcplog
option tcpka
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance roundrobin
server mymd01 8.1.3.157:8066 check port 43306 inter 2000ms rise 2 fall 3 weight 10
server mymd02 8.1.3.158:8066 check port 43306 inter 2000ms rise 2 fall 3 weight 10
#vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
#sysctl -p
四、启动haproxy并测试
#systemctl start haproxy18
此时查看系统端口
#netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 14452/haproxy18
tcp 0 0 0.0.0.0:48800 0.0.0.0:* LISTEN 14452/haproxy18
测试一、使用MySQL客户端通过HAProxy连接Mycat
#mysql -umycatroot -pmycatJiapm605A1 -h8.1.3.155 -P3307
mysql> show databases;
mysql> select * from t1;
+---------------------+--------+
| time | db_nm |
+---------------------+--------+
| 2021-02-21 19:53:31 | test01 |
| 2021-02-21 19:53:33 | test01 |
| 2021-02-21 19:53:34 | test01 |
| 2021-02-21 19:53:34 | test01 |
| 2021-02-21 19:53:35 | test01 |
+---------------------+--------+
5 rows in set (0.00 sec)
测试二、登录 HAProxy 的状态信息统计页面
http://8.1.3.155:48800/admin-status
myha01上的HAproxy就配置完成了,同样的进行myha02的配置。