业务突然反馈写失败,报错:Lost connection to MySQL server during query。读没有问题
检查proxysql报如下日志:
2019-04-28 16:53:06 MySQL_Session.cpp:2934:handler(): [WARNING] Error during query on (1,10.x.x.87,3306): 1290, The MySQL server is running with the -- read-only option so it cannot execute this statement
2019-04-28 16:53:06 MySQL_Session.cpp:95:kill_query_thread(): [WARNING] KILL CONNECTION 2740507 on 10.x.x.87:3306
2019-04-28 16:54:06 MySQL_Session.cpp:2934:handler(): [WARNING] Error during query on (1,10.x.x.87,3306): 1290, The MySQL server is running with the -- read-only option so it cannot execute this statement
2019-04-28 16:54:06 MySQL_Session.cpp:95:kill_query_thread(): [WARNING] KILL CONNECTION 2737976 on 10.x.x.87:3306
2019-04-28 16:55:29 MySQL_Session.cpp:2934:handler(): [WARNING] Error during query on (1,10.x.x.87,3306): 1290, The MySQL server is running with the -- read-only option so it cannot execute this statement
2019-04-28 16:55:29 MySQL_Session.cpp:95:kill_query_thread(): [WARNING] KILL CONNECTION 2796368 on 10.x.x.87:3306
2019-04-28 16:59:07 MySQL_Session.cpp:2934:handler(): [WARNING] Error during query on (1,10.x.x.87,3306): 1054, Unknown column 'date' in 'order clause'
2019-04-28 17:05:10 MySQL_Session.cpp:2934:handler(): [WARNING] Error during query on (1,10.x.x.87,3306): 1290, The MySQL server is running with the -- read-only option so it cannot execute this statement
2019-04-28 17:05:10 MySQL_Session.cpp:95:kill_query_thread(): [WARNING] KILL CONNECTION 2797386 on 10.x.x.87:3306
2019-04-28 17:06:45 MySQL_Session.cpp:2934:handler(): [WARNING] Error during query on (1,10.x.x.87,3306): 1290, The MySQL server is running with the -- read-only option so it cannot execute this statement
2019-04-28 17:06:45 MySQL_Session.cpp:95:kill_query_thread(): [WARNING] KILL CONNECTION 2797798 on 10.x.x.87:3306
意思就是有写操作到从了,由于从是只读的,写失败kill了连接,导致报lost connection to mysql during query.
为什么写会路由到从呢?
先看路由配置:
![proxysql读写分离配置](http://imgs.itopers.com/YD20190429-093817.png "在这里输入图片标题")
读写分离路由是没有问题的,通过日志看到有set autocommit=0这样开启事务。检查配置发现:
mysql-forward_autocommit=false
mysql-autocommit_false_is_transaction=false
这两个设置为false,会导致set autocommit = 0不会开启事务,所以如果set autocommit=0后,先select,再有写操作,有可能会导致写到从库(也存在bug)。
如下有issue类似问题链接:
- [Write Queries were routing to read hostgroup](https://github.com/sysown/proxysql/issues/1658)
- [When mysql-forward_autocommit=true , treat autocommit=0 as a transaction](https://github.com/sysown/proxysql/issues/1155)
- ["set autocommit = 0" is not a transaction](https://github.com/sysown/proxysql/issues/1716)
所以建议将如下配置为true:
mysql-forward_autocommit=true
mysql-autocommit_false_is_transaction=true
还有写请求会落到从库的,有可能是路由规划配置有问题,如用match_pattern匹配,就有可能在sql换行就匹配不到的情况。最好是用match_digest来匹配。
它们的区别是:
match_pattern :原始的sql进行正则匹配
match_digest :签名后的sql进行正则匹配,如stats_mysql_query_digest里的sql
###总结:
ProxySQL是MySQL中件间中比较优秀的,但在使用上还需要多注意,尽量简单使用,理解配置产生的效果及影响。
文章最后更新时间:
2019年04月29日 10:02:10