这前做了Percona xtraDB Cluster(配置见链接
http://hans925.blog.51cto.com/542170/1607157)
现在用Haproxy给Percona xtraDB Cluster做负载均衡,机器还是那两台机器,(本来做负载均衡要三台机器,但个人笔记本跑两个虚拟机就有点卡了所以就用了两个)。就把Haproxy安装到第一台percona上了。
Haproxy
OS: Ubuntu 14.04 server IP: 172.16.53.136
Percona- Node 1
OS: Ubuntu 14.04 server IP: 172.16.53.136
Percona- Node 2
OS: Ubuntu 14.04 server IP: 172.16.53.137
两台Percona都要启动起来:
mysql -u root -p -e "INSERT INTO mysql.user (Host,User) values (‘172.16.53.136‘,‘haproxy_check‘); FLUSH PRIVILEGES;"
root@mysql-1# mysql -u root -p -e "GRANT ALL PRIVILEGES ON *.* TO ‘haproxy_root‘@‘172.16.53.136‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION; FLUSH PRIVILEGES"
数据库要能从haproxy机器登录。
安装Haproxy
ubuntu安装下面链接里提供的方法就能安装好
我用的是1.5版本,安装完后执行
sed -i "s/ENABLED=0/ENABLED=1/" /etc/default/haproxy
配置:
vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
uid 99
gid 99
daemon
#debug
quiet
defaults
log global
mode http
option tcplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
listen mysql-cluster 0.0.0.0:3307 (如果用两台机器测试 要把这个端口改掉,默认为3306)
mode tcp
balance roundrobin
option tcpka
#option httpchk 如果机器上没安装 http服务,(没开80端口)就把 option httpchk 注释或删除.
server db01.com 172.16.53.136 check port 9200 inter 12000 rise 3 fall 3
server db02.com 172.16.53.137:3306 check port 9200 inter 12000 rise 3 fall 3
listen stats 0.0.0.0:8080
mode http
option httpchk
balance roundrobin
stats uri /
stats refresh 10s
stats realm Haproxy\ Statistics
stats auth test:123456 这个账号是登录haproxy网页的账号
在数据库的机器上编辑(两台机器都是加):
vi /usr/bin/clustercheck (这个要有执行权限)
MYSQL_USERNAME=haproxy_root 之前设置的数据库用户名 (这个要能从远程连接过来)
MYSQL_PASSWORD=123456
如果没有从别的地拷贝过来具体方法在网上搜下(同时还要有mysqlchk这个文件。这两个文件都要有执行权限)
vi /etc/services 最后加上:
mysqlchk 9200/tcp # mysqlchk
启动 xinetd (如果没有安装apt-get install xinetd)
service xinetd restart
启动haproxy
services haproxy start
或
haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy.pid -d -V (这个为调试启动,有错误会打印出来)
percona clust 用 Haproxy做负载均衡:
一直报:
[WARNING] 020/135012 (2536) : Server mysql-cluster/db01.com is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 9ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[WARNING] 020/135012 (2536) : Server mysql-cluster/db02.com is DOWN, reason: Layer7 wrong status, code: 503, info: "Service Unavailable", check duration: 9ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[CST下午1时58分14秒] Hans Wang: [ALERT] 020/135018 (2536) : proxy ‘mysql-cluster‘ has no server available!
解决方法:
如果机器上没安装 http服务,就把 mysql-cluster 里的option httpchk 注释或删除.
打开option httpchk 这个机器上一定要开80端口
用Haproxy负载percona XtraDB Cluster
原文地址:http://hans925.blog.51cto.com/542170/1607169