Resolving Database Connection Errors


Common Causes

  • Incorrect credentials (username/password).
  • Firewall blocking the database port (e.g., 3306 for MySQL).
  • Database service stopped.

How to Fix:

1. Verify Credentials


// Example WordPress config (wp-config.php)
define('DB_NAME', 'database_name');
define('DB_USER', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
    

2. Check Database Service Status

For MySQL:


sudo systemctl status mysql  # Check status
sudo systemctl start mysql   # Start service
    

3. Allow Remote Connections

Edit my.cnf (MySQL):


bind-address = 0.0.0.0  # Allow all IPs
    

4. Test Connection with CLI


mysql -u username -p -h localhost  # MySQL
psql -U username -h localhost -d dbname  # PostgreSQL
    

Prevention Tips

  • Use strong passwords and restrict user privileges.
  • Monitor uptime with tools like Pingdom.
  • Regularly backup databases (e.g., mysqldump).
Note: For cloud databases (e.g., AWS RDS), ensure security groups allow inbound traffic.

Did you find this article useful?