Flask API + MySQL on Render β README
This project is a simple Flask API that connects to a MySQL database. It supports full CRUD operations for a students table.
Your file structure should look like this:
.
βββ app.py
βββ .env
βββ .gitignore
βββ requirements.txt
Create a .env file to store your sensitive credentials. Example:
DB_HOST=tcp.ap-northeast-1.clawcloudrun.com
DB_USER=root
DB_PASSWORD=Unitec123
DB_NAME=iscg7444
DB_PORT=40910
β οΈ Never commit this file to version control!
Create a .gitignore file and include:
.venv
.env
Replace hardcoded config with this:
import os
from dotenv import load_dotenv
load_dotenv()
db_config = {{
'host': os.getenv('DB_HOST'),
'user': os.getenv('DB_USER'),
'password': os.getenv('DB_PASSWORD'),
'database': os.getenv('DB_NAME'),
'port': os.getenv('DB_PORT')
}}Install packages and freeze:
pip install flask flask-cors python-dotenv mysql-connector-python
pip freeze > requirements.txt- Go to https://render.com and create a Web Service.
- Connect your GitHub repository.
- Choose Python environment.
- Set start command to:
gunicorn app:app
- Add environmental variables based on your
.envfile:
DB_HOST
DB_USER
DB_PASSWORD
DB_NAME
DB_PORT
- Click Deploy.
β Done! Your Flask API with MySQL is now running on Render.