Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.
Original topic: django代码连接tidb报Lost connection to MySQL server during query错误
【TiDB Usage Environment】Production Environment
【TiDB Version】
【Reproduction Path】What operations were performed when the issue occurred
【Encountered Issue: Issue Phenomenon and Impact】
【Resource Configuration】
【Attachments: Screenshots/Logs/Monitoring】
Django code connecting to TiDB sometimes reports an error, with the error message as follows:
CR.CR_SERVER_LOST, “Lost connection to MySQL server during query”)
TiDB QPS and load are very low, and there are no slow queries.
It seems like you have a connection timeout.
Is there middleware like HAProxy in front of TiDB?
Check if there are any issues with the network.
Additionally, you should check if the wait_timeout and interactive_timeout settings are too short.
Connection timed out, check the read timeout setting.
There is no middleware, the business uses k8s pods.
Using TiDB’s default value. This value should be quite sufficient.
This is also the default value for TiDB, which seems to be 28800, not small.
This is not a connection timeout. It should be a connection interruption.
What is the value of CONN_MAX_AGE configured in the Django framework?
Sometimes it reports “Lost connection.” Is there a regular pattern to this? How often does it report?
This CONN_MAX_AGE is 10 minutes.
There is no pattern to follow; it appears suddenly. Then it lasts for a few hours and disappears again.
Not sure what the issue is, sometimes I wonder if it’s packet loss. But when using MySQL, there’s no problem.
Is there an OOM? Is it happening during data import or large queries?
Also, if you have identified the root cause, could you please teach me how to reliably reproduce it on a local single machine? I just started working with TiDB today, specifically to reproduce and solve this issue.
There is no OOM. We provide OLTP services and have very high requirements for the service.
Yes, it is a connection interruption. When the connection exceeds wait_timeout, it gets disconnected. If you try to query again, you will encounter this issue. The MySQL client has the same problem.
The client’s timeout is only 10 minutes; the connection timeout for the TiDB database is wait_timeout, which is 8 hours. This should not be an issue.
When an issue occurs, have you checked the tidb.log logs? Are there any special logs?