Question

How to setup Mysql Database Connection on Flask Wsgi Production Server

File "/usr/local/lib/python3.6/dist-packages/flask_sqlalchemy/__init__.py", line 972, in create_engine, referer: http://123.123.123.123/
[Tue Jul 21 12:09:17.446596 2020] [wsgi:error] [pid 2995] [client 117.96.57.241:57794]     return sqlalchemy.create_engine(sa_url, **engine_opts), referer: http://123.123.123.123/
[Tue Jul 21 12:09:17.446600 2020] [wsgi:error] [pid 2995] [client 117.96.57.241:57794]   File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine, referer: http://123.123.123.123/
[Tue Jul 21 12:09:17.446606 2020] [wsgi:error] [pid 2995] [client 117.96.57.241:57794]     return strategy.create(*args, **kwargs), referer: http://123.123.123.123/
[Tue Jul 21 12:09:17.446609 2020] [wsgi:error] [pid 2995] [client 117.96.57.241:57794]   File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/strategies.py", line 87, in create, referer: http://123.123.123.123/dbapi = dialect_cls.dbapi(**dbapi_args), referer: http://123.123.123.123/File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 118, in dbapi, referer: http://123.123.123.123/return __import__("MySQLdb"), referer: http://123.123.123.123/
ModuleNotFoundError: No module named 'MySQLdb', referer: http://123.123.123.123/
[Tue Jul 21 12:09:17.446637 2020] [wsgi:error] [pid 2995] [client 117.96.57.241:57794] , referer: http://123.123.123.123/

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 6, 2023

Hi there,

The error message you are seeing: ModuleNotFoundError: No module named ‘MySQLdb’ indicates that the required MySQL driver for Python is not installed in your environment. You need to install the MySQLdb package (also known as mysqlclient) to interact with a MySQL database using SQLAlchemy.

Here are the steps to set up the MySQL connection in a Flask app running on a WSGI production server:

  1. Install MySQL Driver:

    • Run the following command to install the mysqlclient package:

      pip install mysqlclient
      
  2. Configure Your Flask Application:

    • In your Flask application, you need to set up the database URI to point to your MySQL database:

      from flask import Flask
      from flask_sqlalchemy import SQLAlchemy
      
      app = Flask(__name__)
      app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@localhost/dbname'
      db = SQLAlchemy(app)
      

      Make sure to replace username, password, localhost, and dbname with your actual database credentials and host information.

  3. Configure WSGI:

    • If you are using a WSGI server like mod_wsgi or gunicorn, make sure that it is properly configured to serve your Flask application. You may need to refer to specific documentation depending on the WSGI server you are using.
  4. Restart Your WSGI Server:

    • After installing the required package and configuring your application, restart the WSGI server to apply the changes.
  5. Check Application Logs:

    • If you still encounter issues, check the application logs for detailed error messages that can help you identify the root cause of any problems.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel