The ‘error es­tab­lish­ing a database con­nec­tion’ occurs when MySQL or MariaDB cannot establish a con­nec­tion between the ap­plic­a­tion and the database. This is usually due to incorrect cre­den­tials, a stopped database server, or con­fig­ur­a­tion issues.

What does the message ‘error es­tab­lish­ing a database con­nec­tion’ mean?

The message ‘error es­tab­lish­ing a database con­nec­tion’ appears when an ap­plic­a­tion — for example, a PHP website — cannot connect to a MySQL or MariaDB database. This issue is often caused by incorrect login cre­den­tials such as a wrong username or password, a database server that is not running, or mis­con­figured server settings. Network-related problems like blocked ports or incorrect host in­form­a­tion can also trigger the error. In content man­age­ment systems such as WordPress, this message is displayed im­me­di­ately if the database is un­reach­able. Because no data can be loaded or saved, it usually prevents access to the entire website.

How to fix ‘error es­tab­lish­ing a database con­nec­tion’ in MySQL/MariaDB

If you can’t connect to your database, several issues could be to blame. Work through the steps below to identify why the con­nec­tion is failing in your case and how to fix it.

Solution 1: Ensure MySQL/MariaDB is running

If MySQL or MariaDB is no longer running, scripts and websites will no longer be able to connect. On a Linux server, you can use the following command to check if MySQL is running:

ps -aux | grep mysql

This returns a list of running processes with ‘mysql’ in the name. The list includes the command you just executed. If MySQL is running, it will also include the MySQL process.

Image: MySQL process
From the output, you can determine whether MySQL/MariaDB is running.

If MySQL/MariaDB is not running, you will only see your grep command listed.

Start MySQL/MariaDB with the following commands:

# Ubuntu
systemctl start mysql
systemctl start mariadb
# CentOS
systemctl start mysqld
systemctl start mariadb

Make sure to use the correct username and password. These cre­den­tials are set when you create the MySQL database. Ad­di­tion­ally, you must configure the correct hostname.

Solution 2: Check the database con­nec­tion

Once you know that MySQL/MariaDB is running, the next step is to connect from the command line using the command:

mysql -u root -p

You should be prompted to enter the password for the root user. By default, the password for root access is the same as the original root password for your server.

Note

If you change the password for the root server user, the password for the MySQL root user will not be changed.

After entering the password, you should be in the MySQL/MariaDB client. For example, a MySQL client looks like this:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 9.4.0 Homebrew
Copyright (c) 2000, 2025, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

If you encounter issues with es­tab­lish­ing a MySQL con­nec­tion using a different username and password, run this test a second time and replace root with the other username.

Solution 3: Check the MySQL/MariaDB username

If you enter the wrong login cre­den­tials, you will receive an error like this:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Make sure that both the username shown in the request and the password you entered are spelled correctly. Note that the username is case-sensitive — Root is not the same as root.

Therefore, it is worth­while to check if you are using the correct spelling and case sens­it­iv­ity for the username. To list all MySQL/MariaDB users, log in to the command-line client as root using the command:

mysql -u root -p

Next, list all MySQL users and their hosts with the command:

select host, user from mysql.user;

This will display the username exactly as it was created. Ad­di­tion­ally, the Host column shows the location from which the user is allowed to connect.

Solution 4: Reset the MySQL/MariaDB user password

Note

Par­tic­u­lar caution is required when resetting the root user’s password. Pay close attention when following the tips below to avoid ac­ci­dent­ally deleting the wrong password.

If the spelling of the username wasn’t the issue, the ‘error es­tab­lish­ing a database con­nec­tion’ message could be due to an invalid password. To reset the MySQL/MariaDB user’s password, log in as root using the command line client:

mysql -u root -p

Next, reset the password of the re­spect­ive user with the command:

ALTER USER 'myuser'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;

Replace new_password with the new password and myuser with the username.

Solution 5: Ensure that the user has the correct per­mis­sions

If you are certain that the username and password are correct, the issue might be that the user does not have the correct per­mis­sions (grants) for this database.

To check a user’s per­mis­sions (grants), log in to the command line client as root with the command:

mysql -u root -p

Next, display the user’s per­mis­sions with the command:

SHOW GRANTS FOR 'myuser'@'localhost';

Replace myuser with the username. If necessary, change localhost to the hostname.

You should receive a list of priv­ileges that the user has for the re­spect­ive database. It should look something like this:

+-------------------------------------------------------------+
| Grants for myuser@localhost                                 |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO `myuser`@`localhost`                  |
| GRANT ALL PRIVILEGES ON `reviews`.* TO `myuser`@`localhost` |
+-------------------------------------------------------------+
2 rows in set (0,004 sec)

Note that this user has all per­mis­sions for a database named reviews.

If no per­mis­sions have been assigned to the user for a database, you will see something like this:

+--------------------------------------------+
| Grants for myuser@localhost                |
+--------------------------------------------+
| GRANT USAGE ON *.* TO `myuser`@`localhost` |
+--------------------------------------------+
1 row in set (0,001 sec)

Note that this user only has USAGE per­mis­sions on ‘.’ and no per­mis­sions for databases. This may cause the access error.

Solution 6: Check the database con­nec­tion from the web

Even if MySQL or MariaDB are running locally, issues can still occur in the web en­vir­on­ment. A simple browser-based test can help verify the con­nec­tion and detect potential access problems early. Use a small script to check whether your database accepts the con­nec­tion. Create a file named database-connection-test.php with the following content:

<?php
$test_connect = mysqli_connect('localhost', 'root', 'password');
if (!$test_connect) {
    die('Could not connect: ' . mysqli_connect_error());
}
echo 'Successful database connection.';
mysqli_close($test_connect);
?>

Replace the word password with the root password.

Place this file in your web directory and open it in a browser. If you can connect using the root username and password, your database is accepting con­nec­tions.

If you encounter issues con­nect­ing to MySQL/MariaDB with a different username and password, run this test again — this time replacing root with the other username and password with the cor­res­pond­ing password.

Note

Be sure to delete this file once you have completed your tests.

Solution 7: Check firewall and ports

Even if MySQL or MariaDB are running correctly and the cre­den­tials are accurate, a firewall may still block the con­nec­tion. By default, MySQL uses port 3306 for incoming con­nec­tions. First, check if the port is open. On a Linux system, you can use the following commands:

sudo netstat -tulnp | grep 3306
# On systems with ufw (Ubuntu)
sudo ufw status

If the firewall is blocking the port, you can spe­cific­ally allow it:

# Ubuntu with ufw
sudo ufw allow 3306/tcp
sudo ufw reload
# CentOS or RHEL with firewalld
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload

Make sure to allow the port only for IP addresses that actually need access. Opening it to all traffic can pose a security risk. Once the firewall has been adjusted ac­cord­ingly, your ap­plic­a­tion should be able to establish a con­nec­tion to the MySQL/MariaDB database again. Verify this using the PHP test script or directly via the console.

Go to Main Menu