Overview
The Furniture Store Backend uses MySQL as its database system with SQLAlchemy as the ORM (Object-Relational Mapping) layer. The application uses PyMySQL as the MySQL database driver.MySQL Connection Setup
Prerequisites
Connection String Format
The application constructs the SQLAlchemy database URI automatically from environment variables:Connection String Components
mysql+pymysql - Specifies MySQL database with PyMySQL driverDatabase username for authentication
Database password for authentication
Database server hostname (e.g.,
localhost, 127.0.0.1, or remote host)Database server port (default:
3306)Name of the database to connect to
Example Configurations
SQLAlchemy Configuration
The application uses Flask-SQLAlchemy for database operations. Configuration is defined inconfig.py:
config.py
Configuration Options
SQLALCHEMY_DATABASE_URI
SQLALCHEMY_DATABASE_URI
The database connection string. This is the primary configuration option that tells SQLAlchemy how to connect to your database.Format:
dialect+driver://username:password@host:port/databaseSQLALCHEMY_TRACK_MODIFICATIONS
SQLALCHEMY_TRACK_MODIFICATIONS
Set to
False to disable Flask-SQLAlchemy’s modification tracking system.Why disable it?- Reduces memory overhead
- Improves performance
- The feature is deprecated and will be removed in future versions
This setting is recommended by Flask-SQLAlchemy documentation unless you specifically need the modification tracking feature.
Database Extensions Initialization
Extensions are initialized inapp/extensions.py:
app/extensions.py
app/__init__.py
The
db.init_app() pattern allows you to create the database object separately from the Flask app, enabling the application factory pattern.Database Initialization
Creating Tables
After configuring the database connection, you need to create the database tables. The application uses Flask-Migrate (Alembic) for this:See the Migrations page for detailed information about database migrations.
Manual Table Creation (Not Recommended)
While not recommended for production, you can create tables manually using SQLAlchemy:Testing Database Connection
The application includes a built-in connection test inrun.py:
run.py
Expected Output
Troubleshooting
Connection Refused Error
Connection Refused Error
Error:
Can't connect to MySQL serverSolutions:- Verify MySQL server is running:
sudo systemctl status mysql - Check
DB_HOSTandDB_PORTvalues in.env - Ensure firewall allows connections on port 3306
- For remote connections, verify the host is accessible
Authentication Failed
Authentication Failed
Error:
Access denied for userSolutions:- Verify
DB_USERandDB_PASSWORDare correct - Check user has proper privileges:
SHOW GRANTS FOR 'user'@'host'; - Ensure user is created for correct host (e.g.,
localhostvs%) - Try connecting manually:
mysql -u username -p -h host database
Database Does Not Exist
Database Does Not Exist
Error:
Unknown databaseSolutions:- Verify database exists:
SHOW DATABASES; - Create the database:
CREATE DATABASE database_name; - Check
DB_NAMEin.envmatches the actual database name
PyMySQL Not Installed
PyMySQL Not Installed
Error: Or install from requirements.txt:
No module named 'pymysql'Solution:Character Encoding Issues
Character Encoding Issues
Error: Incorrect string value or encoding errorsSolutions:
- Ensure database uses UTF-8:
ALTER DATABASE dbname CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - Add charset to connection string:
Database Connection Pooling
SQLAlchemy automatically manages connection pooling. For production environments, you may want to configure pool settings:config.py
Number of connections to keep open (default: 5)
Number of seconds after which to recycle connections (prevents timeout issues)
Test connections before using them (prevents “MySQL server has gone away” errors)
Security Best Practices
Use Strong Passwords
Always use strong, unique passwords for database users
Principle of Least Privilege
Grant only necessary permissions to application users
Secure Connections
Use SSL/TLS for remote database connections
Regular Backups
Implement automated database backup strategies
Next Steps
Migrations
Learn how to manage database schema changes with Flask-Migrate
Environment Setup
Configure environment variables and application settings