Step 1: Create a MySQL Database on cPanel

  1. Log in to your cPanel account.
  2. Go to MySQL® Databases.
  3. Under Create New Database, enter a database name (e.g., flaskdb) and click Create Database.
  4. Scroll down to MySQL Users and create a new user with a strong password.
  5. Under Add User to Database, select the user and database you just created, then click Add.
  6. Grant ALL PRIVILEGES to the user.

For a step-by-step guide on creating a MySQL database in cPanel, follow this illustrated tutorial: How to Set Up a MySQL Database & User in cPanel (2 Easy Methods).

Step 2: Install MySQL Connector in Your Flask Environment

Since cPanel supports Python virtual environments, do the following:

  1. Open cPanel and navigate to Terminal.
  2. Activate your Python virtual environment if you have one:
    source /home/your-cpanel-username/virtualenv/your-env-name/3.9/bin/activate
    (Adjust the path based on your Python version and environment name
  3. Install mysql-connector-python or PyMySQL:
    pip install mysql-connector-python
    OR
    pip install PyMySQL

Step 3: Update Your Flask Application

Modify your Flask application to use MySQL instead of SQLite.

  1. Edit config.py or app.py and update the database URI:

    from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://your_db_user:your_db_password@localhost/your_db_name' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app)
  2. Replace SQLite references in your models (models.py):

    from app import db class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) email = db.Column(db.String(100), unique=True, nullable=False)

Step 4: Apply Database Migrations

If you are using Flask-Migrate, initialize and migrate the MySQL database.

Final Notes

  • Review stderr.log in the Flask application project folder.

That’s it! Your Flask app is now using MySQL instead of SQLite3. ????

Was this answer helpful? 0 Users Found This Useful (0 Votes)