Getting "MySQL server has gone away" error on cPanel-hosted Django site

I'm hosting a Django project on a shared server using cPanel, and I’ve been running into a frustrating issue lately.

On several pages across the site, I get a 500 Internal Server Error. After checking Sentry, I consistently see this error:

Level: Error  
(2006, "MySQL server has gone away (ConnectionResetError(104, 'Connection reset by peer'))")

This happens randomly but frequently, and seems to occur on any page that touches the database.

What I’ve Tried

  • Set CONN_MAX_AGE = 600 to persist DB connections

  • Increased connect_timeout

  • Searched online extensively and even tried ChatGPT suggestions — no luck so far

No access to MySQL logs or server configs (shared cPanel hosting)

My current DATABASES config:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '***',
        'USER': '***',
        'PASSWORD': '***', 
        'HOST': 'localhost',
        'PORT': '3306',
        'CONN_MAX_AGE': 600, 
        'OPTIONS': {
            'charset': 'utf8mb4',
            'connect_timeout': 10,
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"
        }
    }
}

What I Need Help With

  • Is this a MySQL timeout issue or something related to cPanel's MySQL limits?
  • Any way to better handle this within Django, given my hosting limitations?
  • Should I ask my host to change MySQL configs (e.g., wait_timeout, max_allowed_packet)?
Вернуться на верх