架构篇(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)