一直想等到BETA版出來再試驗的,可仍是經不住誘惑阿,下午終於有時間測試一下了。
(本文參考地址:
[url]http://blog.chinaunix.net/u/8111/showart.php?id=451420[/url])
1、必備軟件:
一、LUA
能夠去LUA的官方下載:[url]www.lua.org[/url]
二、MySQL Proxy
這裏有好多二進制版本。
[url]http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/[/url]
或者去MYSQL官方下載源代碼。
三、測試過程當中取消了B和C的REPLICATION。這樣SQL語句一會兒就看出來從哪裏來的。
若是是M-S(能夠先在SLAVE上進行STOP SLAVE)
2、測試主機地址:
一、MySQL Proxy 安裝地址:192.168.0.234(簡稱A)
二、MySQL 服務器地址:192.168.0.235(簡稱B)/236(簡稱C)
3、安裝體驗
若是是按照二進制包安裝的,跳過這一步。
一、 LUA的安裝
[root
@localhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local
[root
@localhost ~]# cd /usr/local/
[root
@localhost local]# mv lua-5.1 lua
[root
@localhost lua]# cd lua
[root
@localhost lua]#make local;make install;
導出環境變量:
[root
@localhost lua]#export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm"
二、MySQL Proxy 安裝
[root
@localhost ~]#tar -zxvf mysql-proxy-0.6.1-linux-rhel4-x86-32bit.tar.gz -C /usr/local/mysql/
[root
@localhost ~]#cd /usr/local/mysql
[root
@localhost mysql]#mv mysql-proxy-0.6.1-linux-rhel4-x86-32bit/ mysql-proxy
[root
@localhost sbin]# export PATH=$PATH:/usr/local/mysql/mysql-proxy/sbin/
4、使用MySQL Proxy
一、查看幫助選項
[root
@localhost ~]# mysql-proxy --help-all
二、對MySQL 操做
MySQL服務器假設已經安裝。(安裝步驟這裏就不寫了)
兩臺機器上的表初始結構和數據都是同樣的,並且都有t_girl_user這個用戶。
mysql> desc t;
+-------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| c_str | char(64) | NO | | | |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
2 rows in set (0.00 sec)
我在B上插入一條記錄
mysql> insert into t(c_str) values('B');
Query OK, 1 row affected (0.00 sec)
在C上一樣插入一條記錄
mysql> insert into t(c_str) value('C');
Query OK, 1 row affected (0.00 sec)
mysql>
三、啓動MySQL-Proxy(測試讀寫分離)
[root
@localhost sbin]# mysql-proxy --proxy-read-only-backend-addresses=192.168.0.236:3306 --proxy-backend-addresses=192.168.0.235:3306 --proxy-lua-script=/usr/local/mysql/mysql-proxy/share/mysql-proxy/rw-splitting.lua &
[1] 32554
讓MYSQL PROXY自動啓動的簡單腳本
#!/bin/sh
# export PATH=$PATH:/usr/local/mysql-proxy
cd /usr/local/mysql-proxy
./mysql-proxy --proxy-read-only-backend-addresses=192.168.0.236:3306 --proxy-backend-addresses=192.168.0.235:3306 --proxy-lua-script=rw-splitting.lua >> /tmp/log
~
這個例子中限制192.168.0.236爲只讀,192.168.0.235爲可寫。
四、下來咱們來看試驗結果。
咱們用幾臺客戶端開啓4個鏈接。
[root@localhost ~]# /usr/local/mysql/bin/mysql -ut_girl_user -p123456 -P4040 -h192.168.0.234 -Dt_girl
我這邊已經啓動了好幾個客戶端,這裏就不貼了,命令和上面的同樣。
寫數據。
mysql> insert into t(c_str) values ('wangwang');
Query OK, 1 row affected (0.01 sec)
mysql> show processlist;
+----+-------------+---------------------+--------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+---------------------+--------+---------+------+-------+------------------+
| 12 | t_girl_user | 192.168.0.234:44975 | t_girl | Sleep | 28 | | NULL |
| 13 | t_girl_user | 192.168.0.234:44976 | t_girl | Sleep | 15 | | NULL |
| 14 | t_girl_user | 192.168.0.234:44977 | t_girl | Sleep | 19 | | NULL |
| 15 | t_girl_user | 192.168.0.234:44978 | t_girl | Query | 0 | NULL | show processlist |
+----+-------------+---------------------+--------+---------+------+-------+------------------+
4 rows in set (0.00 sec)
4 rows in set (0.00 sec)
讀數據(如今仍是寫和讀都在B上)
mysql> select * from t;
+----+----------+
| id | c_str |
+----+----------+
| 1 | B |
| 2 | wangwang |
+----+----------+
2 rows in set (0.00 sec)
再增長一個客戶端鏈接。
1 row in set (0.00 sec)
mysql> show processlist;
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+
| 2 | system user | | NULL | Connect | 1842 | Has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 5 | root | localhost | t_girl | Query | 0 | NULL | show processlist |
| 12 | t_girl_user | 192.168.0.234:44975 | t_girl | Sleep | 446 | | NULL |
| 13 | t_girl_user | 192.168.0.234:44976 | t_girl | Sleep | 188 | | NULL |
| 14 | t_girl_user | 192.168.0.234:44977 | t_girl | Sleep | 206 | | NULL |
| 15 | t_girl_user | 192.168.0.234:44978 | t_girl | Sleep | 203 | | NULL |
| 16 | t_girl_user | 192.168.0.234:44979 | t_girl | Sleep | 164 | | NULL |
| 17 | t_girl_user | 192.168.0.234:44980 | t_girl | Sleep | 210 | | NULL |
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+
8 rows in set (0.00 sec)
如今咱們來讀數據。
mysql> select * from t;
+----+-------+
| id | c_str |
+----+-------+
| 1 | C |
+----+-------+
1 row in set (0.00 sec)
這個數據很明顯是來自C的。
再插入一條記錄
mysql> insert into t(c_str) values ('wangwei');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t;
+----+-------+
| id | c_str |
+----+-------+
| 1 | C |
+----+-------+
1 row in set (0.00 sec)
C上的數據沒有變。
仍是沒有數據。
如今跑到B上看看。
mysql> show processlist;
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+
| 2 | system user | | NULL | Connect | 1842 | Has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 5 | root | localhost | t_girl | Query | 0 | NULL | show processlist |
| 12 | t_girl_user | 192.168.0.234:44975 | t_girl | Sleep | 446 | | NULL |
| 13 | t_girl_user | 192.168.0.234:44976 | t_girl | Sleep | 188 | | NULL |
| 14 | t_girl_user | 192.168.0.234:44977 | t_girl | Sleep | 206 | | NULL |
| 15 | t_girl_user | 192.168.0.234:44978 | t_girl | Sleep | 203 | | NULL |
| 16 | t_girl_user | 192.168.0.234:44979 | t_girl | Sleep | 164 | | NULL |
| 17 | t_girl_user | 192.168.0.234:44980 | t_girl | Sleep | 210 | | NULL |
+----+-------------+---------------------+--------+---------+------+-----------------------------------------------------------------------+------------------+
8 rows in set (0.00 sec)
mysql> select * from t;
+----+----------+
| id | c_str |
+----+----------+
| 1 | B |
| 2 | wangwang |
| 3 | wangwei |
+----+----------+
3 rows in set (0.00 sec)
數據被成功插入到B
這個讀寫分離應該看得很清楚。其餘的功能等我測試了再總結吧。
若是要知道爲何鏈接多了纔會進行分離的話,看看
手冊
論壇裏面的討論
還有其餘我之前收藏的地址,能夠參考如下。
[url]http://www.infoq.com/news/2007/10/mysqlproxyrwsplitting;jsessionid=F9E0B91935E82033BAFAC067A326DC4B[/url]
[url]http://blogs.mysql.com/kaj/2007/12/10/combining-mysql-proxy-with-mysql-cluster/[/url]