The “Error Establishing a Database Connection” message means your website cannot connect to its database. As a result, the server cannot retrieve the information required to load your website.

This error commonly occurs on WordPress websites, but it can also affect custom PHP applications and Laravel projects that use MySQL or another database system.

The problem may be caused by incorrect database credentials, a stopped database server, connection limits, corrupted database tables, or server configuration issues.

In this guide, you'll learn how to identify the cause and fix the database connection error step by step.

Why Does the Error Establishing a Database Connection Happen?

Your application needs valid database connection details to communicate with the database server.

If the connection fails, the website cannot retrieve its content or application data.

Common causes include:

  • Incorrect database name, username, or password.
  • Wrong database hostname or port.
  • MySQL or MariaDB service is stopped.
  • Database user does not have the required permissions.
  • Database server has reached its connection limit.
  • Corrupted database tables.
  • Incorrect environment configuration.
  • Database server is unavailable due to hosting or network issues.
  • Recent deployment or configuration changes.

Let's go through the solutions.

1. Check Your Database Credentials

Incorrect database credentials are one of the most common reasons for this error.

Check your application's database configuration and verify that the database name, username, password, and hostname are correct.

For WordPress

Open your WordPress installation's wp-config.php file and locate these settings:

define('DB_NAME', 'your_database_name');
define('DB_USER', 'your_database_user');
define('DB_PASSWORD', 'your_database_password');
define('DB_HOST', 'localhost');

Verify each value against the database details provided by your hosting provider.

Pay particular attention to:

  • Database name.
  • Database username.
  • Database password.
  • Database hostname.

Do not assume the database hostname is always localhost. Some hosting providers use a separate database hostname.

For Laravel

Open your Laravel project's .env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password

Make sure these values match your actual database configuration.

After changing the environment variables, clear Laravel's cached configuration:

php artisan config:clear
php artisan cache:clear

If your application uses cached configuration in production, rebuild it after verifying the settings:

php artisan config:cache

2. Check Whether MySQL or MariaDB Is Running

Your application cannot connect to the database if the database service is stopped.

If you have server access, check the MySQL service status:

sudo systemctl status mysql

On some servers, the service may be named mariadb:

sudo systemctl status mariadb

If the service is stopped, try restarting it:

sudo systemctl restart mysql

For MariaDB:

sudo systemctl restart mariadb

Then reload your website.

If the service fails to start, inspect its logs:

sudo journalctl -u mysql -n 100 --no-pager

For MariaDB:

sudo journalctl -u mariadb -n 100 --no-pager

The logs may reveal configuration errors, disk space problems, or database startup failures.

If you use shared hosting, contact your hosting provider to confirm that the database server is operational.

3. Test the Database Connection Manually

You can test whether your database credentials work independently of your application.

Run the following command from your server:

mysql -h localhost -u your_database_user -p your_database_name

Replace the hostname, username, and database name with your actual values.

Enter your database password when prompted.

If the connection succeeds, your credentials are likely valid and you should investigate your application's configuration.

If you receive an error, check the error message:

  • Access denied: Check the username, password, and user permissions.
  • Unknown database: Verify that the database exists and the name is correct.
  • Can't connect to MySQL server: Check the database service, hostname, port, and network access.

This test helps identify whether the problem is related to the application or the database server.

4. Verify Database User Permissions

Even when your database username and password are correct, the user may not have permission to access the database.

For MySQL, an administrator can inspect the user's grants:

SHOW GRANTS FOR 'your_database_user'@'localhost';

If the required permissions are missing, an authorized database administrator can grant access:

GRANT ALL PRIVILEGES ON your_database_name.*
TO 'your_database_user'@'localhost';

FLUSH PRIVILEGES;

Use the correct database user and host combination.

Avoid granting unnecessary privileges. Applications should receive only the permissions they actually need.

5. Check the Database Host and Port

The database hostname must point to the correct server.

For a database running on the same machine, the hostname might be:

DB_HOST=127.0.0.1

Or:

DB_HOST=localhost

For a remote database, use the hostname supplied by your hosting provider.

The default MySQL port is:

DB_PORT=3306

However, your hosting environment may use a different port.

You can test whether a remote database port is reachable with:

nc -zv database.example.com 3306

