一、MySQL-Proxy基礎
MySQL Proxy是一個處于你的Client端和MySQL server端之間的簡單程序,它可以監測、分析或改變它們的通信。它使用靈活,沒有限制,常見的用途包括:負載平衡,故障、查詢分析,查詢過濾和修改等等。

(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數據庫客戶和服務端之間的程序,它還支持嵌入性腳本語言Lua。這個代理可以用來分析、監控和變換(transform)通信數據,它支持非常廣泛的使用場景:
負載平衡和故障轉移處理 查詢分析和日志 SQL宏(SQL macros) 查詢重寫(query rewriting) 執行shell命令MySQL Proxy更強大的一項功能是實現“讀寫分離(Read/Write Splitting)”?;镜脑硎亲屩鲾祿焯幚硎聞招圆樵儯鴱臄祿焯幚鞸ELECT查詢。數據庫復制被用來把事務性查詢導致的變更同步到集群中的從數據庫。
二、實戰過程
測試環境:Ubuntu 10.04.2 LTS + MySQL5.1.41-3ubuntu12.10-log
192.168.1.147 proxy 代理 入口
192.168.1.126 master 主機 只寫
192.168.1.145 slaver 從機 只讀
程序上只需要鏈接到192.168.1.147,而192.168.1.126和192.168.1.145對于程序來說是透明的,你完全不需要理會,也不需要知道192.168.1.126和192.168.1.145,你對數據庫的所有操作都只對192.168.1.147進行操作。
1.安裝腳本lua
#apt-get install lua5.1
MySQL-Proxy的讀寫分離主要是通過rw-splitting.lua腳本實現的,因此需要安裝lua。
2.安裝配置MySQL-Proxy
#apt-get mysql-proxy
當前獲取到的版本是:mysql-proxy 0.8.0(查看版本命令:#mysql-proxy -V)
3.修改rw-splitting.lua
#vim /usr/share/mysql-proxy/rw-splitting.lua
配置并使用rw-splitting.lua讀寫分離腳本,腳本目錄是 /usr/share/mysql-proxy,修改讀寫分離腳本rw-splitting.lua,修改默認連接數,進行快速測試,如果不修改連接數的話要達到連接數為4時才會啟用讀寫分離。
新聞熱點
疑難解答