Describe the bug
I have some scheduled tasks, get the connection operation database through the connection pool, and perform the task operation database again to report MySQL 2006 and MySQL2013 error.
my setting:
DATABASES = {
'default': {
'ENGINE': 'dj_db_conn_pool.backends.mysql',
'NAME': SQL_DB,
'USER': SQL_USER,
'HOST': SQL_HOST,
'POOL_OPTIONS': {
'POOL_SIZE': 5,
'MAX_OVERFLOW': -1,
'RECYCLE': 60
}
},
}
Environment
- Python Version: 3.7
- Django Version: 2.2.4
Traceback
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "", line 1, in
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 263, in repr
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 269, in len
self._fetch_all()
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 53, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 73, in execute
return self.cursor.execute(query, args)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/MySQLdb/cursors.py", line 319, in _query
db.query(q)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/MySQLdb/connections.py", line 259, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (2006, 'MySQL server has gone away')
Traceback (most recent call last):
File "", line 1, in
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 263, in repr
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 269, in len
self._fetch_all()
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/query.py", line 53, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/utils.py", line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 73, in execute
return self.cursor.execute(query, args)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/MySQLdb/cursors.py", line 206, in execute
res = self._query(query)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/MySQLdb/cursors.py", line 319, in _query
db.query(q)
File "/Users/admin/Desktop/code/virtualenv/mbot/lib/python3.9/site-packages/MySQLdb/connections.py", line 259, in query
_mysql.connection.query(self, query)
django.db.utils.OperationalError: (2013, 'Lost connection to MySQL server during query')
Due to network error?
No, I don't know how connection pooling keeps connections active and how to allocate valid connections.
I'm guessing there's a problem with the connection pool allocation that caused the problem above.