Application Crashes on Deployment - ModuleNotFoundError for MySQLdb

antonioc-clPRO

3 months ago

I am facing an issue when deploying my Flask application on Railway. The app fails to start, and I receive the following error:

ModuleNotFoundError: No module named 'MySQLdb'


[2025-02-04 16:27:43 +0000] [6] [INFO] Worker exiting (pid: 6)

[2025-02-04 16:27:43 +0000] [1] [ERROR] Worker (pid:4) exited with code 3

[2025-02-04 16:27:43 +0000] [1] [ERROR] Worker (pid:5) was sent SIGTERM!

[2025-02-04 16:27:43 +0000] [1] [ERROR] Worker (pid:6) was sent SIGTERM!

[2025-02-04 16:27:43 +0000] [1] [ERROR] Worker (pid:7) was sent SIGTERM!

[2025-02-04 16:27:43 +0000] [1] [ERROR] Shutting down: Master

[2025-02-04 16:27:43 +0000] [1] [ERROR] Reason: Worker failed to boot.

The full stack trace shows that SQLAlchemy is trying to use the MySQLdb driver, despite having PyMySQL installed and specified in my requirements.txt.

Environment Setup

  • Tech stack:

  • Flask (version: 2.3.3)

  • Flask-SQLAlchemy (version: 3.1.1)

  • Gunicorn (version: 23.0.0)

  • PyMySQL (version: 1.1.0)

  • SQLAlchemy (version: 2.0.37)

  • Project structure:

├── backend
│   └── app.py (Flask app definition)
├── main.py (entry point, imports app from backend.app)
├── requirements.txt
└── nixpacks.toml
  • Current DATABASE_URL: The environment variable DATABASE_URL is set in Railway's settings but does not specify the pymysql driver.

Steps Taken to Fix

  1. Updated `DATABASE_URL

  2. Verified that PyMySQL is installed via requirements.txt.

  3. Updated the Gunicorn command in the custom start command:

   gunicorn backend.app:app --workers 4 --bind 0.0.0.0:$PORT
  1. Tested locally, and the app works without issues.

Could you confirm whether Railway supports mysql+pymysql?
Is there anything else I need to configure in the deployment process to resolve this issue?
Any suggestions for further debugging?

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

Solved

2 Replies

3 months ago

Hello,

You have PyMySQL in your requirements.txt file, but you aren't using it anywhere in your code, instead you are using SQLAlchemy

Please make sure you are using only PyMySQL in your code.


antonioc-clPRO

3 months ago

Thanks for your help as always, Brody.


Status changed to Solved brody 3 months ago


Application Crashes on Deployment - ModuleNotFoundError for MySQLdb - Railway Help Station