Configuration Guide
Before running Muebles Roble, you need to configure the database connection and set up environment variables. This guide walks you through the complete configuration process.All sensitive configuration data is stored in environment variables using a
.env file, never hard-coded in the application.Environment Variables Setup
Locate the Template File
The project includes a
.env-template file that serves as a reference for all required environment variables:Create Your .env File
Copy the template to create your actual environment file:On Windows (Command Prompt):On Windows (PowerShell):
Configure Database Variables
Open the Replace the values with your actual MySQL credentials.
.env file in your text editor and update the database credentials:Environment Variables Reference
Flask Variables
| Variable | Required | Default | Description |
|---|---|---|---|
FLASK_APP | No | app.py | Entry point for Flask CLI commands |
FLASK_ENV | No | production | Environment mode: development or production |
SECRET_KEY | Production only | dev-secret-key... | Secret key for session management and CSRF protection |
Database Variables
| Variable | Required | Example | Description |
|---|---|---|---|
DB_USER | Yes | root | MySQL database username |
DB_PASSWORD | Yes | mypassword | MySQL database password |
DB_HOST | Yes | localhost | Database server hostname or IP |
DB_PORT | Yes | 3306 | MySQL server port (default: 3306) |
DB_NAME | Yes | muebles_roble_db | Name of the database to use |
Configuration File Structure
Theconfig.py file loads and processes these environment variables:
How It Works
1. Loading Environment Variables
1. Loading Environment Variables
The
load_dotenv() function reads your .env file and loads all variables into os.environ.2. Constructing Database URI
2. Constructing Database URI
SQLAlchemy requires a connection string in this format:The config builds this from individual environment variables.
3. Secret Key Safety
3. Secret Key Safety
In production (
FLASK_ENV=production), the app will crash if SECRET_KEY is not set. This prevents running production with insecure defaults.4. Configuration Loading
4. Configuration Loading
The Flask app factory loads this configuration:
Database Setup
Install MySQL Server
Ensure MySQL is installed and running on your system:
- Windows: MySQL Installer
- macOS:
brew install mysqlor MySQL DMG - Linux:
sudo apt-get install mysql-server(Ubuntu/Debian)
Create Database
Connect to MySQL and create the database:Enter your MySQL root password, then:Verify the database was created:Exit MySQL:
Create Database User (Optional)
For better security, create a dedicated user instead of using root:Update your
.env file:Database Connection String Examples
Here are example connection strings for different scenarios:Local MySQL (Standard)
Remote MySQL Server
Custom Port
Running Database Migrations
After configuring the database, you’ll need to create the tables:Initialize Migrations
If migrations haven’t been initialized yet:This creates a
migrations/ folder.This step may already be done in the repository. Only run it if the
migrations/ folder doesn’t exist.Create Initial Migration
Generate a migration from your models:This analyzes your models and creates migration scripts in
migrations/versions/.Apply Migrations
Apply the migration to create tables:This executes the migration scripts and creates all tables in your database.
Environment-Specific Configuration
Development Environment
For local development, use these settings:Development mode enables debug mode, auto-reloading, and detailed error pages.
Production Environment
For production deployment:Troubleshooting Configuration Issues
Database connection fails
Database connection fails
Error:
Database connection failed: Access deniedSolutions:- Verify
DB_USERandDB_PASSWORDare correct - Check that MySQL is running:
mysql -u root -p - Ensure the user has permissions:
SHOW GRANTS FOR 'user'@'localhost'; - Verify the database exists:
SHOW DATABASES;
Can't connect to MySQL server
Can't connect to MySQL server
Error:
Can't connect to MySQL server on 'localhost'Solutions:- Check MySQL is running:
systemctl status mysql(Linux) orbrew services list(macOS) - Verify
DB_HOSTandDB_PORTare correct - Check firewall settings if connecting to a remote server
- Try connecting manually:
mysql -h localhost -P 3306 -u root -p
Environment variables not loading
Environment variables not loading
Error: Variables are
None or using defaultsSolutions:- Ensure
.envfile is in the project root (same directory asrun.py) - Check file is named
.envexactly (not.env.txt) - Verify
python-dotenvis installed:pip list | grep dotenv - Add print statement to verify:
print(os.getenv("DB_USER"))
SECRET_KEY error in production
SECRET_KEY error in production
Error:
ValueError: SECRET_KEY must be set in production environmentSolutions:- Add
SECRET_KEYto your.envfile - Generate a secure key:
python -c "import secrets; print(secrets.token_hex(32))" - Ensure
FLASK_ENV=productionis set correctly
Migration errors
Migration errors
Error:
Target database is not up to dateSolutions:- Run migrations:
flask db upgrade - Check migration status:
flask db current - View migration history:
flask db history - If needed, downgrade and reapply:
flask db downgradethenflask db upgrade
Security Best Practices
- Never commit
.envfiles - Add.envto.gitignore - Use strong passwords - Generate random passwords for database users
- Set SECRET_KEY - Use a cryptographically random string
- Restrict database users - Grant only necessary permissions
- Use environment variables - Never hard-code credentials
- Enable SSL - Use encrypted database connections in production
- Regular backups - Implement automated database backups
Verifying Configuration
You can verify your configuration is correct:Next Steps
First Steps
Run the application and create your first catalog entries
Features
Learn about all available features and capabilities