標籤:mysq 抓取 images code wait 快速定位 官方文檔 流量 alt
官方文檔:https://github.com/session-replay-tools/mysql-replay-module
tcpcopy可以將正式環境上來自用戶端的提取複寫一份到測試端並複現,想要真實的對MySQL進行容量規劃,可以藉助tcpcopy來將線上的流量
呈倍數的增長,將其複製到測試環境,從而快速定位測試環境出現瓶頸時負載情況,進而做好容量的全域把控
部署
偽裝用戶端IP:1.1.1.4
online server:1.1.1.1
target server :1.1.1.2
assistant server:1.1.1.3
前提條件:
1、三個節點的網路互連無網卡的安全限制(大多數雲環境設定了安全限制),tcpcopy通過 -c 選項可以將線上伺服器抓取的包複製一份並將來源IP
偽裝成指定的用戶端IP發送給target,如果進行了安全限制,一個網卡無法綁定2個IP,所以online server則會拒絕發送複製包,在online server
上通過tcpdump抓取的包將會如下
tcpdump -i eth0 -nn port 3306 and host 1.1.1.4
每隔3秒online server會發送RST
關於tcp標誌
正常情況下偽用戶端會和target建立三向交握
2、作業系統關閉rp_filter,核心2.6版本預設是關閉
echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter
3、按照官方文檔將MySQL服務的使用者名稱密碼寫入設定檔
4、assistant server關閉路由功能,預設是關閉的
echo 0 > /proc/sys/net/ipv4/ip_forward
具體操作
online server
/usr/local/src/tcpcopy/objs/tcpcopy -x 3306-1.1.1.2:3306 -s 1.1.1.3 -c 1.1.1.4 -n 3 -d
# 如果是多執行個體,sourcePort-targetIP:targetPort,以逗號分隔
/usr/local/src/tcpcopy/objs/tcpcopy -x 3306-1.1.1.2:3306,3307-1.1.1.2:3307 -s 1.1.1.3 -c 1.1.1.4 -n 3 -d
tcpcopy會捕獲當前主機的‘3306’報文,更改用戶端的IP為1.1.1.4,發送這些報文到target server 1.1.1.2的目標連接埠‘3306’,並串連1.1.1.3詢問intercept 將響應包傳遞給自己-n 3 是複製3倍份流量到target,tcpcopy會處理衝突的部分
target server
/usr/local/src/intercept/objs/intercept -i eth0 -F tcp and src port 3306 -d
ntercept會從eth0網卡捕獲監聽3306連接埠的tcp包
assistant server
route add -host 1.1.1.4 gw 1.1.1.3
路由用戶端的所有響應包到assistant server類比用戶端流量online server
# mysql -h1.1.1.1 -uadmin -p123123 -P3306
# 建立帶有主鍵的表t2CREATE TABLE `t2` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 # 建立無限制欄位的表t3CREATE TABLE `t3` ( `id` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8
# 然後在兩個表中分別插入一條資料
insert into t2 values(1);
insert into t3 values(1);
target server
mysql> show processlist;+------+-------------+---------------------+------+---------+---------+--------------------------------------------------------+------------------+| Id | User | Host | db | Command | Time | State | Info |+------+-------------+---------------------+------+---------+---------+--------------------------------------------------------+------------------+| 11 | system user | | NULL | Connect | 3044244 | Slave has read all relay log; waiting for more updates | NULL || 12 | system user | | NULL | Connect | 3044248 | Waiting for an event from Coordinator | NULL || 13 | system user | | NULL | Connect | 3044248 | Waiting for an event from Coordinator | NULL || 14 | system user | | NULL | Connect | 3044248 | Waiting for an event from Coordinator | NULL || 15 | system user | | NULL | Connect | 3044248 | Waiting for an event from Coordinator | NULL || 3961 | root | localhost | NULL | Query | 0 | starting | show processlist || 3962 | admin | 1.1.1.4:24695 | NULL | Sleep | 5 | | NULL || 3963 | admin | 1.1.1.4:24286 | NULL | Sleep | 5 | | NULL || 3964 | admin | 1.1.1.4:24759 | NULL | Sleep | 5 | | NULL |+------+-------------+---------------------+------+---------+---------+--------------------------------------------------------+------------------+9 rows in set (0.00 sec)
mysql> select * from t2;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> select * from t3;
+------+
| id |
+------+
| 1 |
| 1 |
| 1 |
+------+
3 rows in set (0.00 sec)
可以看到同時3倍的流量複製效應,對應的是3個回話;表t2有約束,tcpcopy會處理衝突的部分,
所以最後看到的是t1表中只有一條資料,而t2表中則是3倍流量複製的結果
MySQL容量規劃之tcpcopy應用之道