最近主要在做mycat分庫分表這一塊的東西,踩過的坑很多,以后會(huì)慢慢分享下
一、配置MyCat狀態(tài)檢查服務(wù)(在MyCat節(jié)點(diǎn)主機(jī)上配置)
安裝xinetd插件
yum install xinetd -y
cd /etc/xinetd.d
touch mycat_status
vim /etc/xinetd.d/mycat_status
service mycat_status
{
flags = REUSE
socket_type = stream
port = 48700
wait = no
user = nobody
server =/opt/export/app/mycat_status
log_on_failure += USERID
disable = no
}
創(chuàng)建xinetd啟動(dòng)服務(wù)腳本
vim /opt/export/app/mycat_status
#!/bin/bash
#/usr/local/bin/mycat_status.sh
# 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=`/opt/export/app/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
修改腳本文件權(quán)限(注意)
我就是在這里被坑了很久,根據(jù)權(quán)威指南上面mycat_status這個(gè)腳本里面的內(nèi)容也有很多問題,好幾個(gè)地方?jīng)]有空格.
chmod u+x /etc/xinetd.d/mycat_status
chmod u+x /opt/export/app/mycat_status
將啟動(dòng)腳本加入服務(wù)
vim /etc/services
在末尾加入
mycat_status 48700/tcp # mycat_status
重啟xinetd服務(wù)
service xinetd restart
將xinetd加入自啟動(dòng)服務(wù)
chkconfig --add xinetd
chkconfig --level 2345 xinetd on
注意:
如果權(quán)限不足,需要使用sudo權(quán)限來啟動(dòng)
二、 HaProxy安裝和配置(mycat節(jié)點(diǎn)之外)
haproxy安裝
添加用戶
useradd haproxy
sudo passwd haproxy
然后添加密碼
創(chuàng)建目錄
mkdir /opt/export/app/haproxy
將haproxy-1.5.16.tar.gz放置該目錄下
然后下面執(zhí)行一下下面的命令
tar zxvf /opt/export/app/haproxy/haproxy-1.5.16.tar.gz
mkdir /opt/export/app/haproxy/haproxy
cd /opt/export/app/haproxy/haproxy-1.5.16
-- 編譯安裝
make TARGET=linux26 PREFIX=/opt/export/app/haproxy/haproxy ARCH=x86_64make install PREFIX=/opt/export/app/haproxy/haproxy
haproxy配置
cd /opt/export/app/haproxy/haproxy
touch haproxy.cfg
vim haproxy.cfg
global
log 127.0.0.1 local0 ##記日志的功能
maxconn 4096
chroot /opt/export/app/haproxy/haproxy
user haproxy
group haproxy
node ha-01
daemon
defaults
log global
option dontlognull
retries 3
option redispatch
maxconn 2000
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
listen admin_stats 172.16.2.4:48800 ##統(tǒng)計(jì)頁面
stats uri /admin-status
stats auth admin:admin
mode http
option httplog
listen mycat_service 172.16.2.4:3306 ##客戶端就是通過這個(gè)ip和端口進(jìn)行連接,這個(gè)vip和端口綁定的是mycat8066端口
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance roundrobin
server mycat01 172.16.1.93:8066 check port 48700 inter 5s rise 2 fall 3
server mycat02 172.16.2.1:8066 check port 48700 inter 5s rise 2 fall 3
timeout server 20000
listen mycat_admin 172.16.2.4:3406 ##客戶端就是通過這個(gè)ip和端口進(jìn)行連接,這個(gè)vip和端口綁定的是mycat9066端口
mode tcp
option tcplog
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
balance roundrobin
server mycat01 172.16.1.93:9066 check port 48700 inter 5s rise 2 fall 3
server mycat02 172.16.2.1:9066 check port 48700 inter 5s rise 2 fall 3
timeout server 20000
配置haproxy記錄日志功能
yum –y install rsyslog
mkdir /etc/rsyslog.d
cd /etc/rsyslog.d/
touch haproxy.conf
vim haproxy.conf
$ModLoad imudp
$UDPServerRun 514
local0.* /opt/export/log/haproxy/haproxy.log
vim /etc/rsyslog.conf
在#### RULES ####上面一行加入以下內(nèi)容
# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
在local7.* /var/log/boot.log下面加入以下內(nèi)容
local0.* /opt/export/log/haproxy/haproxy.log
重啟rsyslog服務(wù)
service rsyslog restart
將rsyslog加入自動(dòng)啟動(dòng)服務(wù)
chkconfig --add rsyslog
chkconfig --level 2345 rsyslog on
創(chuàng)建haproxy啟停腳本
啟動(dòng)腳本
touch /opt/export/app/haproxy/haproxy/sbin/start &&
chmod +x /opt/export/app/haproxy/haproxy/sbin/start &&
vim /opt/export/app/haproxy/haproxy/sbin/start
#!/bin/sh
/opt/export/app/haproxy/haproxy/sbin/haproxy -f /opt/export/app/haproxy/haproxy/haproxy.cfg &
停止腳本
touch /opt/export/app/haproxy/haproxy/sbin/stop &&
chmod +x /opt/export/app/haproxy/haproxy/sbin/stop &&
vim /opt/export/app/haproxy/haproxy/sbin/stop
#!/bin/sh
ps -ef | grep sbin/haproxy | grep -v grep |awk '{print $2}'|xargs kill -s 9
授權(quán)
chown -R haproxy.haproxy /opt/export/app/haproxy/haproxy/*
啟動(dòng)
sudo /opt/export/app/haproxy/haproxy/sbin/start
檢查haproxy是否啟動(dòng)成功
netstat -ntlp | grep :48800