Django code reports "Lost connection to MySQL server during query" error when connecting to TiDB

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错误

| username: TiDBer_lei_xin

【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.

| username: Raymond | Original post link

It seems like you have a connection timeout.

| username: TiDBer_pkQ5q1l0 | Original post link

Is there middleware like HAProxy in front of TiDB?

| username: tidb菜鸟一只 | Original post link

Check if there are any issues with the network.

| username: TiDBer_pkQ5q1l0 | Original post link

Additionally, you should check if the wait_timeout and interactive_timeout settings are too short.

| username: liuis | Original post link

Connection timed out, check the read timeout setting.

| username: TiDBer_lei_xin | Original post link

There is no middleware, the business uses k8s pods.

| username: TiDBer_lei_xin | Original post link

Using TiDB’s default value. This value should be quite sufficient.

| username: TiDBer_lei_xin | Original post link

This is also the default value for TiDB, which seems to be 28800, not small.

| username: TiDBer_lei_xin | Original post link

This is not a connection timeout. It should be a connection interruption.

| username: 啦啦啦啦啦 | Original post link

What is the value of CONN_MAX_AGE configured in the Django framework?

| username: TiDBer_pkQ5q1l0 | Original post link

Sometimes it reports “Lost connection.” Is there a regular pattern to this? How often does it report?

| username: TiDBer_lei_xin | Original post link

This CONN_MAX_AGE is 10 minutes.

| username: TiDBer_lei_xin | Original post link

There is no pattern to follow; it appears suddenly. Then it lasts for a few hours and disappears again.

| username: TiDBer_lei_xin | Original post link

Not sure what the issue is, sometimes I wonder if it’s packet loss. But when using MySQL, there’s no problem.

| username: TiDBer_1YkjeKTC | Original post link

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.

| username: TiDBer_lei_xin | Original post link

There is no OOM. We provide OLTP services and have very high requirements for the service.

| username: Raymond | Original post link

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.

| username: TiDBer_lei_xin | Original post link

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.

| username: yilong | Original post link

When an issue occurs, have you checked the tidb.log logs? Are there any special logs?