OneProxy实现MySQL读写分离与负载均衡
Part1:寫在最前
????OneProxy平民軟件完全自主開發(fā)的分布式數(shù)據(jù)訪問層,幫助用戶在MySQL/PostgreSQL集群上快速搭建支持分庫分表的分布式數(shù)據(jù)庫中間件,也是一款具有SQL白名單(防SQL注入)及IP白名單功能的SQL防火墻軟件。采用與MySQL Proxy一致的反向協(xié)議輸出模式,對應(yīng)用非常簡單和透明易用,讓用戶畏懼的數(shù)據(jù)庫故障切換(Failover)、讀寫分離(Read/Write Split)、分庫分表(Horizontal Partitioning)等復(fù)雜方案變得極其簡單可控!基于Libevent機(jī)制實現(xiàn),單個實例可以實現(xiàn)25萬的SQL轉(zhuǎn)發(fā)能力,用一個OneProxy節(jié)點(diǎn)可以帶動整個MySQL集群,為業(yè)務(wù)發(fā)展貢獻(xiàn)一份力量。
Part2:環(huán)境簡介
HE1:192.168.1.248 slave1
HE2:192.168.1.249 slave2
HE3:192.168.1.250 Master
HE4:192.168.1.251 Oneproxy
環(huán)境構(gòu)建
Part1:安裝Oneproxy
| 1 2 3 | [root@HE4?~]#?tar?xvf?oneproxy-rhel5-linux64-v5.8.5-ga.tar.gz [root@HE4?oneproxy]#?ls bin??conf??demo.sh??log??oneproxy.service??README??testadmin.sql??testautocommit.sql??testproxy.sql??trantest.sql |
Part2:構(gòu)建主從環(huán)境
本文的架構(gòu)是一主兩從,HE3作為Master,HE1,HE2作為Slave。主從的構(gòu)建不是本文的重點(diǎn),需要的可移步:
1分鐘利用mysqlreplicate快速搭建MySQL主從
http://suifu.blog.51cto.com/9167728/1878443
Part3:配置Oneproxy
目錄中的demo是初次啟動腳本,oneproxy.service是啟停腳本,在新版的oneproxy中,conf文件夾的proxy.cnf為配置文件
| 1 2 3 4 5 6 | [root@HE4?oneproxy]#?cat?demo.sh? #/bin/bash # export?ONEPROXY_HOME=/root/oneproxy #?valgrind?--leak-check=full?\ ??${ONEPROXY_HOME}/bin/oneproxy?--defaults-file=${ONEPROXY_HOME}/conf/proxy.conf |
我們將demo.sh中的ONEPROXY_HOME變更為解壓oneproxy時所在的目錄
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | [root@HE4?oneproxy]#?cat?oneproxy.service? #!/bin/bash #?chkconfig:?-?30?21 #?description:?OneProxy?service. #?Source?Function?Library .?/etc/init.d/functions #?OneProxy?Settings ONEPROXY_HOME=/root/oneproxy ONEPROXY_SBIN="${ONEPROXY_HOME}/bin/oneproxy" ONEPROXY_CONF="${ONEPROXY_HOME}/conf/proxy.conf" ONEPROXY_PID="${ONEPROXY_HOME}/log/oneproxy.pid" RETVAL=0 prog="OneProxy" start()?{ ????????echo?-n?$"Starting?$prog?...?" ????????daemon?$ONEPROXY_SBIN?--defaults-file=$ONEPROXY_CONF RETVAL=$? echo } stop()?{ ????????echo?-n?$"Stopping?$prog?...?" if?[?-e?${ONEPROXY_PID}?];?then daemon?kill?-INT?$(cat?${ONEPROXY_PID}) ???????RETVAL=$? fi echo } restart(){ ????????stop sleep?1 ????????start } case?"$1"?in ??start) ????????start ????????;; ??stop) ????????stop ????????;; ??restart) ????????restart ????????;; ??*) ????????echo?$"Usage:?$0?{start|stop|restart}" ????????RETVAL=1 esac exit?$RETVAL |
同時,將oneproxy.service中的ONEPROXY_HOME也改為解壓時的目錄
進(jìn)入oneproxy中的bin目錄,使用mysqlpwd對密碼進(jìn)行加密
[root@HE4 oneproxy]# cd bin/
[root@HE4 bin]# ls
mysqlpwd ?oneproxy
[root@HE4 bin]# ./mysqlpwd MANAGER
1C6D087BA5D2607A27DECB2F2AFE247E911E877A
編輯proxy.cnf中的內(nèi)容
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@HE4?conf]#?cat?proxy.conf? [oneproxy] keepalive?????=?1 event-threads?=?4 proxy-group-policy.2?=?test:read-slave log-file??????=?log/oneproxy.log pid-file??????=?log/oneproxy.pid lck-file??????=?log/oneproxy.lck proxy-auto-readonly?=?1 proxy-forward-clientip?=?1 proxy-trans-debug?=?1 proxy-address????????????=?:3307 mysql-version?=?5.7.16 proxy-master-addresses.1?=?192.168.1.250:3306@test proxy-slave-addresses.2?=?192.168.1.248:3306@test proxy-slave-addresses.3?=?192.168.1.249:3306@test proxy-user-list??????????=?sys_admin/1C6D087BA5D2607A27DECB2F2AFE247E911E877A@test proxy-user-group?????????=?test:sys_admin/1C6D087BA5D2607A27DECB2F2AFE247E911E877A@test proxy-part-template??????=?conf/template.txt proxy-part-tables.1??????=?conf/part.txt proxy-part-tables.2??????=?conf/part2.txt proxy-part-tables.3??????=?conf/cust1.txt proxy-charset????????????=?utf8_bin proxy-secure-client?=?127.0.0.1 #?proxy-license??=?32C54560E06EFF3E proxy-httpserver?=?:8080 proxy-httptitle?=?OneProxy?Monitor |
Part4:啟動Oneproxy
[root@HE4 oneproxy]# ./demo.sh
[root@HE4 oneproxy]# ./oneproxy.service restart
Stopping OneProxy ... ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]
Starting OneProxy ... ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]
測試
Part1:Oneproxy狀態(tài)校驗
瀏覽器打開192.168.1.251:8080端口能夠看到oneproxy的管理頁面
這里可以看到主從的各種狀態(tài)信息。
Part2:負(fù)載均衡與讀寫分離校驗
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | [root@HE1?~]#?mysql?-usys_admin?-pMANAGER?-h192.168.1.251?-P3307?-e"select?@@hostname;" mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. +------------+ |?@@hostname?| +------------+ |?HE1????????| +------------+ [root@HE1?~]#?mysql?-usys_admin?-pMANAGER?-h192.168.1.251?-P3307?-e"select?@@hostname;" mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. +------------+ |?@@hostname?| +------------+ |?HE2????????| +------------+ [root@HE1?~]#?mysql?-usys_admin?-pMANAGER?-h192.168.1.251?-P3307?-e"select?@@hostname;" mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. +------------+ |?@@hostname?| +------------+ |?HE1????????| +------------+ [root@HE1?~]#?mysql?-usys_admin?-pMANAGER?-h192.168.1.251?-P3307?-e"select?@@hostname;" mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. +------------+ |?@@hostname?| +------------+ |?HE2????????| +------------+ [root@HE1?~]#?mysql?-usys_admin?-pMANAGER?-h192.168.1.251?-P3307?-e"select?@@hostname;" mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. +------------+ |?@@hostname?| +------------+ |?HE1????????| +------------+ [root@HE1?~]#?mysql?-usys_admin?-pMANAGER?-h192.168.1.251?-P3307?-e"begin;select?@@hostname;commit;" mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. +------------+ |?@@hostname?| +------------+ |?HE3????????| +------------+ [root@HE1?~]#?mysql?-usys_admin?-pMANAGER?-h192.168.1.251?-P3307?-e"begin;select?@@hostname;commit;" mysql:?[Warning]?Using?a?password?on?the?command?line?interface?can?be?insecure. +------------+ |?@@hostname?| +------------+ |?HE3????????| +------------+ |
可以看到HE1,HE2兩個slave作為負(fù)載均衡沒有問題,HE3作為寫服務(wù)器也沒有問題。
——總結(jié)——
OneProxy配合MySQL實現(xiàn)讀寫分離與負(fù)載均衡實驗構(gòu)建成功,Oneproxy還具有分庫分表功能,今后會進(jìn)一步研究。由于筆者的水平有限,編寫時間也很倉促,文中難免會出現(xiàn)一些錯誤或者不準(zhǔn)確的地方,不妥之處懇請讀者批評指正。
?本文轉(zhuǎn)自 dbapower 51CTO博客,原文鏈接:http://blog.51cto.com/suifu/1884673 ,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的OneProxy实现MySQL读写分离与负载均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 6. ZooKeeper访问控制列表
- 下一篇: zabbix-server 的安装-ce