How to fix “error establishing a database connection” in MySQL/MariaDB
The “error establishing a database connection” occurs when MySQL or MariaDB cannot establish a connection between the application and the database. This is usually due to incorrect credentials, a stopped database server, or configuration issues.
What does the message “error establishing a database connection” mean?
The message “error establishing a database connection” appears when an application — for example, a PHP website — cannot connect to a MySQL or MariaDB database. This issue is often caused by incorrect login credentials such as a wrong username or password, a database server that is not running, or misconfigured server settings. Network-related problems like blocked ports or incorrect host information can also trigger the error. In content management systems such as WordPress, this message is displayed immediately if the database is unreachable. Because no data can be loaded or saved, it usually prevents access to the entire website.
How to fix “error establishing a database connection” 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 connection 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 mysqlThis 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.

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 mariadbMake sure to use the correct username and password. These credentials are set when you create the MySQL database. Additionally, you must configure the correct hostname.
Solution 2: Check the database connection
Once you know that MySQL/MariaDB is running, the next step is to connect from the command line using the command:
mysql -u root -pYou 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.
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 establishing a MySQL connection 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 credentials, 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.
- Sell on social media in minutes
- Manage it all from one platform
- Works with any product or service
Therefore, it is worthwhile to check if you are using the correct spelling and case sensitivity for the username. To list all MySQL/MariaDB users, log in to the command-line client as root using the command:
mysql -u root -pNext, 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. Additionally, the Host column shows the location from which the user is allowed to connect.
Solution 4: Reset the MySQL/MariaDB user password
Particular caution is required when resetting the root user’s password. Pay close attention when following the tips below to avoid accidentally deleting the wrong password.
If the spelling of the username wasn’t the issue, the “error establishing a database connection” 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 -pNext, reset the password of the respective 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 permissions
If you are certain that the username and password are correct, the issue might be that the user does not have the correct permissions (grants) for this database.
To check a user’s permissions (grants), log in to the command line client as root with the command:
mysql -u root -pNext, display the user’s permissions 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 privileges that the user has for the respective 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 permissions for a database named reviews.
If no permissions 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 permissions on “.” and no permissions for databases. This may cause the access error.
Solution 6: Check the database connection from the web
Even if MySQL or MariaDB are running locally, issues can still occur in the web environment. A simple browser-based test can help verify the connection and detect potential access problems early. Use a small script to check whether your database accepts the connection. 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 connections.
If you encounter issues connecting 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 corresponding password.
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 credentials are accurate, a firewall may still block the connection. By default, MySQL uses port 3306 for incoming connections. 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 statusIf the firewall is blocking the port, you can specifically 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 --reloadMake 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 accordingly, your application should be able to establish a connection to the MySQL/MariaDB database again. Verify this using the PHP test script or directly via the console.

