Original article: http://www.ningoo.net/html/2008/how_mysql_proxy_works.html
The MySQL proxy is located between the client application and the MySQL server, and implements its function by truncating, changing, and forwarding the communication between the client and the backend database.WinGateThe basic idea of network proxy servers is the same. The proxy server deals with the TCP/IP protocol. To understand the working mechanism of the MySQL proxy, you must also understand the communication protocol between the mysql client and the server,MySQL ProtocolThere are two basic processes: authentication and query:
The authentication process includes:
- The client initiates a connection request to the server.
- The server sends a handshake message to the client.
- The client sends an authentication request to the server.
- The server sends the authentication result to the client.
If the authentication succeeds, the query process is displayed:
- The client initiates a query request to the server.
- The server returns the query result to the client.
Of course, this is just a rough description. The packages sent in each process are in a fixed format. If you want to learn more about MySQL protocol, please go here. What MySQL proxy needs to do is to intervene in various processes of the Protocol. First, MySQL proxy accepts client requests as the server, analyzes and processes these requests according to the configuration, and then forwards the requests to the corresponding backend database server as the client, and then accepts the server information, return to the client. Therefore, MySQL proxy must implement both the client and server protocols. To analyze the SQL statements sent from the client, you also need to include an SQL parser. MySQL proxy is equivalent to a lightweight mysql. In fact, the Admin Server of MySQL proxy can use SQL to query status information.
MySQL proxyLuaScript to control the connection forwarding mechanism. The main functions are used in various MySQL Protocol processes. This can be seen from the function name:
- Connect_server ()
- Read_handshake ()
- Read_auth ()
- Read_auth_result ()
- Read_query ()
- Read_query_result ()
Why?LuaScript language. I think this is because MySQL proxy usesWormholeThe relationship between storage engines. This plug-in storage engine is very interesting. The data storage format is a Lua script, which is really creative.