I'm running a Python Pyramid app on a CentOS server using uWSGI and nginx. I'm using SQLAlchemy as an ORM, MySQLdb as the API, and MySQL as a database. The site hasn't gone live yet, so the only traffic is me and some other employees of the company. We purchased some data to populate the database, so the largest (and most frequently queried) table is ~150,000 rows.
Yesterday I opened four new tabs of the website in quick succession, and I got back a couple 502 Bad Gateway errors. I looked in the uWSGI log, and found the following:
sqlalchemy.exc.OperationalError: (OperationalError) (2006, 'MySQL server has gone away') 'SELECT ge...
Important note: This error is not due to MySQL's wait_timeout. Been there, done that.
I wondered if the issue was caused by concurrent requests being served simultaneously. I made myself a poor man's load tester:
for i in {1..10}; do (curl -o /dev/null http://domain.com &); done;
Sure enough, within those ten requests at least one would throw a 2006 error, oftentimes more. Sometimes the errors would get even stranger, for example:
sqlalchemy.exc.NoSuchColumnError: "Could not locate column in row for column 'table.id'"
When the column most definitely exists and worked fine on all the other identical requests. Or, this one:
sqlalchemy.exc.ResourceClosedError: This result object does not return rows. It has been closed automatically.
When, once again, it worked fine for all the other requests.
To further verify that the problem stemmed from concurrent database connections, I set uWSGI to a single worker and multi-threading disabled, forcing the requests to be processed one at a time. Sure enough, the problems disappeared.
In an attempt to find the problem, I set up an error log for MySQL. With the exception of some notices during MySQL starting up, it remains empty.
Here is my MySQL config:
[mysqld]
default-storage-engine = myisam
key_buffer = 1M
query_cache_size = 1M
query_cache_limit = 128k
max_connections=25
thread_cache=1
skip-innodb
query_cache_min_res_unit=0
tmp_table_size = 1M
max_heap_table_size = 1M
table_cache=256
concurrent_insert=2
max_allowed_packet = 1M
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
innodb_file_per_table=1
log-error=/var/log/mysql/error.log
Heavy Googling on the error revealed little, but suggested that I increase max_allowed_packet. I increased it to 100M and restarted MySQL, but that didn't help at all.
To summarize: Concurrent connections to MySQL cause 2006, 'MySQL server has gone away'
and some other strange errors. There is nothing of relevance in MySQL's error log.
I have been working at this for hours and haven't made any progress. Can somebody help me out?