I have a Django (v2.2.23) application connected to a MySQL (v5.7) database. Every now and then, for a few minutes at a time, all my DB queries start to fail with one of the following errors: "Unknown MySQL Error" (error code 2000), "InterfaceError", or "Malformed Packet" (error code 2027). The errors resolve themselves on their own after a few minutes, but then they pop up again a some time later (on average, every few hours). These errors happen seemingly at random and I haven't found any reliable way to reproduce them till now. There is nothing wrong with the code as far as I can see because when the queries do execute, they work as expected and give the correct results.
I have already made several attempts to fix the issue to no avail, including :-
- Following all the steps given in this article
- Increasing the value of connect_timeout and wait_timeout
- Changing the charset used in the table in my DB (tried latin1 and utf8)
- Upgrading the Python mysqlclient package to the latest version (2.0.3)
This error is occuring both on local and staging environments.
In order to debug this further, I create a custom exception handler and logged the error as well as the last 3 SQL queries executed (which I got using connection.queries[-3:]
).
The result is given below:
'EXCEPTION': InterfaceError(0, ''), 'LAST_QUERIES': [{'sql': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED', 'time': '0.000'}, {'sql': None, 'time': '0.000'}]
Notice that for the second query, the value of 'sql' is coming as None. Is Django trying to execute a null query? Could this be the cause of the strange, intermittent errors?