工做拓撲:mysql
MySQL Proxy有一項強大功能是實現「讀寫分離」,基本原理是讓主數據庫處理寫方面事務,讓從庫處理SELECT查詢。linux
Amoeba for MySQL是一款優秀的中間件軟件,一樣能夠實現讀寫分離,負載均衡等功能,而且穩定性也高於MySQL Proxy,有興趣的能夠測試一下。sql
環境描述:數據庫
操做系統:CentOS6.3_x64後端
主服務器Master:192.168.0.202bash
從服務器Slave:192.168.0.203服務器
調度服務器MySQL-Proxy:192.168.0.204負載均衡
1、mysql主從複製
tcp
這裏就省略了,請參考http://going.blog.51cto.com/7876557/1290431ide
2、mysql-proxy實現讀寫分離
一、安裝mysql-proxy
實現讀寫分離是有lua腳本實現的,如今mysql-proxy裏面已經集成,無需再安裝
下載:http://dev.mysql.com/downloads/mysql-proxy/
tar zxvf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gzmv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy
二、配置mysql-proxy,建立主配置文件
cd /usr/local/mysql-proxymkdir lua #建立腳本存放目錄mkdir logs #建立日誌目錄cp share/doc/mysql-proxy/rw-splitting.lua ./lua #複製讀寫分離配置文件cp share/doc/mysql-proxy/admin-sql.lua ./lua #複製管理腳本vi /etc/mysql-proxy.cnf #建立配置文件[mysql-proxy]user=root #運行mysql-proxy用戶admin-username=proxy #主從mysql共有的用戶admin-password=123.com #用戶的密碼proxy-address=192.168.0.204:4000 #mysql-proxy運行ip和端口,不加端口,默認4040proxy-read-only-backend-addresses=192.168.0.203 #指定後端從slave讀取數據proxy-backend-addresses=192.168.0.202 #指定後端主master寫入數據proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua #指定讀寫分離配置文件位置admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua #指定管理腳本log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log #日誌位置log-level=info #定義log日誌級別,由高到低分別有(error|warning|info|message|debug)daemon=true #以守護進程方式運行keepalive=true #mysql-proxy崩潰時,嘗試重啓保存退出!chmod 660 /etc/mysql-porxy.cnf
三、修改讀寫分離配置文件
vi /usr/local/mysql-proxy/lua/rw-splitting.luaif not proxy.global.config.rwsplit then proxy.global.config.rwsplit = { min_idle_connections = 1, #默認超過4個鏈接數時,纔開始讀寫分離,改成1 max_idle_connections = 1, #默認8,改成1 is_debug = false }end
四、啓動mysql-proxy
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnfnetstat -tupln | grep 4000 #已經啓動tcp 0 0 192.168.0.204:4000 0.0.0.0:* LISTEN 1264/mysql-proxy 關閉mysql-proxy使用:killall -9 mysql-proxy
五、測試讀寫分離
1>.在主服務器建立proxy用戶用於mysql-proxy使用,從服務器也會同步這個操做
mysql> grant all on *.* to 'proxy'@'192.168.0.204' identified by '123.com';
2>.使用客戶端鏈接mysql-proxy
mysql -u proxy -h 192.168.0.204 -P 4000 -p123.com
建立數據庫和表,這時的數據只寫入主mysql,而後再同步從slave,能夠先把slave的關了,看能不能寫入,這裏我就不測試了,下面測試下讀的數據!
mysql> create table user (number INT(10),name VARCHAR(255));mysql> insert into test values(01,'zhangsan');mysql> insert into user values(02,'lisi');
3>.登錄主從mysq查看新寫入的數據以下,
mysql> use test;Database changed mysql> select * from user;+--------+----------+| number | name |+--------+----------+| 1 | zhangsan || 2 | lisi |+--------+----------+
4>.再登錄到mysql-proxy,查詢數據,看出能正常查詢
mysql -u proxy -h 192.168.0.204 -P 4000 -p123.com mysql> use test;mysql> select * from user;+--------+----------+| number | name |+--------+----------+| 1 | zhangsan || 2 | lisi |+--------+----------+
5>.登錄從服務器關閉mysql同步進程,這時再登錄mysql-proxy確定會查詢不出數據
slave stop;
6>.登錄mysql-proxy查詢數據,下面看來,能看到表,查詢不出數據
mysql> use test;Database changed mysql> show tables;+----------------+| Tables_in_test |+----------------+| user |+----------------+ mysql> select * from user;ERROR 1146 (42S02): Table 'test.user' doesn't exist
配置成功!真正實現了讀寫分離的效果!