Haproxy代理Mysql服务

-----client---------haproxy-------mysql1---------mysql2------mysql

192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20linux

1、安装mysqlredis

[root@localhost ~]#tar -zxvf bison-2.5.tar.gzsql

[root@localhost ~]#./configure && make&& make install数据库

[root@localhost ~]#tar -zxvf cmake-2.8.7.tar.gzbootstrap

[root@localhost ~]#./bootstrap && gmake && gmake installvim

[root@localhost ~]#tar -zxvf mysql-5.5.22.tar.gz服务器

[root@localhost ~]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -socket

DSYSCONFDIR=/etc/ -DDEFAULT_CHARSET=utf8 -tcp

DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all

[root@localhost ~]#make && make install

初始化mysql:

[root@localhost ~]# cp support-files/my-medium.cnf /etc/my.cnf

[root@localhost ~]# cd scripts

[root@localhost ~]# ./mysql_install_db --basedir=/usr/local/mysql/ --

datadir=/usr/local/mysql/data/ --user=mysql

[root@localhost ~]# cp ../support-files/mysql.server /etc/init.d/mysqld

[root@localhost ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/

[root@localhost ~]# mysqladmin -u root password 'redhat'

登陆测试:

[root@localhost ~]# mysql -u root -p

建立数据库:

[root@localhost ~]# mysql> createdata server1;

固然你也可使用yum的方式安装mysql

[root@localhost ~]# yum -y install mysql mysql-devel mysql-server

2、安装haproxy

一、安装

[root@localhost ~]# yum -y install pcre-devel zlib-devel

[root@localhost ~]# tar -zxvf haproxy-1.4.24.tar.gz -C /usr/src/

[root@localhost ~]# cd /usr/src/haproxy-1.4.24/

[root@localhost ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy

注意:linux26 是指linux 的内核版本号。

[root@localhost ~]# make install PREFIX=/usr/local/haproxy

二、配置haproxy

[root@localhost ~]# mkdir /etc/haproxy

[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/

[root@localhost ~]# 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)

则手动建立:

[root@localhost ~]# mkdir /usr/share/haproxy

若是启动时出现报错:Starting proxy cacti: cannot bind socket

则执行:

[root@localhost ~]# sysctl -e net.ipv4.ip_nonlocal_bind=1

三、启动haproxy

[root@localhost ~]# ln -s /usr/local/haproxy/sbin/* /usr/sbin/ //注意软连接的目录

[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init

/etc/init.d/haproxy

[root@localhost ~]# chmod +x /etc/init.d/haproxy

[root@localhost ~]# /etc/init.d/haproxy start

[root@localhost ~]# /etc/init.d/haproxy status

[root@localhost ~]# netstat -anp | grep haproxy //占用的也是TCP 的80 端口

[root@localhost ~]# chkconfig --add haproxy

[root@localhost ~]# chkconfig haproxy on

http://192.168.56.200:6677/haproxyadmin?stats 查看集群的状态

4、MySql 受权用户登陆(集群内的mysql 都要受权)

mysql> GRANT ALL ON *.* TO 'root'@'192.168.56.%' IDENTIFIED BY 'redhat';

mysql> flush privileges;

5、测试:

[root@localhost ~]# mysql -u root -h 192.168.56.200 -p

登陆两次分别查看

可增长keepalived

相关文章
相关标签/搜索