1:現有環境
機器 | MySQL | 環境 |
---|---|---|
172.16.128.240 | MGR NODE1 | MGR |
172.16.128.241 | MGR NODE2 | MGR |
172.16.128.242 | MGR NODE3 | MGR |
172.16.128.239 | VIP | keepalived |
環境介紹,VIP 172.16.128.239在172.16.128.240上,我們是基於172.16.128.240和172.16.128.241做的keepalived。MGR為多主模式。我們考慮使用中介軟體能夠實現MGR的讀寫分離。中介軟體選型為Atlas,Atlas是360團隊開源的一套基於MySQL-Proxy基礎之上的代理,修改了MySQL-Proxy的一些BUG,並且進行了優化。
2:安裝Atlas
[root@YC-ss1 tmp]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm Preparing... ################################# [100%] Updating / installing... 1:Atlas-2.2.1-1 ################################# [100%] [root@YC-ss1 mysql-proxy]# pwd /usr/local/mysql-proxy [root@YC-ss1 mysql-proxy]# ls bin conf lib log
我們看一下,安裝完Atlas 以後會生成四個資料夾。首先我們要配置密碼的加密:
[root@YC-ss1 bin]# ./encrypt 123456 /iZxz+0GRoA=
我的root@%賬號密碼就是123456,比較簡單。我們先記錄這個加密字串,等會要在配置檔案中配置。配置配置檔案
[root@YC-ss1 conf]# cp test.cnf atlas.cnf [root@YC-ss1 conf]# cat atlas.cnf [mysql-proxy]
#帶#號的為非必需的配置專案
#管理介面的使用者名稱
admin-username = user
#管理介面的密碼
admin-password = pwd
#Atlas後端連線的MySQL主庫的IP和埠,可設定多項,用逗號分隔
proxy-backend-addresses = 172.16.128.239:3306
#Atlas後端連線的MySQL從庫的IP和埠,@後面的數字代表權重,用來作負載均衡,若省略則預設為1,可設定多項,用逗號分隔
proxy-read-only-backend-addresses = 172.16.128.240:3306,172.16.128.241:3306,172.16.128.242:3306
#使用者名稱與其對應的加密過的MySQL密碼,密碼使用PREFIX/bin目錄下的加密程式encrypt加密,下行的user1和user2為示例,將其替換為你的MySQL的使用者名稱和加密密碼!
pwds = root:/iZxz+0GRoA=
#設定Atlas的執行方式,設為true時為守護程式方式,設為false時為前臺方式,一般開發除錯時設為false,線上執行時設為true,true後面不能有空格。
daemon = true
#設定Atlas的執行方式,設為true時Atlas會啟動兩個程式,一個為monitor,一個為worker,monitor在worker意外退出後會自動將其重啟,設為false時只有worker,沒有monitor,一般開發除錯時設為false,線上執行時設為true,true後面不能有空格。
keepalive = true
#工作執行緒數,對Atlas的效能有很大影響,可根據情況適當設定
event-threads = 16
#日誌級別,分為message、warning、critical、error、debug五個級別
log-level = message
#日誌存放的路徑
log-path = /usr/local/mysql-proxy/log
#SQL日誌的開關,可設定為OFF、ON、REALTIME,OFF代表不記錄SQL日誌,ON代表記錄SQL日誌,REALTIME代表記錄SQL日誌且實時寫入磁碟,預設為OFF
#sql-log = OFF
#慢日誌輸出設定。當設定了該引數時,則日誌只輸出執行時間超過sql-log-slow(單位:ms)的日誌記錄。不設定該引數則輸出全部日誌。
#sql-log-slow = 10
#例項名稱,用於同一臺機器上多個Atlas例項間的區分
#instance = test
#Atlas監聽的工作介面IP和埠
proxy-address = 0.0.0.0:6002
#Atlas監聽的管理介面IP和埠
admin-address = 0.0.0.0:6003
#分表設定,此例中person為庫名,mt為表名,id為分表欄位,3為子表數量,可設定多項,以逗號分隔,若不分表則不需要設定該項
#tables = person.mt.id.3
#預設字符集,設定該項後客戶端不再需要執行SET NAMES語句
#charset = utf8
#允許連線Atlas的客戶端的IP,可以是精確IP,也可以是IP段,以逗號分隔,若不設定該項則允許所有IP連線,否則只允許列表中的IP連線
#client-ips = 127.0.0.1, 192.168.1
#Atlas前面掛接的LVS的物理網路卡的IP(注意不是虛IP),若有LVS且設定了client-ips則此項必須設定,否則可以不設定
#lvs-ips = 192.168.1.1
啟動Atlas
[root@YC-ss1 bin]# ./mysql-proxyd atlas start OK: MySQL-Proxy of atlas is started
OK,現在我們的Atlas已經啟動了。我們登入檢視一下資訊:
[root@YC-ss1 conf]# mysql -h127.0.0.1 -P6003 -uuser -ppwd mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 1 Server version: 5.0.99-agent-admin Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type `help;` or `h` for help. Type `c` to clear the current input statement. (user@127.0.0.1) [(none)]> SELECT * FROM help -> ; +----------------------------+---------------------------------------------------------+ | command | description | +----------------------------+---------------------------------------------------------+ | SELECT * FROM help | shows this help | | SELECT * FROM backends | lists the backends and their state | | SET OFFLINE $backend_id | offline backend server, $backend_id is backend_ndx`s id | | SET ONLINE $backend_id | online backend server, ... | | ADD MASTER $backend | example: "add master 127.0.0.1:3306", ... | | ADD SLAVE $backend | example: "add slave 127.0.0.1:3306", ... | | REMOVE BACKEND $backend_id | example: "remove backend 1", ... | | SELECT * FROM clients | lists the clients | | ADD CLIENT $client | example: "add client 192.168.1.2", ... | | REMOVE CLIENT $client | example: "remove client 192.168.1.2", ... | | SELECT * FROM pwds | lists the pwds | | ADD PWD $pwd | example: "add pwd user:raw_password", ... | | ADD ENPWD $pwd | example: "add enpwd user:encrypted_password", ... | | REMOVE PWD $pwd | example: "remove pwd user", ... | | SAVE CONFIG | save the backends to config file | | SELECT VERSION | display the version of Atlas | +----------------------------+---------------------------------------------------------+ 16 rows in set (0.00 sec) (user@127.0.0.1) [(none)]> SELECT * FROM backends; +-------------+---------------------+-------+------+ | backend_ndx | address | state | type | +-------------+---------------------+-------+------+ | 1 | 172.16.128.239:3306 | up | rw | | 2 | 172.16.128.240:3306 | up | ro | | 3 | 172.16.128.241:3306 | up | ro | | 4 | 172.16.128.242:3306 | up | ro | +-------------+---------------------+-------+------+ 4 rows in set (0.00 sec)
然後使用服務埠訪問一下:
[root@YC-ss1 ~]# mysql -h127.0.0.1 -P6002 -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 4 Server version: 5.0.81-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type `help;` or `h` for help. Type `c` to clear the current input statement. (root@127.0.0.1) [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mxqteset | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) (root@127.0.0.1) [(none)]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 1282403306 | +-------------+ 1 row in set (0.00 sec) (root@127.0.0.1) [(none)]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 1282413306 | +-------------+ 1 row in set (0.00 sec) (root@127.0.0.1) [(none)]> select @@server_id; +-------------+ | @@server_id | +-------------+ | 1282423306 | +-------------+ 1 row in set (0.00 sec)
可以看到我們查詢了三次。已經路由到了MGR的三個節點上了。讀已經是沒有問題的。寫的話都會通過VIP:172.16.128.239來寫。而且我們的VIP是會自動故障轉移,也就是說保證是可寫的。這樣基本上就實現的讀寫分離。如果我們不想讓240這臺機器發生讀,就可以把優先順序配置的低一些,優先路由倒其他的機器。
3:壓力測試
我們使用tpcc-mysql進行測試。先進行安裝,下載地址:http://imysql.com/wp-content/uploads/2014/09/tpcc-mysql-src.tgz
[root@YC-ss1 tpccmysql]# gunzip tpcc-mysql-src.tgz [root@YC-ss1 tpccmysql]# tar xf tpcc-mysql-src.tar [root@YC-ss1 tpccmysql]# cd src/ -bash: cd: src/: No such file or directory [root@YC-ss1 tpccmysql]# ls tpcc-mysql tpcc-mysql-src.tar [root@YC-ss1 tpccmysql]# cd tpcc-mysql [root@YC-ss1 tpcc-mysql]# ls add_fkey_idx.sql count.sql create_table.sql drop_cons.sql load.sh README schema2 scripts src [root@YC-ss1 tpcc-mysql]# cd src/ [root@YC-ss1 src]# make cc -w -O2 -g -I. `mysql_config --include` -c load.c cc -w -O2 -g -I. `mysql_config --include` -c support.c cc load.o support.o `mysql_config --libs_r` -lrt -o ../tpcc_load cc -w -O2 -g -I. `mysql_config --include` -c main.c cc -w -O2 -g -I. `mysql_config --include` -c spt_proc.c cc -w -O2 -g -I. `mysql_config --include` -c driver.c cc -w -O2 -g -I. `mysql_config --include` -c sequence.c cc -w -O2 -g -I. `mysql_config --include` -c rthist.c cc -w -O2 -g -I. `mysql_config --include` -c neword.c cc -w -O2 -g -I. `mysql_config --include` -c payment.c cc -w -O2 -g -I. `mysql_config --include` -c ordstat.c cc -w -O2 -g -I. `mysql_config --include` -c delivery.c cc -w -O2 -g -I. `mysql_config --include` -c slev.c cc main.o spt_proc.o driver.o support.o sequence.o rthist.o neword.o payment.o ordstat.o delivery.o slev.o `mysql_config --libs_r` -lrt -o ../tpcc_start [root@YC-ss1 src]# cd .. [root@YC-ss1 tpcc-mysql]# ls add_fkey_idx.sql count.sql create_table.sql drop_cons.sql load.sh README schema2 scripts src tpcc_load tpcc_start
初始化壓測資料:
(root@127.0.0.1) [tpccmysql]> create database tpccmysql; Query OK, 1 row affected (0.02 sec) (root@127.0.0.1) [tpccmysql]> use tpccmysql Database changed (root@127.0.0.1) [tpccmysql]> source /tmp/create_table.sql; Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.01 sec) Query OK, 0 rows affected, 1 warning (0.01 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.18 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec)
但是沒有外來鍵的話是不能壓測的,及時壓測很多也是失敗的。唉,坑爹,還好有一個TPCC去除外來鍵的,這個應該是葉大大搞的,github地址:https://github.com/yejr/tpcc-mysql-autoinc-pk
我們使用去除外來鍵的TPCC進行壓測,先進行安裝
[root@YC-ss1 tpccmysql]# cd tpcc-mysql-autoinc-pk-master [root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ls add_fkey_idx.sql create_table-autoinc-pk.sql load.sh README README.md run_tpcc.sh schema2 scripts src tpcc_load_parallel.sh [root@YC-ss1 tpcc-mysql-autoinc-pk-master]# cd src/ [root@YC-ss1 src]# make cc -w -O2 -g -I. `mysql_config --include` -c load.c cc -w -O2 -g -I. `mysql_config --include` -c support.c cc load.o support.o `mysql_config --libs_r` -lrt -o ../tpcc_load cc -w -O2 -g -I. `mysql_config --include` -c main.c cc -w -O2 -g -I. `mysql_config --include` -c spt_proc.c cc -w -O2 -g -I. `mysql_config --include` -c driver.c cc -w -O2 -g -I. `mysql_config --include` -c sequence.c cc -w -O2 -g -I. `mysql_config --include` -c rthist.c cc -w -O2 -g -I. `mysql_config --include` -c neword.c cc -w -O2 -g -I. `mysql_config --include` -c payment.c cc -w -O2 -g -I. `mysql_config --include` -c ordstat.c cc -w -O2 -g -I. `mysql_config --include` -c delivery.c cc -w -O2 -g -I. `mysql_config --include` -c slev.c cc main.o spt_proc.o driver.o support.o sequence.o rthist.o neword.o payment.o ordstat.o delivery.o slev.o `mysql_config --libs_r` -lrt -o ../tpcc_start [root@YC-ss1 src]# ls delivery.c driver.c load.c load.o main.o neword.c ordstat.c parse_port.h payment.o rthist.h sequence.c sequence.o slev.o spt_proc.h support.c tpc.h delivery.o driver.o load.c.orig main.c Makefile neword.o ordstat.o payment.c rthist.c rthist.o sequence.h slev.c spt_proc.c spt_proc.o support.o trans_if.h [root@YC-ss1 src]# cd .. [root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ls add_fkey_idx.sql create_table-autoinc-pk.sql load.sh README README.md run_tpcc.sh schema2 scripts src tpcc_load tpcc_load_parallel.sh tpcc_start
初始化資料
(root@127.0.0.1) [(none)]> create database tpcc; Query OK, 1 row affected (0.07 sec) (root@127.0.0.1) [(none)]> use tpcc Database changed (root@127.0.0.1) [tpcc]> source /home/tpccmysql/tpcc-mysql-autoinc-pk-master/create_table-autoinc-pk.sql;
使用tpcc_load載入資料:
[root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ./tpcc_load 127.0.0.1:6002 tpcc root "123456" 30 ************************************* *** ###easy### TPC-C Data Loader *** ************************************* <Parameters> [server]: 127.0.0.1 [port]: 6002 [DBname]: tpcc [user]: root [pass]: 123456 [warehouse]: 30 TPCC Data Load Started... Loading Item .................................................. 50000 .................................................. 100000
這個過程有點漫長,可以去樓下抽支菸。然後進行壓力測試,測試一個小時,這個時間也有點久,可以下樓買個冰棍了。
[root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ./tpcc_start -h 127.0.0.1 -P 6002 -d tpcc -u root -p 123456 -w 30 -c 8 -r 120 -l 3600 -f tpcc_0705 *************************************** *** ###easy### TPC-C Load Generator *** *************************************** option h with value `127.0.0.1` option P with value `6002` option d with value `tpcc` option u with value `root` option p with value `123456` option w with value `30` option c with value `8` option r with value `120` option l with value `3600` option f with value `tpcc_0705` <Parameters> [server]: 127.0.0.1 [port]: 6002 [DBname]: tpcc [user]: root [pass]: 123456 [warehouse]: 30 [connection]: 8 [rampup]: 120 (sec.) [measure]: 3600 (sec.)
等待擦測試結果
STOPPING THREADS........ <Raw Results> [0] sc:24209 lt:1 rt:0 fl:0 [1] sc:24167 lt:0 rt:0 fl:0 [2] sc:2421 lt:1 rt:0 fl:0 [3] sc:2420 lt:0 rt:0 fl:0 [4] sc:2423 lt:0 rt:0 fl:0 in 3600 sec. <Raw Results2(sum ver.)> [0] sc:24209 lt:1 rt:0 fl:0 [1] sc:24210 lt:0 rt:0 fl:0 [2] sc:2421 lt:1 rt:0 fl:0 [3] sc:2421 lt:0 rt:0 fl:0 [4] sc:2423 lt:0 rt:0 fl:0 <Constraint Check> (all must be [OK]) [transaction percentage] Payment: 43.43% (>=43.0%) [OK] Order-Status: 4.35% (>= 4.0%) [OK] Delivery: 4.35% (>= 4.0%) [OK] Stock-Level: 4.35% (>= 4.0%) [OK] [response time (at least 90% passed)] New-Order: 100.00% [OK] Payment: 100.00% [OK] Order-Status: 99.96% [OK] Delivery: 100.00% [OK] Stock-Level: 100.00% [OK] <TpmC> 403.500 TpmC
我們可以看到在我這個級別壓測下,Atlas+MGR還是很穩定的。下面我們看下故障轉移
4:故障轉移的測試
往上面回顧一下我們的機器環境
機器 | MySQL | 環境 |
---|---|---|
172.16.128.240 | MGR NODE1 | MGR |
172.16.128.241 | MGR NODE2 | MGR |
172.16.128.242 | MGR NODE3 | MGR |
172.16.128.239 | VIP | keepalived |
現在我們Atlas提供的服務埠是172.16.128.242:6002,我們模擬NODE1節點故障 ,然後看一下壓測的結果最終結果是如何的。
[root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ./tpcc_start -h 127.0.0.1 -P 6002 -d tpcc -u root -p 123456 -w 30 -c 8 -r 120 -l 300 -f tpcc_0706 *************************************** *** ###easy### TPC-C Load Generator *** *************************************** option h with value `127.0.0.1` option P with value `6002` option d with value `tpcc` option u with value `root` option p with value `123456` option w with value `30` option c with value `8` option r with value `120` option l with value `300` option f with value `tpcc_0706` <Parameters> [server]: 127.0.0.1 [port]: 6002 [DBname]: tpcc [user]: root [pass]: 123456 [warehouse]: 30 [connection]: 8 [rampup]: 120 (sec.) [measure]: 300 (sec.) RAMP-UP TIME.(120 sec.)
第二部關閉NODE1,
2006, HY000, MySQL server has gone away payment 4:1 2006, HY000, MySQL server has gone away payment 4:1 2006, HY000, MySQL server has gone away payment 4:1 2006, HY000, MySQL server has gone away
全部都TM的gone away了,也真是無奈的,我們在重新開始壓測,發現是沒問題的,
[root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ./tpcc_start -h 127.0.0.1 -P 6002 -d tpcc -u root -p 123456 -w 30 -c 8 -r 120 -l 300 -f tpcc_0706 *************************************** *** ###easy### TPC-C Load Generator *** *************************************** option h with value `127.0.0.1` option P with value `6002` option d with value `tpcc` option u with value `root` option p with value `123456` option w with value `30` option c with value `8` option r with value `120` option l with value `300` option f with value `tpcc_0706` <Parameters> [server]: 127.0.0.1 [port]: 6002 [DBname]: tpcc [user]: root [pass]: 123456 [warehouse]: 30 [connection]: 8 [rampup]: 120 (sec.) [measure]: 300 (sec.) RAMP-UP TIME.(120 sec.) MEASURING START. 10, 905(0):1.694|4.648, 905(0):0.449|4.042, 91(0):0.217|0.562, 90(0):2.178|3.151, 90(0):5.098|10.983 20, 724(0):1.750|2.948, 725(0):0.548|1.545, 73(0):0.173|0.230, 72(0):1.947|2.309, 73(0):4.700|5.217 30, 1050(0):1.746|4.647, 1046(0):0.372|0.746, 104(0):0.163|0.258, 105(0):1.843|1.966, 105(0):5.323|5.616 40, 917(0):1.754|4.656, 918(0):0.392|0.985, 92(0):0.190|0.191, 91(0):2.042|2.340, 92(0):5.574|6.931 50, 951(0):1.688|2.792, 950(1):0.440|5.618, 96(0):0.206|0.373, 96(0):2.240|3.440, 95(0):4.809|4.985 60, 965(0):1.764|2.737, 966(0):0.393|0.836, 96(0):0.222|0.517, 97(0):2.220|2.448, 95(0):4.989|4.990 70, 940(0):1.589|2.525, 941(0):0.463|1.766, 94(0):0.164|0.263, 94(0):2.446|3.339, 96(0):5.512|5.704 80, 746(2):1.671|7.135, 742(1):0.445|5.139, 75(0):0.229|0.324, 74(0):2.353|2.653, 74(0):5.342|5.454 90, 1022(0):1.784|4.030, 1022(0):0.421|1.311, 102(0):0.311|0.479, 102(0):2.093|3.696, 102(0):5.810|6.025 100, 1012(0):1.809|2.774, 1012(0):0.436|0.961, 101(0):0.207|0.381, 102(0):2.135|2.262, 101(0):5.867|5.931 110, 928(0):1.651|2.960, 930(0):0.397|1.347, 93(0):0.193|0.371, 92(0):2.157|3.873, 93(0):5.445|6.339 120, 1000(0):1.670|2.486, 1001(0):0.408|0.888, 99(0):0.257|0.405, 100(0):1.943|2.189, 100(0):5.292|5.770 130, 992(0):1.749|4.123, 990(0):0.463|1.120, 100(0):0.206|0.277, 99(0):1.742|1.759, 100(0):4.674|5.843 140, 868(1):1.893|7.145, 864(0):0.447|1.527, 86(0):0.188|0.221, 87(0):2.262|2.292, 86(0):4.586|4.684 150, 1024(0):1.641|2.351, 1027(0):0.415|0.680, 103(0):0.153|0.195, 102(0):1.936|5.337, 102(0):4.877|5.004 <Raw Results> [0] sc:28932 lt:6 rt:0 fl:0 [1] sc:28921 lt:2 rt:0 fl:0 [2] sc:2894 lt:0 rt:0 fl:0 [3] sc:2893 lt:0 rt:0 fl:0 [4] sc:2894 lt:0 rt:0 fl:0 in 300 sec. <Raw Results2(sum ver.)> [0] sc:28932 lt:6 rt:0 fl:0 [1] sc:28934 lt:2 rt:0 fl:0 [2] sc:2894 lt:0 rt:0 fl:0 [3] sc:2893 lt:0 rt:0 fl:0 [4] sc:2894 lt:0 rt:0 fl:0 <Constraint Check> (all must be [OK]) [transaction percentage] Payment: 43.47% (>=43.0%) [OK] Order-Status: 4.35% (>= 4.0%) [OK] Delivery: 4.35% (>= 4.0%) [OK] Stock-Level: 4.35% (>= 4.0%) [OK] [response time (at least 90% passed)] New-Order: 99.98% [OK] Payment: 99.99% [OK] Order-Status: 100.00% [OK] Delivery: 100.00% [OK] Stock-Level: 100.00% [OK] <TpmC> 5787.600 TpmC
我們再看一下Atlas的管理資訊
(user@127.0.0.1) [(none)]> SELECT * FROM backends; +-------------+---------------------+-------+------+ | backend_ndx | address | state | type | +-------------+---------------------+-------+------+ | 1 | 172.16.128.239:3306 | up | rw | | 2 | 172.16.128.240:3306 | up | ro | | 3 | 172.16.128.241:3306 | up | ro | | 4 | 172.16.128.242:3306 | up | ro | +-------------+---------------------+-------+------+ 4 rows in set (0.00 sec)
發現有問題的NODE1節點的狀態居然還是UP,也就是說我們還要自己想辦法通知Atlas我這個節點掛了,要修改配置資訊。不過最起碼說明讀寫分離是可以做到了。所以說這個還是有點不靠譜的啊,我們沒辦法實現。我們選材mycat試試
MyCat讀寫分離高可用測試
安裝使用mycat,貼一下mycat的配置檔案
[root@YC-ss1 conf]# cat schema.xml <?xml version="1.0"?> <!DOCTYPE mycat:schema SYSTEM "schema.dtd"> <mycat:schema xmlns:mycat="http://io.mycat/"> <schema name="TESTDB" checkSQLschema="false" sqlMaxLimit="100"> <table name="customer" primaryKey="aid" dataNode="tp1" /> <table name="district" primaryKey="aid" dataNode="tp1" /> <table name="history" primaryKey="aid" dataNode="tp1" /> <table name="item" primaryKey="aid" dataNode="tp1" /> <table name="new_orders" primaryKey="aid" dataNode="tp1" /> <table name="order_line" primaryKey="aid" dataNode="tp1" /> <table name="orders" primaryKey="aid" dataNode="tp1" /> <table name="stock" primaryKey="aid" dataNode="tp1" /> <table name="warehouse" primaryKey="aid" dataNode="tp1" /> </schema> <dataNode name="tp1" dataHost="dbhost239" database="tpcc" /> <dataHost name="dbhost239" maxCon="1000" minCon="10" balance="1" writeType="0" dbType="mysql" dbDriver="native" switchType="-1" slaveThreshold="100"> <heartbeat>select user()</heartbeat> <!-- can have multi write hosts --> <writeHost host="hostM1" url="172.16.128.239:3306" user="root" password="123456"> </writeHost> <writeHost host="hostS1" url="172.16.128.240:3306" user="root" password="123456" /> <writeHost host="hostS2" url="172.16.128.241:3306" user="root" password="123456" /> <writeHost host="hostS3" url="172.16.128.242:3306" user="root" password="123456" /> </dataHost> </mycat:schema>
不在贅述,直接通過節點一load資料
[root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ./tpcc_load 172.16.128.239:3306 tpcc root "123456" 10 ************************************* *** ###easy### TPC-C Data Loader *** ************************************* <Parameters> [server]: 172.16.128.239 [port]: 3306 [DBname]: tpcc [user]: root [pass]: 123456 [warehouse]: 10 TPCC Data Load Started... Loading Item .................................................. 50000 .................................................. 100000 Item Done. Loading Warehouse Loading Stock Wid=1 .................................................. 50000 .................................................. 100000 Stock Done. Loading District Loading Stock Wid=2 .................................................. 50000 .................................................. 100000
然後先壓測:
[root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ./tpcc_start -h 127.0.0.1 -P 8066 -d TESTDB -u root -p 123456 -w 10 -c 8 -r 120 -l 300 -f tpcc_0710
看一下壓測結果
250, 1320(0):1.793|4.080, 1320(0):0.370|1.357, 133(0):0.160|0.185, 131(0):1.993|2.155, 132(0):2.476|3.372 260, 1154(0):1.695|3.316, 1153(0):0.371|1.664, 114(0):0.138|0.144, 116(0):1.936|3.117, 114(0):3.079|3.506 270, 1076(0):1.780|3.807, 1076(0):0.359|0.706, 108(0):0.155|0.195, 107(0):1.830|3.335, 109(0):2.296|2.368 280, 1094(0):1.682|2.435, 1092(0):0.362|1.352, 109(0):0.142|0.163, 110(0):1.963|2.259, 109(0):2.760|2.868 290, 1264(0):1.668|2.191, 1269(0):0.339|0.744, 127(0):0.159|0.188, 127(0):1.847|1.943, 126(0):2.909|3.890 300, 1169(0):1.605|4.055, 1166(0):0.336|1.421, 117(0):0.160|0.192, 117(0):1.874|3.247, 118(0):2.456|2.595 STOPPING THREADS........ <Raw Results> [0] sc:35560 lt:3 rt:0 fl:0 [1] sc:35563 lt:1 rt:0 fl:0 [2] sc:3557 lt:0 rt:0 fl:0 [3] sc:3557 lt:0 rt:0 fl:0 [4] sc:3557 lt:0 rt:0 fl:0 in 300 sec. <Raw Results2(sum ver.)> [0] sc:35560 lt:3 rt:0 fl:0 [1] sc:35565 lt:1 rt:0 fl:0 [2] sc:3557 lt:0 rt:0 fl:0 [3] sc:3557 lt:0 rt:0 fl:0 [4] sc:3557 lt:0 rt:0 fl:0 <Constraint Check> (all must be [OK]) [transaction percentage] Payment: 43.48% (>=43.0%) [OK] Order-Status: 4.35% (>= 4.0%) [OK] Delivery: 4.35% (>= 4.0%) [OK] Stock-Level: 4.35% (>= 4.0%) [OK] [response time (at least 90% passed)] New-Order: 99.99% [OK] Payment: 100.00% [OK] Order-Status: 100.00% [OK] Delivery: 100.00% [OK] Stock-Level: 100.00% [OK] <TpmC> 7112.600 TpmC
由於大部分壓測都是更新資料,所以都路由到了239VIP上。我們可以直接測一下讀取
(root@127.0.0.1) [TESTDB]> select *,@@server_id from item limit 1; +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ | aid | i_id | i_im_id | i_name | i_price | i_data | @@server_id | +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ | 35 | 1 | 1320 | lUPdQrxNjkFvKSw698smv | 11.14 | jtIe2rjbaByDZp8wXr9QBbNFjV7VOIL8BdmBSTk | 1282403306 | +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ 1 row in set (0.37 sec) (root@127.0.0.1) [TESTDB]> select *,@@server_id from item limit 1; +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ | aid | i_id | i_im_id | i_name | i_price | i_data | @@server_id | +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ | 35 | 1 | 1320 | lUPdQrxNjkFvKSw698smv | 11.14 | jtIe2rjbaByDZp8wXr9QBbNFjV7VOIL8BdmBSTk | 1282413306 | +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ 1 row in set (0.05 sec) (root@127.0.0.1) [TESTDB]> select *,@@server_id from item limit 1; +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ | aid | i_id | i_im_id | i_name | i_price | i_data | @@server_id | +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ | 35 | 1 | 1320 | lUPdQrxNjkFvKSw698smv | 11.14 | jtIe2rjbaByDZp8wXr9QBbNFjV7VOIL8BdmBSTk | 1282423306 | +-----+------+---------+-----------------------+---------+-----------------------------------------+-------------+ 1 row in set (0.07 sec)
讀的請求會路由倒三臺機器上。現在假設240的主機掛掉,VIP239從240飄到241,我們看一下mycat會不會把連線有沒有問題。 首先給一個持續的壓測,
[root@YC-ss1 tpcc-mysql-autoinc-pk-master]# ./tpcc_start -h 127.0.0.1 -P 8066 -d TESTDB -u root -p 123456 -w 10 -c 8 -r 120 -l 1800
在壓測的過程中我們停掉240主機,VIP會切換的241上。我們再看壓測結果。
1184, HY000, Connection refused payment 7:1 1184, HY000, Connection refused neword 4:1 1184, HY000, Connection refused payment 6:1 1184, HY000, Connection refused payment 5:1 1184, HY000, Connection refused payment 1:1 1184, HY000, Connection refused payment 0:1 1184, HY000, Connection refused neword 3:1 1184, HY000, Connection refused payment 6:1 1184, HY000, Connection refused neword 4:1 1184, HY000, Connection refused payment 7:1 1184, HY000, Connection refused 70, 723(2):1.654|47.728, 726(5):0.655|76.966, 72(0):0.164|0.298, 72(0):1.957|2.127, 72(0):2.550|2.571 80, 1178(0):1.769|2.702, 1182(0):0.416|0.870, 118(0):0.159|0.288, 117(0):1.938|1.971, 118(0):2.769|3.452 90, 1332(0):1.766|4.103, 1330(0):0.425|1.254, 134(0):0.175|0.178, 133(0):2.207|2.644, 134(0):3.027|3.508 100, 901(0):1.822|2.738, 902(0):0.431|1.109, 90(0):0.152|0.158, 90(0):2.040|2.049, 90(0):2.991|3.176 ------- <Raw Results> [0] sc:22875 lt:3 rt:81654 fl:40 [1] sc:22879 lt:5 rt:87541 fl:40 [2] sc:2288 lt:0 rt:9995 fl:5 [3] sc:2287 lt:0 rt:13993 fl:7 [4] sc:2291 lt:0 rt:5997 fl:3 in 240 sec. <Raw Results2(sum ver.)> [0] sc:22881 lt:3 rt:81655 fl:40 [1] sc:22881 lt:5 rt:87552 fl:40 [2] sc:2288 lt:0 rt:9995 fl:5 [3] sc:2287 lt:0 rt:13993 fl:7 [4] sc:2291 lt:0 rt:5997 fl:3 <Constraint Check> (all must be [OK]) [transaction percentage] Payment: 43.48% (>=43.0%) [OK] Order-Status: 4.35% (>= 4.0%) [OK] Delivery: 4.35% (>= 4.0%) [OK] Stock-Level: 4.35% (>= 4.0%) [OK] [response time (at least 90% passed)] New-Order: 99.99% [OK] Payment: 99.98% [OK] Order-Status: 100.00% [OK] Delivery: 100.00% [OK] Stock-Level: 100.00% [OK] <TpmC> 5719.500 TpmC
可以看到,在keepalived切換過程中,連線是失敗的,但是最後VIP切換成功以後所有的連線就正常了。所以說使用MYCAT來實現讀寫分離是可行的。但是也有一個問題,session close的時候會有問題,但是新連結能夠保證是沒有問題的,從最後壓測結果也是可以看出來的。但是從基本也滿足了我們的基本需求。