How to fix database connection errors

Databases. They are the engines behind our websites and web apps running. Our cPanel shared hosting supports MySQL and PostgreSQL databases.

Sometimes, after uploading a website, you may get database connection errors. This guide will help us troubleshoot this error and resolve it

To troubleshoot your database connection error, we will use a checklist approach, where we will do a diagnosis of each of our causes and fixes of database connection errors one at a time. By the end of the article, the issue should be resolved

1.Incorrect Database Credentials

The most common cause of Database Errors is incorrect database credentials. To successfully define a database connections, 5 components are needed:

  • Database Username: This is the user for your database.
  • Database Name: This is the name of the database
  • Database Password: This is the password for your Database Username
  • Database Host: This is always localhost
  • Database Port: This is set automatically and does not need to be defined. If your app explicitly needs to define a port, use 3306 for MySQL and 5432 for PostgreSQL

What To Check

  • Check that the database credentials are spelt correctly. Sometimes, you may upload files from your computer and forget to update your database details from what you used locally. e.g using username root
  • Also, check if the username and database name are written in full. The system prefixes database names and usernames with the cPanel Username. For example, if you create a database user called dbuser, and your cPanel username is truehost, your database username will be truehost_dbuser and not just dbuser.

2.Database User lacks proper permissions to access the database.

After you create a user and database name, you have to assign correct permissions to the user for the database.

To do add permissions for a user, check this video

3.Crashed database

MySQL uses 2 main engines: MyISAM and Innodb.

MyISAM crashes oftnely but allows you to run repair on it. Innodb does not crash easily, but does not allow repairs on it. We recommend using Innodb whenever you have a choice. However, if your website uses MyISAM, you can repair it as follows

  • Login to cPanel and go to MySQL Databases icon
  • Under Modify Databases, select your database and click Repair Database

Was this article helpful?

Related Articles

Leave A Comment?