在OneProxy的基礎(chǔ)上實(shí)行MySQL讀寫分離與負(fù)載均衡
簡介
Part1:寫在最前
OneProxy平民軟件完全自主開發(fā)的分布式數(shù)據(jù)訪問層,幫助用戶在MySQL/PostgreSQL集群上快速搭建支持分庫分表的分布式數(shù)據(jù)庫中間件,也是一款具有SQL白名單(防SQL注入)及IP白名單功能的SQL防火墻軟件。采用與MySQL Proxy一致的反向協(xié)議輸出模式,對(duì)應(yīng)用非常簡單和透明易用,讓用戶畏懼的數(shù)據(jù)庫故障切換(Failover)、讀寫分離(Read/Write Split)、分庫分表(Horizontal Partitioning)等復(fù)雜方案變得極其簡單可控!基于Libevent機(jī)制實(shí)現(xiàn),單個(gè)實(shí)例可以實(shí)現(xiàn)25萬的SQL轉(zhuǎn)發(fā)能力,用一個(gè)OneProxy節(jié)點(diǎn)可以帶動(dòng)整個(gè)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
[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),需要的可移步:
Part3:配置Oneproxy
目錄中的demo是初次啟動(dòng)腳本,oneproxy.service是啟停腳本,在新版的oneproxy中,conf文件夾的proxy.cnf為配置文件
[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時(shí)所在的目錄
[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
同時(shí),將oneproxy.service中的ONEPROXY_HOME也改為解壓時(shí)的目錄
進(jìn)入oneproxy中的bin目錄,使用mysqlpwd對(duì)密碼進(jìn)行加密
[root@HE4 oneproxy]# cd bin/ [root@HE4 bin]# ls mysqlpwd oneproxy [root@HE4 bin]# ./mysqlpwd MANAGER 1C6D087BA5D2607A27DECB2F2AFE247E911E877A
編輯proxy.cnf中的內(nèi)容
[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:啟動(dòng)Oneproxy
[root@HE4 oneproxy]# ./demo.sh [root@HE4 oneproxy]# ./oneproxy.service restart Stopping OneProxy ... [ OK ] Starting OneProxy ... [ OK ]
測試
Part1:Oneproxy狀態(tài)校驗(yàn)
瀏覽器打開192.168.1.251:8080端口能夠看到oneproxy的管理頁面
這里可以看到主從的各種狀態(tài)信息。
Part2:負(fù)載均衡與讀寫分離校驗(yàn)
[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兩個(gè)slave作為負(fù)載均衡沒有問題,HE3作為寫服務(wù)器也沒有問題。
——總結(jié)——
OneProxy配合MySQL實(shí)現(xiàn)讀寫分離與負(fù)載均衡實(shí)驗(yàn)構(gòu)建成功,Oneproxy還具有分庫分表功能,今后會(huì)進(jìn)一步研究。由于筆者的水平有限,編寫時(shí)間也很倉促,文中難免會(huì)出現(xiàn)一些錯(cuò)誤或者不準(zhǔn)確的地方,不妥之處懇請(qǐng)讀者批評(píng)指正。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Mysql雙機(jī)熱備和負(fù)載均衡的實(shí)現(xiàn)步驟
- 利用MySQL系統(tǒng)數(shù)據(jù)庫做性能負(fù)載診斷的方法
- MySQL如何實(shí)現(xiàn)負(fù)載均衡功能
- 如何使用nginx充當(dāng)mysql的負(fù)載均衡器
- 基于mysql+mycat搭建穩(wěn)定高可用集群負(fù)載均衡主備復(fù)制讀寫分離操作
- python實(shí)現(xiàn)mysql的讀寫分離及負(fù)載均衡
- Keepalived+HAProxy實(shí)現(xiàn)MySQL高可用負(fù)載均衡的配置
- 分析MySQL中索引引引發(fā)的CPU負(fù)載飆升的問題
- 快速增加MYSQL數(shù)據(jù)庫連接數(shù)負(fù)載能力的方法分享
- 具有負(fù)載均衡功能的MySQL服務(wù)器集群部署及實(shí)現(xiàn)
- mysql CPU高負(fù)載問題排查
相關(guān)文章
MySQL中你可能忽略的COLLATION實(shí)例詳解
mysql的collation大致的意思就是字符序,這篇文章主要給大家介紹了關(guān)于MySQL中COLLATION的相關(guān)資料,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05MySQL刪除表數(shù)據(jù)與MySQL清空表命令的3種方法淺析
刪除現(xiàn)有MySQL表非常容易,但是刪除任何現(xiàn)有的表時(shí)要非常小心,因?yàn)閯h除表后丟失的數(shù)據(jù)將無法恢復(fù),下面這篇文章主要給大家介紹了關(guān)于MySQL刪除表數(shù)據(jù)與MySQL清空表命令的3種方法的相關(guān)資料,需要的朋友可以參考下2022-08-08MySQL count(1)、count(*)、count(字段)的區(qū)別
COUNT在數(shù)據(jù)庫行數(shù)統(tǒng)計(jì)中被廣泛使用,那么你知道MySQL count(1)、count(*)、count(字段)的區(qū)別嗎,本文就想的介紹一下,感興趣的可以了解一下2021-12-12MySQL如何快速創(chuàng)建800w條測試數(shù)據(jù)表
這篇文章主要介紹了MySQL如何快速創(chuàng)建800w條測試數(shù)據(jù)表,下面文章圍繞MySQL創(chuàng)建測試數(shù)據(jù)表的相關(guān)資料展開詳細(xì)內(nèi)容,具有一的的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03