MySQL ProxyIt is between the client application and the MySQL server and implements its function by truncating, changing, and forwarding communications between the client and the backend database, this is the same as the basic idea of network proxy servers such as WinGate. The proxy server deals with the TCP/IP protocol.MySQL ProxyThe working mechanism should also be clearMySQLCommunication protocol between the client and the server,MySQLProtocol includes 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 ProxyUse the lua script 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()
As for why the lua scripting language is used, I think this is because the wormhole storage engine is used in MySQL Proxy. This worm hole storage engine is very interesting. The data storage format isLua scriptIt's really creative.
Summary:MySQL Proxy Lua scriptThe work mechanism analysis is complete. I hope this article will help you.