機器 | 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,而且進行了優化。mysql
[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 之後會生成四個文件夾。首先咱們要配置密碼的加密:git
[root@YC-ss1 bin]# ./encrypt 123456 /iZxz+0GRoA=
個人root@%帳號密碼就是123456,比較簡單。咱們先記錄這個加密字符串,等會要在配置文件中配置。配置配置文件github
[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
啓動Atlassql
[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)
而後使用服務端口訪問一下:bash
[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這臺機器發生讀,就能夠把優先級配置的低一些,優先路由倒其餘的機器。session
咱們使用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仍是很穩定的。下面咱們看下故障轉移
往上面回顧一下咱們的機器環境
機器 | 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的配置文件
[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的時候會有問題,可是新連接可以保證是沒有問題的,從最後壓測結果也是能夠看出來的。可是從基本也知足了咱們的基本需求。