一、MySQL-Proxy基礎(chǔ)
MySQL Proxy是一個(gè)處于你的Client端和MySQL server端之間的簡(jiǎn)單程序,它可以監(jiān)測(cè)、分析或改變它們的通信。它使用靈活,沒(méi)有限制,常見(jiàn)的用途包括:負(fù)載平衡,故障、查詢分析,查詢過(guò)濾和修改等等。

(Figure1:MySQL Proxy)
MySQL-Proxy, announced in June, is a binary application that sits between your MySQL client and server, and supports the embedded scripting language Lua. The proxy can be used to analyze, monitor and transform communication, and supports a wide range of scenarios including:
load balancing and fail over handling query analysis and logging SQL macros query rewriting executing shell commandsOne of the more powerful features of MySQL Proxy is the ability to do "Read/Write Splitting". The basic concept is to have a master database handle transactional queries while slaves handle SELECT queries. Replication is used to synchronize the changes due to transactional queries with the slaves in the cluster.
MySQL-Proxy是處在你的MySQL數(shù)據(jù)庫(kù)客戶和服務(wù)端之間的程序,它還支持嵌入性腳本語(yǔ)言Lua。這個(gè)代理可以用來(lái)分析、監(jiān)控和變換(transform)通信數(shù)據(jù),它支持非常廣泛的使用場(chǎng)景:
負(fù)載平衡和故障轉(zhuǎn)移處理 查詢分析和日志 SQL宏(SQL macros) 查詢重寫(xiě)(query rewriting) 執(zhí)行shell命令MySQL Proxy更強(qiáng)大的一項(xiàng)功能是實(shí)現(xiàn)“讀寫(xiě)分離(Read/Write Splitting)”。基本的原理是讓主數(shù)據(jù)庫(kù)處理事務(wù)性查詢,而從數(shù)據(jù)庫(kù)處理SELECT查詢。數(shù)據(jù)庫(kù)復(fù)制被用來(lái)把事務(wù)性查詢導(dǎo)致的變更同步到集群中的從數(shù)據(jù)庫(kù)。
二、實(shí)戰(zhàn)過(guò)程
測(cè)試環(huán)境:Ubuntu 10.04.2 LTS + MySQL5.1.41-3ubuntu12.10-log
192.168.1.147 proxy 代理 入口
192.168.1.126 master 主機(jī) 只寫(xiě)
192.168.1.145 slaver 從機(jī) 只讀
程序上只需要鏈接到192.168.1.147,而192.168.1.126和192.168.1.145對(duì)于程序來(lái)說(shuō)是透明的,你完全不需要理會(huì),也不需要知道192.168.1.126和192.168.1.145,你對(duì)數(shù)據(jù)庫(kù)的所有操作都只對(duì)192.168.1.147進(jìn)行操作。
1.安裝腳本lua
#apt-get install lua5.1
MySQL-Proxy的讀寫(xiě)分離主要是通過(guò)rw-splitting.lua腳本實(shí)現(xiàn)的,因此需要安裝lua。
2.安裝配置MySQL-Proxy
#apt-get mysql-proxy
當(dāng)前獲取到的版本是:mysql-proxy 0.8.0(查看版本命令:#mysql-proxy -V)
3.修改rw-splitting.lua
#vim /usr/share/mysql-proxy/rw-splitting.lua
配置并使用rw-splitting.lua讀寫(xiě)分離腳本,腳本目錄是 /usr/share/mysql-proxy,修改讀寫(xiě)分離腳本rw-splitting.lua,修改默認(rèn)連接數(shù),進(jìn)行快速測(cè)試,如果不修改連接數(shù)的話要達(dá)到連接數(shù)為4時(shí)才會(huì)啟用讀寫(xiě)分離。
-- connection pool
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, //默認(rèn)為4
max_idle_connections = 1, //默認(rèn)為8
is_debug = false
}
end
這是因?yàn)閙ysql-proxy會(huì)檢測(cè)客戶端連接,當(dāng)連接沒(méi)有超過(guò)min_idle_connections預(yù)設(shè)值時(shí), 不會(huì)進(jìn)行讀寫(xiě)分離, 即查詢操作會(huì)發(fā)生到Master上。
4.新建文件夾/var/log/mysql-proxy/和文件mysql-proxy.log
#mkdir /var/log/mysql-proxy
#vi mysql-proxy.log
5.執(zhí)行讀寫(xiě)分離
#sudo mysql-proxy --proxy-read-only-backend-addresses=192.168.1.145:3306 --proxy-backend-addresses=192.168.1.126:3306 --proxy-lua-script=/usr/share/mysql-proxy/rw-splitting.lua >/var/log/mysql-proxy/mysql-proxy.log &
參數(shù)說(shuō)明:
192.168.1.147 proxy 代理 入口
192.168.1.126 master 主機(jī) 只寫(xiě)
192.168.1.145 slaver 從機(jī) 只讀
當(dāng)運(yùn)行sudo mysql-proxy 上面語(yǔ)句后,查詢進(jìn)程沒(méi)有4040的時(shí)候,需要重啟mysql ( sudo /etc/init.d/mysql restart) 之后再輸入proxy設(shè)置。
6.查看進(jìn)程端口
#netstat -ant
#netstat 主站蜘蛛池模板: 嘉义县| 焦作市| 白水县| 河津市| 孟津县| 宜良县| 黎川县| 会宁县| 新丰县| 班戈县| 巨鹿县| 乳山市| 青川县| 阿拉善盟| 平舆县| 阳泉市| 多伦县| 湘阴县| 衡东县| 泰安市| 洛阳市| 北川| 桑日县| 桦南县| 西乡县| 浙江省| 疏勒县| 昆山市| 永善县| 榆社县| 张家口市| 镇远县| 山阳县| 东乡族自治县| 石泉县| 尉氏县| 大姚县| 五家渠市| 德昌县| 棋牌| 余干县|