Step 1: Create a MySQL Database on cPanel
- Log in to your cPanel account.
- Go to MySQL® Databases.
- Under Create New Database, enter a database name (e.g.,
flaskdb
) and click Create Database. - Scroll down to MySQL Users and create a new user with a strong password.
- Under Add User to Database, select the user and database you just created, then click Add.
- 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:
- Open cPanel and navigate to Terminal.
- Activate your Python virtual environment if you have one: (Adjust the path based on your Python version and environment name
- Install
mysql-connector-python
orPyMySQL
: OR
Step 3: Update Your Flask Application
Modify your Flask application to use MySQL instead of SQLite.
-
Edit
config.py
orapp.py
and update the database URI: -
Replace SQLite references in your models (
models.py
):
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. ????