# MySQL-Proxy **Repository Path**: waldenth/my-sql-proxy ## Basic Information - **Project Name**: MySQL-Proxy - **Description**: Proxy层断点恢复 - **Primary Language**: C - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-25 - **Last Updated**: 2024-01-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Proxy - failover-switch : Failover自动切换 - mysql-proxy : Hook断点SQL,续作,仓库二进制为win版本,逻辑脚本(.lua)应该可在linux下使用 ### mysql-proxy - mysql服务端需要设置密码型连接才可使用Proxy层 ```sql use mysql; select host, user, plugin from user; ALTER USER 'root'@'root的host' IDENTIFIED WITH mysql_native_password BY '你的密码'; select host, user, plugin from user; ``` 确保用户类型是`mysql_native_password` ```bash /conf/mysql-proxy.conf [mysql-proxy] proxy-address=:3308 # 3308端口代理,客户端 mysql -u root -P 3308 -p proxy-backend-addresses=127.0.0.1:3306 # 监听127.0.0.1:3306,此为实际mysql服务端 proxy-lua-script = ../proxy.lua log-file= ../mysql-proxy.log log-level=info daemon=true ``` 以数据库testdb,表userinfo(name varchar(255),hash varchar(255))为例 - 启动服务端,在mysql-proxy目录下打开终端 ```bash .\bin\mysql-proxy.exe --defaults-file=".\conf\mysql-proxy.conf" ``` - Windows下mysql官方客户端终端回显不兼容,使用Navicat等工具,新建连接, 连接右键点击命令列界面,打开shell ![Alt text](image.png) ![Alt text](image-1.png) 输入sql语句测试 ```sql select * from testdb.userinfo; ``` ![Alt text](image-2.png) 复制以下多条sql语句,输入。其中select sleep(8)表示忙等8秒后返回结果,如果异常中断则会提前返回,用于模拟长事务,亦可设置更长时间。 ```sql select * from testdb.userinfo; select sleep(8); insert into testdb.userinfo values("test","111111"); ``` 立刻关闭服务端 ![Alt text](image-3.png) (由于单机测试,没有故障转移,会回显全部后端宕机) 此时,服务端正在执行sleep(8),因为故障提前返回,insert语句发出后没有响应,proxy运行终端将显示`Server Down!`信息 重启服务端,输入SQL语句 ```sql select * from testdb.userinfo; ``` proxy将尝试恢复中断后发送的最后一条语句,拦截返回结果,并显示客户端操作语句的在续作后的结果 ![Alt text](image-4.png) ![Alt text](image-5.png) 重新运行时请清空sqls.txt和types.txt