架構篇(architecture)
MySQL Proxy 的定位是存在於 mysql client 和 mysql server 之間的一個簡單的程序,可以對從其上經過的數據進行檢查、轉換和直接進行相應操做。
應用範圍包括:
- 負載均衡(load balancing)
- 故障處理(fail over)
- 查詢追蹤(query tracking)
- 查詢分析(query analysis)
- (...)
內部實現上講,MySQL Proxy 是這樣的一個協議棧:
(應該有個圖,可是,額,太監了)
基於這個 core 可以將 protocol 的各個 phase 暴露給 plugin 。
- connect -> auth;
- auth -> command;
- command -> disconnect;
- command -> command;
- connect -> disconnect;
- auth -> disconnect;
上述生命週期中的每個階段都包含了多種協議狀態變遷。以鑑權過程爲例,其包含了至少 3 個包:
life-cycle 中涉及的實體: Client, Proxy, Server;
Client -> Proxy [ label = "accept()" ];
Proxy -> Proxy [ label = "script: connect_server()" ];
Proxy -> Server [ label = "connect()" ];
...;
Server -> Proxy [ label = "recv(auth-challenge)" ];
Proxy -> Proxy [ label = "script: read_handshake()" ];
Proxy -> Client [ label = "send(auth-challenge)" ];
Client -> Proxy [ label = "recv(auth-response)" ];
Proxy -> Proxy [ label = "script: read_auth()" ];
Server -> Proxy [ label = "send(auth-response)" ];
Server -> Proxy [ label = "recv(auth-result)" ];
Proxy -> Proxy [ label = "script: read_auth_result()" ];
Proxy -> Client [ label = "send(auth-result)" ];
...;
當核心層 core 面對大量 connection 的時候是具有自由伸縮特性時,plugin/scripting 層就可以輕易對 end-users 隱藏細節,並簡化客戶端的實現。
MySQL Proxy 是經過各類庫(Chassis, libraries and Plugins)構建出來的協議棧:
其中 chassis 提供了命令行和做爲 daemon 應用所需的全部通用函數實現,包括:
- 命令行和配置文件(commandline and configfiles)
- 日誌(logging)
- 支持 daemon/service 方式運行(daemon/service support)
- plugin 加載(plugin loading)
同時 MySQL Procotol 的 libraries 還實現了實現了各類編碼解碼功能:
- 客戶端協議(client protocol)
- binlog 協議(binlog protocol)
- myisam 文件處理(myisam files)
- frm 文件處理(frm files)
- masterinfo 文件處理(masterinfo files)