Replace the hostname and port with your actual database details.

If the connection fails, check firewall rules, network restrictions, and remote database access settings.

6. Check Whether the Database Has Reached Its Connection Limit

MySQL limits the number of simultaneous client connections.

If too many applications or requests connect at the same time, new connections may fail.

An administrator can inspect the current connection count:

SHOW STATUS LIKE 'Threads_connected';

Check the configured maximum:

SHOW VARIABLES LIKE 'max_connections';

If the server regularly reaches its connection limit, investigate:

  • Applications opening too many database connections.
  • Connections that are not being released.
  • Long-running queries.
  • Traffic spikes.
  • Background workers creating excessive connections.

Increasing max_connections may help in some cases, but it should not replace investigating the underlying cause. Make sure the server has enough memory to handle additional connections.

7. Repair Corrupted WordPress Database Tables

If you use WordPress, corrupted database tables may prevent your website from working correctly.

WordPress provides a database repair feature.

First, back up your database.

Then temporarily add this line to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Visit:

https://yourdomain.com/wp-admin/maint/repair.php

Choose the repair option and follow the instructions.

After the repair is complete, immediately remove the WP_ALLOW_REPAIR line from wp-config.php.

This repair page does not require normal WordPress login authentication, so do not leave it enabled.

If the database is seriously corrupted, restore it from a verified backup or ask your hosting provider for assistance.

8. Check PHP Error Logs

Your PHP error logs may contain useful information about why the application cannot connect to the database.

For WordPress, check your hosting control panel's PHP error logs or the configured server error log.

For Laravel, inspect:

storage/logs/laravel.log

You can also review your web server logs.

For Nginx:

sudo tail -f /var/log/nginx/error.log

For Apache:

sudo tail -f /var/log/apache2/error.log

Look for messages such as:

  • Access denied for user.
  • Connection refused.
  • Unknown database.
  • Too many connections.
  • SQLSTATE connection errors.

These messages can help you identify the exact cause instead of changing database settings randomly.

9. Check Database Configuration After Deployment

If the error started immediately after deploying your website to a new server, reviewing the deployment configuration should be one of your first steps.

Your local database settings may not work in production.

Verify:

  • Production database name.
  • Production database username and password.
  • Database hostname and port.
  • Database user permissions.
  • Remote database access.
  • Environment variables on the production server.

For Laravel, make sure the production .env file exists and contains the correct values.

After updating the configuration, run:

php artisan config:clear
php artisan config:cache

Also confirm that the application is connecting to the intended production database rather than a local or staging database.

10. Contact Your Hosting Provider

Sometimes the problem is outside your application's code.

Your hosting provider may be experiencing:

  • MySQL server downtime.
  • Database server overload.
  • Disk space exhaustion.
  • Database service failures.
  • Network connectivity problems.
  • Database account restrictions.

If your credentials are correct and the database service is unavailable, contact your hosting provider.

Ask them to check database server availability, connection limits, account permissions, and database server logs.

How to Prevent Database Connection Errors

You can reduce the likelihood of database connection problems by following a few good practices:

  • Store database credentials securely in environment variables.
  • Avoid committing production credentials to version control.
  • Monitor database server availability and resource usage.
  • Keep regular database backups.
  • Optimize slow database queries.
  • Close database connections when they are no longer needed.
  • Review database logs regularly.
  • Verify production database settings after deployments.
  • Monitor disk space and database connection limits.

These practices help keep your website stable and make database problems easier to diagnose.

Final Checklist

Before considering the issue fixed, verify the following:

  • Database name is correct.
  • Database username and password are valid.
  • Database hostname and port are correct.
  • MySQL or MariaDB service is running.
  • Database user has the required permissions.
  • Database server accepts new connections.
  • Database tables are not corrupted.
  • Application environment configuration is correct.
  • PHP and application logs do not show unresolved database errors.

Still Getting the Error Establishing a Database Connection?

If your website still displays the Error Establishing a Database Connection message after trying these solutions, the issue may require a deeper investigation of your server, database configuration, or application code.

I can help troubleshoot database connection errors in WordPress, Laravel, and custom PHP applications.

Get Help Fixing Your Website

Share the error message and relevant server logs, and we can identify the cause and work toward a reliable fix.