MySQL中haproxy的搭建#Olivia丶長歌#
阿新 • • 發佈:2019-02-11
***********************************搭建haproxy***********************************
1,新增haproxy使用者
useradd haproxy
2,在MARIADB GALERA CLUSTER叢集中新增登入使用者
mysql> grant all on *.* to [email protected]'%' identified by '123456’;
重新整理許可權
mysql> flush privileges;
3,上傳haproxy的軟體和配置檔案到/soft
ls
haproxy-1.4.20.tar.gz haproxy.cfg
4,解壓和建立資料目錄、日誌目錄
tar -zxvf haproxy-1.4.20.tar.gz
cd /usr/local/haproxy/
ls
mkdir conf
mkdir logs
cd /soft
cp -rf haproxy.cfg /usr/local/haproxy/conf/
4,編譯安裝
cd haproxy-1.4.20
make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=X86_64
make install PREFIX=/usr/local/haproxy
5,修改使用者
chown -R haproxy.haproxy /usr/local/haproxy
6,修改配置檔案(************星號是要修改的位置**************)
cd /usr/local/haproxy
vim haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
maxconn 4096
log 127.0.0.1 local1 notice
#log loghost local0 info
#maxconn 4096
chroot /usr/local/haproxy
uid 501
gid 501
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
#debug
#quiet
defaults
log global
#option dontlognull
retries 3
option redispatch
maxconn 4096
timeout http-keep-alive 10s
timeout check 10s
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen admin_stats 192.168.100.12:48800 ******在哪個主機上搭的haproxy寫哪個主機的IP***頁面訪問的埠號******
stats enable
stats hide-version
stats realm <realm>
stats refresh 5s
stats uri /haproxy-status *********網頁登入的字尾地址************
stats auth admin:admin *******網頁登入的賬戶和密碼************
stats admin if TRUE
mode http
option httplog
timeout connect 50000
timeout check 50000
timeout client 300000
timeout server 300000
listen mysql_proxy 192.168.100.12:3310 *********haproxy的IP和埠號********************
mode tcp
balance roundrobin
option tcpka
option tcplog
server pxc01 192.168.100.12:3306 weight 1 check inter 2000 rise 2 fall 5 *********主伺服器*********
server pxc02 192.168.100.13:3307 weight 1 check inter 2000 rise 2 fall 5 *********從伺服器1*********
server pxc03 192.168.100.14:3308 weight 1 check inter 2000 rise 2 fall 5 *********從伺服器2*********
7,啟動haproxy
檢視haproxy的命令
/usr/local/haproxy/sbin/haproxy ?f /usr/local/haproxy/conf/haproxy.cfg
啟動
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
8,檢視haproxy程序
ps -ef|grep ha
檢視haproxy
netstat -nltp|grep 3310
測試haproxy+pxc負載均衡方案
/usr/local/mysql/bin/mysql -h 192.168.100.12 -P 3306 -proot -e "select
@@hostname";
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| oracle |
+------------+
/usr/local/mysql/bin/mysql -h 192.168.100.13 -P 3308 -proot -e "select
@@hostname";
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql |
+------------+
此時haproxy可以輪訓訪問三個節點
1,新增haproxy使用者
useradd haproxy
2,在MARIADB GALERA CLUSTER叢集中新增登入使用者
mysql> grant all on *.* to [email protected]'%' identified by '123456’;
重新整理許可權
mysql> flush privileges;
3,上傳haproxy的軟體和配置檔案到/soft
ls
haproxy-1.4.20.tar.gz haproxy.cfg
4,解壓和建立資料目錄、日誌目錄
tar -zxvf haproxy-1.4.20.tar.gz
cd /usr/local/haproxy/
ls
mkdir conf
mkdir logs
cd /soft
cp -rf haproxy.cfg /usr/local/haproxy/conf/
4,編譯安裝
cd haproxy-1.4.20
make TARGET=linux26 PREFIX=/usr/local/haproxy ARCH=X86_64
make install PREFIX=/usr/local/haproxy
5,修改使用者
chown -R haproxy.haproxy /usr/local/haproxy
6,修改配置檔案(************星號是要修改的位置**************)
cd /usr/local/haproxy
vim haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
log 127.0.0.1 local0
maxconn 4096
log 127.0.0.1 local1 notice
#log loghost local0 info
#maxconn 4096
chroot /usr/local/haproxy
uid 501
gid 501
daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid
#debug
#quiet
defaults
log global
#option dontlognull
retries 3
option redispatch
maxconn 4096
timeout http-keep-alive 10s
timeout check 10s
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen admin_stats 192.168.100.12:48800 ******在哪個主機上搭的haproxy寫哪個主機的IP***頁面訪問的埠號******
stats enable
stats hide-version
stats realm <realm>
stats refresh 5s
stats uri /haproxy-status *********網頁登入的字尾地址************
stats auth admin:admin *******網頁登入的賬戶和密碼************
stats admin if TRUE
mode http
option httplog
timeout connect 50000
timeout check 50000
timeout client 300000
timeout server 300000
listen mysql_proxy 192.168.100.12:3310 *********haproxy的IP和埠號********************
mode tcp
balance roundrobin
option tcpka
option tcplog
server pxc01 192.168.100.12:3306 weight 1 check inter 2000 rise 2 fall 5 *********主伺服器*********
server pxc02 192.168.100.13:3307 weight 1 check inter 2000 rise 2 fall 5 *********從伺服器1*********
server pxc03 192.168.100.14:3308 weight 1 check inter 2000 rise 2 fall 5 *********從伺服器2*********
7,啟動haproxy
檢視haproxy的命令
/usr/local/haproxy/sbin/haproxy ?f /usr/local/haproxy/conf/haproxy.cfg
啟動
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
8,檢視haproxy程序
ps -ef|grep ha
檢視haproxy
netstat -nltp|grep 3310
測試haproxy+pxc負載均衡方案
/usr/local/mysql/bin/mysql -h 192.168.100.12 -P 3306 -proot -e "select
@@hostname";
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| oracle |
+------------+
/usr/local/mysql/bin/mysql -h 192.168.100.13 -P 3308 -proot -e "select
@@hostname";
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| mysql |
+------------+
此時haproxy可以輪訓訪問三個節點