Haproxy代理Mysql服務

來源:互聯網
上載者:User

標籤: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]st ~]# 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服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.