How to Fix a Joomla Database Connection Error

Leave your reply

Introduction

There are several possible reasons why your Joomla installation might give database connection errors like "Unable to connect to the database." Learn how to diagnose and solve these errors to bring your website back online.

Database Error When Installing Joomla

If you receive a database connection error when installing Joomla and setting up your website, the most likely problem is one of the following:

  • Incorrect username, password, and/or database name. This is the most common cause of the error "Database connection error (2): could not connect to mysql".
  • Attempting to connect to the wrong host. This is the most common cause of the error "Database connection error (3): Could not connect to database".

To review the Joomla installation process, consult our article Install Joomla on CentOS 7 and Ubuntu 14.04.

Check the Configuration File

The correct username, password, and database name need to be set in the configuration.php file, which is located in your Joomla root installation folder.

Edit this file with the command:

sudo nano configuration.php

Find the lines which read:

public $user = '[database username]';
public $password = '[database password]';
public $db = '[database name]';

Make sure that the username, password, and database name match what you created in MySQL/MariaDB during the Joomla installation process. Update the lines if necessary.

Save and exit the file, then check the website to see if the problem has been resolved.

Verify That the Username, Password, and Database Name are Correct

If the username and password are correct, you will be able to use this information to log into MySQL/MariaDB from the command line with the command:

mysql -u [username] -p [Joomla database name]

For example, if your Joomla database username is joomlauser and the database name is joomladb the command is:

mysql -u joomlauser -p joomladb

You will be prompted to enter the user's password. If the information is correct, you will be logged into the MySQL/MariaDB command line client.

If the information is incorrect, you will get an error like:

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

If this is the case, the simplest solution is to create a new database user by following the steps in our article Install Joomla on CentOS 7 and Ubuntu 14.04.

Be sure to update the configuration.php file with the new information after you have created the new user.

Check the Database Host

Unless you are using a different server as a remote database host, the database host should be set to localhost in the configuration.php file, which is located in your Joomla root installation folder.

Edit this file with the command:

sudo nano configuration.php

Find the line which reads:

public $host = '[database hostname]';

Update this line if necessary to read:

public $host = 'localhost';

Note: Do not use your server's IP address or domain name in this field.

Save and exit the file, then check the website to see if the problem has been resolved.

Database Error on an Existing Joomla Website

If your website was running fine before now, then MySQL/MariaDB has most likely stopped. To check this, log in to your server and use the command:

  • CentOS 7:sudo systemctl status mariadb
  • Ubuntu 14.04:sudo service mysql status
  • Ubuntu 16.04:sudo systemctl status mysql

If the server replies that the database has stopped, start it with the command:

  • CentOS 7:sudo systemctl start mariadb
  • Ubuntu 14.04:sudo service mysql start
  • Ubuntu 16.04:sudo systemctl start mysql

After the database has started, check the website to see if the problem has been resolved.