When the site says it cannot connect to the database

It is one of the more frightening messages a site can show, because there is no page at all behind it: just a line of white saying there is no database connection. The good news is that your text, images and orders have not gone anywhere. What broke is the route between the site and the place they are kept.

There are only a handful of causes and they repeat. Work through them in this order, which is the order they turn up in real life.

1. The connection details changed, or were never right

By far the most common cause, and it nearly always follows something: you changed the database user’s password in cPanel, restored an old copy, moved the site from another server, or reinstalled over the top. The site’s configuration file is still trying the old details.

1 Open the file where your site keeps those details. Every program keeps them somewhere different, and the full list is in where each application keeps its connection details.
2 Compare four things, one at a time: database name, user, password, host. The first three have to match what cPanel shows under MySQL Databases.
3 Check the prefix. cPanel puts your account name in front of both the database name and the user name. If the file only has the part you chose, with no prefix, it will not connect.
4 Check the host. Unless we told you otherwise it is localhost. It is not your website address and it is not the server IP.
Test the details without touching the site. Go to phpMyAdmin and try to log in with that user and that password. If it refuses you, the problem is proven and you never had to open the site at all. If it lets you in, the details are fine and the cause is something else: keep reading.

2. The user exists but is not attached to the database

There are three separate things: the database, the user, and the link between them. Creating the first two and forgetting the third produces exactly this error, and it is a classic right after moving a site. In cPanel, under MySQL Databases, find Add User To Database, pick both and grant ALL PRIVILEGES. The walkthrough is in creating databases and using phpMyAdmin.

3. The account ran out of space, or out of inodes

A database has to write to disk even just to answer a query. If the account hit its ceiling, MySQL starts refusing, and the site renders that as «could not connect». Note that you can have plenty of space free and still be at the ceiling, because the limit that bites is often the number of files rather than their weight. It is explained in the limits nobody advertises.

4. The error comes and goes on its own

If the site drops out at busy hours and recovers seconds later, leave the configuration file alone: it is fine. What is happening is that the account is running out of simultaneous processes or connections, and the database refuses the leftovers. That is a consumption problem, and it has a different cure.

Symptom Where to look
Always at the same time of day A scheduled job running far too often, or a bot crawling the site. Check Resource Usage in cPanel.
In bursts, at no fixed hour One heavy page somebody visits now and then. See what to measure before you change plan.
Ever since you installed a plugin It is the plugin. Switch it off and confirm before you look any further.

To bring the consumption down instead of buying a bigger plan, see what eats CPU on a website.

5. The database itself is damaged

Rare, but it happens: a table left corrupt after a power cut or an interrupted import. In phpMyAdmin you can select the tables and choose Repair table. If the error comes straight back, stop: you are fighting a symptom. Talk to us before it gets worse.

Before repairing, exporting or deleting anything, take a copy. There is no undo in phpMyAdmin. If you cannot export because the database will not answer, use the panel’s own copies: restoring with JetBackup.

Where the real error is written down

The message the visitor sees is a summary. The actual error, with the reason attached, is in your account’s PHP error log. That is the difference between guessing and knowing. See where the PHP error log is.

Been through the list and still down? Send us the site address and the time it started.

Open a support ticket

SEE ALSO

Hosting plans and what each one includes

Support Policy: how far our help goes

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from $10.00/mo

See plans
  • 0 Users Found This Useful
Was this answer helpful?