Portal Home > Knowledgebase > PHP > MySQL database connection troubleshooting


MySQL database connection troubleshooting




Troubleshooting MySQL Database Connection Issues

Databases can be a key part of a website. Databases tell a website how to function as well as provide data too. Without a proper connection to an SQL database, a website cannot function well and you could encounter an error like:

Error establishing a database connection
or,
Warning: mysql_connect(): Access denied for user: 'user1_name1@localhost' (Using password: YES) in /home/abc123/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 at all. This can be corrected by checking each of the connection points between the database and the website as provided in the steps below:

1. Create & Assign a New Database User

The first step is to create a new user via cpanel and then assign the user to the database. This is needed to reconfigure your website's database settings.

 

  1. Create a new database user.
  2. Under Add User To Database, choose the user you just created from the User drop-down menu.
  3. Choose the name of your database from the Database drop-down menu and click Add. (Notice that cPanel will automatically prepend your database name with your cPanel username.)

Note: Be sure to note the database user name and user password for the new user being assigned to the database. This is very essential to the following steps.

2. Locate the Database Configuration File

 

Once you have created a new user and have assigned the user to the database, you will need to locate the database configuration file for the site.

Modify accordingly.

 

3. Reconfigure the Database Settings

Once the configuration file has been located, you can reconfigure the database settings for your website. Though every script can vary, the following instructions provide a general walkthrough of how to complete this step.

    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','4Xyfde%53L');
      define('DB_HOST','localhost');


    3. Make sure that the cPanel username in the first part of the database name is correct. Update the cPanel username (part before the underscore) if needed, but change nothing after the underscore. For example:

      define('DB_NAME','abc123_wrdp1');

    4. Replace the current DB_USER and DB_PASSWORD values in the file with the new values. For example:

      define('DB_USER','joe123_parcomuser');
      define('DB_PASSWORD','nvGXdCC0dCMz');
Be sure that the host name is localhost.ave the file, and you are done!

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

Note: Please contact us if you wish to add information to this article. Thank you!

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Is PHP 4 supported? (Views: 1922)
PHP Pear Modules (Views: 2047)