By Kyle
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.
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.
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.
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!
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:
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:
--auto-repair
, not --autorepair
.-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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.