Haproxy代理Mysql服务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haproxy代理Mysql服务相关的知识,希望对你有一定的参考价值。
-----client---------haproxy-------mysql1---------mysql2------
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20
一、安装mysql
[[email protected] ~]#tar -zxvf bison-2.5.tar.gz
[[email protected] ~]#./configure && make&& make install
[[email protected] ~]#tar -zxvf cmake-2.8.7.tar.gz
[[email protected] ~]#./bootstrap && gmake && gmake install
[[email protected] ~]#tar -zxvf mysql-5.5.22.tar.gz
[[email protected] ~]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -
DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 -
DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all
[[email protected] ~]#make && make install
初始化mysql:
[[email protected] ~]# cp support-files/my-medium.cnf /etc/my.cnf
[[email protected] ~]# cd scripts
[[email protected] ~]# ./mysql_install_db --basedir=/usr/local/mysql/ --
datadir=/usr/local/mysql/data/ --user=mysql
[[email protected] ~]# cp ../support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[[email protected] ~]# mysqladmin -u root password ‘redhat‘
登录测试:
[[email protected] ~]# mysql -u root -p
创建数据库:
[[email protected] ~]# mysql> createdata server1;
当然你也可以使用yum的方式安装mysql
[[email protected] ~]# yum -y install mysql mysql-devel mysql-server
二、安装haproxy
1、安装
[[email protected] ~]# yum -y install pcre-devel zlib-devel
[[email protected] ~]# tar -zxvf haproxy-1.4.24.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/haproxy-1.4.24/
[[email protected] ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy
注意:linux26 是指linux 的内核版本号。
[[email protected] ~]# make install PREFIX=/usr/local/haproxy
2、配置haproxy
[[email protected] ~]# mkdir /etc/haproxy
[[email protected] ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
修改:
global
log 127.0.0.1 local0 //配置日志记录,local0 为日志设备,默认存放到系统日志
log 127.0.0.1 local1 notice //notice 为日志级别,通常有7 个级别
#log loghost local0 info
maxconn 4096 //默认最大连接数,需考虑ulimit-n 限制 :可增加ulimitn
819200 #ulimit 的数量限制
chroot /usr/share/haproxy //运行路径
uid 99
gid 99
#debug
#quiet
defaults
log global //定义日志为global 中的日志
mode tcp //模式为四层
option tcplog //采用http 的日志格式
option dontlognull //不记录健康检查日志信息
retries 3 //三次连接失败就认为是服务器不可用,也可以通过后面设置
#redispatch
maxconn 2000 //最大连接数
contimeout 5000 //连接超时时间
clitimeout 50000 //客户端超时时间
srvtimeout 50000 //服务端超时时间
listen stats
mode http
bind :6677
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE
listen mysqlcluster 0.0.0.0:3306
balance roundrobin
server m1 192.168.56.202:3306 check port 3306 maxconn 300
server m2 192.168.56.203:3306 check port 3306 maxconn 300
注意:
如果启动时出现报错:/haproxy.main()] Cannot chroot(/usr/share/haproxy)
则手动创建:
[[email protected] ~]# mkdir /usr/share/haproxy
如果启动时出现报错:Starting proxy cacti: cannot bind socket
则执行:
[[email protected] ~]# sysctl -e net.ipv4.ip_nonlocal_bind=1
3、启动haproxy
[[email protected] ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/ //注意软链接的目录
[[email protected] ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init
/etc/init.d/haproxy
[[email protected] ~]# chmod +x /etc/init.d/haproxy
[[email protected] ~]# /etc/init.d/haproxy start
[[email protected] ~]# /etc/init.d/haproxy status
[[email protected] ~]# netstat -anp | grep haproxy //占用的也是TCP 的80 端口
[[email protected] ~]# chkconfig --add haproxy
[[email protected] ~]# chkconfig haproxy on
http://192.168.56.200:6677/haproxyadmin?stats 查看集群的状态
四、MySql 授权用户登录(集群内的mysql 都要授权)
mysql> GRANT ALL ON *.* TO ‘root‘@‘192.168.56.%‘ IDENTIFIED BY ‘redhat‘;
mysql> flush privileges;
五、测试:
[[email protected] ~]# mysql -u root -h 192.168.56.200 -p
登录两次分别查看
可增加keepalived
本文出自 “11000174” 博客,请务必保留此出处http://11010174.blog.51cto.com/11000174/1890829
以上是关于Haproxy代理Mysql服务的主要内容,如果未能解决你的问题,请参考以下文章