Overview
CompuTécnicos uses MySQL with PDO for database connectivity. The database configuration is centralized inconfig/database.php and uses environment variables for flexibility across different environments.
Connection Parameters
The following parameters control your database connection:Database server hostname or IP address. Use
localhost for local development or your database server IP for remote connections.The name of the MySQL database to connect to. This database must exist before running the application.
MySQL username with appropriate permissions to access the database.
Password for the database user.
MySQL server port. The standard MySQL port is 3306.
PDO Configuration
The database connection uses the following PDO options for optimal performance and security:- ATTR_ERRMODE: Set to
PDO::ERRMODE_EXCEPTIONto throw exceptions on database errors - ATTR_DEFAULT_FETCH_MODE: Set to
PDO::FETCH_ASSOCfor associative array results - ATTR_PERSISTENT: Enabled to reuse connections and reduce latency with remote databases
- ATTR_EMULATE_PREPARES: Disabled to use native prepared statements for better security
- MYSQL_ATTR_FOUND_ROWS: Enabled to return the number of found rows instead of affected rows
Configuration Example
Set these variables in your.env file:
Docker Configuration
When using Docker, additional environment variables are available:Root password for the MySQL container. Required when initializing the MySQL Docker container.
Docker Ports
The port to expose MySQL on the host machine.
Port for phpMyAdmin web interface to manage your database.
Connection String
The application constructs the following PDO connection string:utf8mb4 charset is used to support full Unicode, including emojis and special characters.
Error Handling
If the database connection fails, the application will terminate with an error message:Security Best Practices
Use Strong Passwords
Always use strong, unique passwords for database users. Avoid default passwords in production.
Limit Database Permissions
Create a dedicated database user with only the necessary permissions. Avoid using the root user.
Use Environment Variables
Never hardcode database credentials in your code. Always use environment variables.
Secure the .env File
Ensure your
.env file is not committed to version control and has restricted file permissions (chmod 600).Troubleshooting
Connection Refused
If you see “Connection refused” errors:- Verify MySQL is running:
sudo systemctl status mysql - Check that the port is correct (default is 3306)
- Ensure firewall rules allow the connection
Access Denied
If you see “Access denied” errors:- Verify the username and password are correct
- Check that the user has permissions on the database: