How to Fix the Error Establishing a Database Connection in WordPress

Hi, I installed and activated elementor. I was designing my home page for around one hour using elementor. Then when I clicked on publish and visited website, I got this error “Error establishing database connection”.
Whats the problem? Everything was working fine until I started using elementor. I get same error even when I try ‘

Please help !

Did you made any recent changes in the wp-config.php file? I guess, no. If so, this is a hosting problem.

If you notice this issue randomly, I would suggest moving to another hosting.

Before doing anything, please take backup first.

  1. Ensure that MySQL server is running

Create a new file called testconnection.php and paste the following code in it:

$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
echo 'Connected successfully';

You can find Database name, username and password in the wp-config.php located at public_html.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

If you are able to connect, it’s okay.

Else, please contact once host to report MySQL server down issue.

1 Like

No, I did not make any changes in wp-config.php. I am using Digital Ocean. This is the first time I come across this problem after site launch.

As I am using Digital Ocean, I access my files through FileZilla.
When I connect to FileZilla and click on folder ‘root’, I get error ‘root permission’ denied. I am not able to check wp-config.php file.
Note : After hosting, I had created another user in ubuntu through terminal, and I am connecting to filezilla through new user.
When I log in from user root, it says ‘permission’ is denied.

Give them permission file to 777

May be it works

Please check the correct directory

It may /var/www/html

Enter this in the remote site field in the FileZilla.

1 Like

Thanks. It worked. Everything is working after reboot.

1 Like

Thanks for the support. Everything is working fine after reboot.

hi i am am pretty much new using digital ocean i had hosted my website with plan $5 well starting it used to be fine but now a days i am experiencing error establishng database connection and some time site experiencing techniical difficulties when ever those appear when i check the graph my cpu and disk io is spiked in cpu user is taking more and disk io read is taking more percentage i am experiencing this whenever i am posting an article should i upgrade the plan or is there any problem that i should be sorting because the spike is occuring whenever i am publishing the article and also forgot to say had created using one click wordpress install so i didnot manually created database please help me out

That appears to be WordPress issue related to any existing plugins.

Have you tried optimizing database?

If not, take backup and then you can proceed.

Hey thanks yeah i had optimized now using wp optimizer

That’s a good step. Which all plugins are you using?

ad inserter Anti spam bee, updraft plus, broken link checker , envira gallery lite, insert header and footer , jetpack , Visitors Traffic Real Time Statistics Free , wp super cache , wp optimizer , wordfence security , wp polls , yoast seo , table press

while optimizing i had seen my sql data is less than 50mb and also i am worried to know should i create database manually

Deactivate and delete this plugin, your problem should be resolved now.

thanks bro

You’re welcome!

I am getting " Error establishing a database connection" sometimes. It lasts for 1-2 mins. Not always but more frequently. What might be the reason ?

Unfortunately, Bad plugin. Can you list all of them? I can help in pointing out one.