Loading...

Knowledge Base

Troubleshooting MySQL Database Connection Issues

Databases are the driving element of a website. Databases tell a website how to function on a dynamic level, and the stored information can automatically update without direct user action. Without a proper connection to an SQL database, a website cannot function and will often display critical warnings such as:

Error establishing a database connection

or

Warning: mysql_connect(): Access denied for user: 'user1_name1@localhost' (Using password: YES) in /home/joe123/public_html/catalog/includes/functions/database.php on line 19
Unable to connect to database server!

These warnings indicate that the website database now exists in cPanel, but the website cannot communicate with it. This can be corrected by checking each connection point between the database and the website, as detailed in the steps below.

Step 1: Configure Database User

To resolve most MySQL connection errors, you need to create a new user, assign the user to the database, and configure the correct privileges.

  1. Create a new database user. Please see the article How to Create a Database User for the instructions.
  2. Assign your new user to your database name and user privileges. Please follow the steps in this section, How to Assign Privileges to Database User.
  3. Note the database user name and user password for the new user being assigned to the database. This is essential to the following steps.

Step 2: Locate the Database Configuration File

After creating and assigning a new user, locate the website's database configuration file.

  • If you received a general error message like the first example above, please refer to the following article to determine the location of your configuration file:
  • If you received a specific error message like the second example above, then the error location has been provided. In the example above, the message indicates that the error occurred "in /home/joe123/public_html/catalog/includes/functions/database.php on line 19."

Step 3: Reconfigure the Database Settings

Once the configuration file is found, you can update the settings to fix the SQL database connection issue. Although scripts vary, the process is generally the same:

  1. Go to the configuration file and choose to Edit.
  2. Look in the code for a place where the database name is defined. The database information should be fairly obvious and easy to find.
    Here is an example of what the database for a WordPress installation would look like:
    define('DB_NAME', 'user1_wrdp1');
    define('DB_USER', 'user1_name1');
    define('DB_PASSWORD', '5Jmfde%53L');
    define('DB_HOST', 'localhost'); 
  3. Make sure that 'DB_NAME' reflects your correct database name. You will notice that it got the cPanel username in the first part of the database name (before the underscore). Update the cPanel username if necessary, but change nothing after the underscore.
    For example:
    define('DB_NAME', 'joe123_wrdp1'); 
  4. Replace the current 'DB_USER' and 'DB_PASSWORD' values in the file with the new values.
    For example:
    define('DB_USER', 'joe123_gatorboy');
    define('DB_PASSWORD', 'nvGHdCC0dCMz'); 
  5. Be sure that the hostname ('DB_HOST')  is 'localhost.'
  6. Save the file, and you are done!

After completing the steps above, you should no longer receive an error message regarding your database connection.

Need further assistance? Please contact us via phone or chat so we can assist you!

Loading...