Question

Cannot resolve WordPress "Error Establishing Database Connection"

Per DigitalOcean’s How To Debug the WordPress “Error Establishing Database Connection”, I have followed the steps to attempt to resolve the issue stated in the title of my question. Unfortunately, I’m still encountering the error and, importantly, it is not intermittent, so I do not believe it is a memory issue. For context as to when this issue began, please refer to the note at the bottom.

Overview


Droplet: Regular 1GB / 1 CPU Ubuntu 22.04

Potential issue: I’ve just updated from 20.04 to 22.04. On my DigitalOcean dashboard, my droplet is still showing LAMP on Ubuntu 20.04, though I’m not sure that was ever going to update.

App: WordPress


On the primary URL of my WordPress site, I’m only seeing an index of the files. At the bottom, its says:

Apache/2.4.52 (Ubuntu) Server at [domain] Port 443

User Sergiu Dumitriu on Stack Overflow suggested that a user with a similar (but intermittently problematic) situation ensure the if the default port for MySQL is different than the default 3306 to change it in wp-config.php. I have not had a chance to test this, but I don’t believe 443 corresponds to the MySQL port.

At /wp-admin, I see the following message:

Cannot select database

The database server could be connected to (which means your username and password is okay) but the [database-name] database could not be selected.

  • Are you sure it exists?
  • Does the [database-user] have permission to use the [database-name] database?
  • On some systems the name of your database is prefixed with your username, so it would be like [database-user]_[database-name]. Could that be the problem?

I have verified all of these scenarios are not the culprit by following this DigitalOcean guide. I get the titular “Error establishing database connection” when I attempt to repair the database as outlined in Repairing the WordPress Database.

User DriftingBlueCoral suggested repairing the MySQL tables with the command:

/usr/bin/mysqlcheck --all-databases --auto-repair

I attempted to run this, but received the following error:

 Access denied for user 'root'@'localhost' (using password: NO) when trying to connect

I’m not very familiar with Apache and MySQL, so I’m not sure how I can both run this command and use my root user’s password, since that would mean logging into MySQL and relying on MySQL commands.

It’s worth noting how I got here


My personal portfolio and this WordPress site are siblings on my server. I was uploading files via FTP and accidentally deleted the entire folder for my WordPress site. I attempted to cancel the process, but it had already wiped crucial files for the operation of the site.

I did not perform routine backups. I will now be performing routine backups.

Following DigitalOcean’s How To Install WordPress on Ubuntu 20.04 with a LAMP Stack that got me up and running the first time, I’ve arrived here. I’m confident the issue is some kind of misalignment with my tables.

For instance, would the original database and user have been deleted with WordPress files? Again, I’m new to MySQL, but I don’t believe that is how it’s structured. Are the original database and user conflicting with the ones I have created following the guide? I feel like this is where my problem lies, but I simply am not sure, and am hopeful the community can lend me a hand.

Thank you.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 10, 2023
Accepted Answer

Hi there,

If you’ve already deleted some crucial files, including files in the /var/lib/mysql directory, it would be best to maybe start with a clean new Droplet. That way you will be sure that no system files have been deleted, and you would be able to upload your WordPress files and import your database into that clean Droplet.

You can use the 1-Click WordPress image from the Marketplace:

https://marketplace.digitalocean.com/apps/wordpress

If you prefer to try and get the old Droplet up and running, here are some suggestions for the problems that you’ve mentioned:

Regarding the mysqlcheck errors that you are seeing:

  • The correct flag is --auto-repair, not --autorepair.
  • With the -p flag, you will be prompted to enter the root password for the MySQL server so you will not get the authentication error from above.

Try running the following command:

mysqlcheck --all-databases --auto-repair -u root -p

Regarding the MySQL service itself, make sure that it is actually up and running using the following command:

sudo systemctl status mysql

If you notice that it is not running, you can try and restart it with:

sudo systemctl restart mysql

If the service is still failing to restart, you should check the MySQL error log which should be located in the /var/log/mysql/error.log.

Another common problem for the WordPress error establishing database connection is incorrect database details in the wp-config.php file as you mentioned. Double-check the wp-config.php file located in the root directory of your WordPress installation. Make sure the following parameters are set correctly:

  • DB_NAME: The name of your WordPress database.
  • DB_USER: The username of the database user.
  • DB_PASSWORD: The password of the database user.
  • DB_HOST: The hostname of your database server (usually ‘localhost’ or ‘127.0.0.1’).

To verify if the credentials are actually correct, you can use the mysql CLI: mysql -u your_username -p and when prompted enter the password from the wp-config.php file.

Let me know how it goes!

Best,

Bobby

Edit: The code snippet provided by user DriftingBlueCoral does not result in the error I stated in the above question. Instead, this error is returned:

mysqlcheck: [ERROR] unknown option '--autorepair'.

The error I stated in the question is actually returned from the following code snippet I found reading about mysqlcheck:

mysqlcheck -o --all-databases

The error returned is as follows:

mysqlcheck: Got error: 1045: Access denied for user 'root'@'localhost' (using password: NO) when trying to connect

To complicate things further, I’ve realized re-reading that article that I should have CD’d into var/lib/mysql, but when I attempt to do this, yet another error is returned: No such file or directory

Funnily enough, a user on Stackoverflow received the same error, but was successfully CD’d into the correct directory.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel