環境介紹,VIP 172.16.128.239在172.16.128.240上,我們是基於172.16.128.240和172.16.128.241做的keepalived。MGR為多主模式。我們考慮使用中間件能夠實現MGR的讀寫分離。中間件選型為Atlas,Atlas是360團隊開源的一套基於My ...
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) ([email protected]) [(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%