A beginner-friendly FastAPI project that connects to a MySQL database running locally via XAMPP.
This project demonstrates how to build and structure a simple CRUD API with connection pooling, environment variables, and clean modular design.
📦 fastapi-mysql-xampp
├── main.py # FastAPI entry point (endpoints and app startup)
├── database.py # Database connection pool and table initialization
├── config.py # Loads environment variables (from .env)
├── requirements.txt # All project dependencies
└── .env # MySQL credentials (NOT uploaded to GitHub)
git clone https://github.com/<your-username>/fastapi-mysql-xampp.git
cd fastapi-mysql-xampppython -m venv env
# Activate it:
# Windows:
env\Scripts\activate
# macOS / Linux:
source env/bin/activatepip install -r requirements.txtCreate a file named .env in the root folder and add your MySQL credentials:
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_USER=your_user
MYSQL_PASSWORD=your_password
MYSQL_DB=your_database💡 Note:
- The
.envfile is intentionally excluded from GitHub using.gitignore. - You can find these credentials in your XAMPP/phpMyAdmin setup.
- Open XAMPP Control Panel.
- Start MySQL service.
- Make sure your database (
your_database) exists in phpMyAdmin.
Start your server using Uvicorn:
uvicorn main:app --reloadThen open your browser and visit:
📄 Swagger Docs → http://127.0.0.1:8000/docs
🧾 ReDoc → http://127.0.0.1:8000/redoc
POST /users/
Request body:
{
"name": "Attractor",
"age": 25
}Response:
{
"id": 1,
"name": "Attractor",
"age": 25
}| Tool | Purpose |
|---|---|
| FastAPI | Modern Python web framework |
| Uvicorn | ASGI server for FastAPI |
| MySQL Connector | Connects Python to MySQL |
| python-dotenv | Loads .env environment variables |
| XAMPP | Local MySQL + phpMyAdmin server |
✅ MySQL connection pooling using mysql.connector.pooling
✅ Environment variable management via .env
✅ CRUD-ready structure
✅ Automatic table initialization at startup
✅ Swagger UI (/docs) for interactive testing
- Add full CRUD endpoints (read, update, delete)
- Switch to SQLAlchemy ORM for cleaner queries
- Add Alembic for database migrations
- Implement error handling and validation middleware
- Dockerize for easy deployment
- Do NOT upload your
.envfile (it contains sensitive credentials). - Always close database connections properly.
- Use async drivers like
aiomysqlfor high-concurrency production systems.