AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

MySQL/MariaDB: “Error connecting to database”

PARTAGEZ

The “Error connecting to database” error occurs when MySQL or MariaDB fails to establish the connection between the application and the database. It most often results from incorrect credentials, a downed database server, or a configuration error.

Error connecting to the database: what does this message mean?

The message “Error connecting to database” (Error Establishing a Database Connection) means that an application, for example a PHP website, failed to establish a connection with the MySQL or MariaDB database. The most common causes are invalid credentials, for example an incorrect username or password, a downed database server, or a bad server configuration. Network issues, such as blocked ports or the wrong host address, can also trigger this message. In content management systems like WordPress, the error is displayed directly if the database is inaccessible. In this case, access to the entire website is usually blocked, because neither loading nor saving of the contents is possible.

If you cannot connect to your database, there may be several causes. The following solutions describe the steps to check to identify the cause of the connection failure in your situation.

Solution 1: Make sure MySQL/MariaDB is working correctly

If MySQL or MariaDB is stopped, scripts and web pages can no longer establish a connection. On a Linux server, you can check if MySQL is running with the following command:

The command returns a list of active processes that contain « mysql » in their name. This list also includes the command you just executed. If MySQL is running, the MySQL process appears there.

Image: Screenshot of MySQL process
You can check in the output if MySQL/MariaDB is running.

If MySQL/MariaDB is not running, only your grep command is displayed.

Run only the command corresponding to your service (MySQL or MariaDB) with the following commands:

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

Make sure you use the correct username and password. These identifiers were defined when creating the MySQL database. Also verify that the hostname is configured correctly.

Solution 2: Test the connection to the MySQL/MariaDB database

Once you have confirmed that MySQL/MariaDB is working, the next step is to test the connection from the command line with the following command:

You will be prompted to enter the password for the root user. By default, this password matches your server's initial root password.

Note

If you change the server root password, it does not change the MySQL root password.

After entering the password, you access the MySQL/MariaDB client. This looks, for example, as follows:

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 are unable to establish a MySQL connection with a different username or password, repeat the test replacing root with the affected username.

Solution 3: Control MySQL/MariaDB login credentials

If the credentials entered are incorrect, an error message similar to this is displayed:

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

Make sure the username displayed in the query, as well as the password you entered, are spelled correctly. Keep in mind that MySQL is case-sensitive: “Root” is not the same as “root”.

Buy button for social networks

Convert your followers into buyers

  • Sell ​​on social media in a snap
  • Manage everything from a single interface
  • Offer all types of products and services for sale

So carefully check the spelling and case of the username. To view the full list of MySQL/MariaDB users, log in as root in the command line client with the following command:

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

SELECT host, user FROM mysql.user;

The command displays the username exactly as it was created. The Host column further indicates where this user is allowed to connect from.

Solution 4: Reset a MySQL/MariaDB User's Password

Note

Be especially careful when resetting the root user's password. Follow the instructions below carefully to avoid changing or removing the wrong password.

If the problem is not with the spelling of the username, it may be related to an invalid password. To reset a user's MySQL/MariaDB password, log in to the command-line client as root using the following command:

Then, reset the password of the affected user with the command:

ALTER USER 'NomUtilisateur'@'localhost' IDENTIFIED BY 'NouveauMotDePasse';
FLUSH PRIVILEGES;

Replace NouveauMotDePasse with the new password and NomUtilisateur by username.

Solution 5: Check MySQL/MariaDB User Permissions

If you are sure that the username and password are correct, the problem may be that the user does not have the correct permissions (grants) for this database.

To check a user's permissions, log in to the command line client as root with the command:

Next, display the user's permissions with the command:

SHOW GRANTS FOR 'NomUtilisateur'@'localhost';

Replace NomUtilisateur by username. Change if necessary localhost in the hostname. You should get a list of privileges that the user has for the respective database. It should look approximately like this:

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

Note that this user has full permissions for a database called reviews.

If the user has not been assigned any permissions for a database, you will only see something like this:

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

Note that this user only has USAGE permissions on . and has no access rights to the databases. This may be causing the access error.

Solution 6: Test MySQL/MariaDB connection from web browser

Even if MySQL or MariaDB works locally, problems may appear in the web environment. A quick test from the browser allows you to check the connection and detect possible access errors from the start. To do this, create a file named database-connection-test.php containing the following code:

Replace the word MotDePasse with the root password.

Then place this file in your web directory and open it in a browser. If logging in with root username and password works, your database is accepting logins.

If you are unable to establish a MySQL/MariaDB connection with another username or password, repeat the test replacing root with the affected username and password with the corresponding one.

Note

Remember to delete this file once testing is complete.

Solution 7: Control the firewall and ports

Even if MySQL or MariaDB works correctly and the credentials are valid, a firewall may block the connection. By default, MySQL uses port 3306 for incoming connections. First check if this port is open. On a Linux system, use the following commands:

sudo netstat -tulnp | grep 3306
# Sur les systèmes avec ufw (Ubuntu)
sudo ufw status

If the firewall is blocking the port, you can specifically allow it with the following command:

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

Be careful to only open the port to IP addresses that actually require access. Opening to all addresses poses a security risk. After adjusting the firewall, your application should be able to reconnect to the MySQL/MariaDB database. Check correct operation using the PHP test script or directly from the console.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Web Marketing

Localhost: how to connect to 127.0.0.1?

When you call an IP address, you are usually trying to contact another computer on the Internet. However, if you call the IP address 127.0.0.1,

Web Marketing

What is Proxmox? – IONOS

Proxmox is an open source platform dedicated to virtualization and containerization. It allows you to manage and operate virtual machines, containers and high availability clusters.

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact