How To Fix The Error By Establishing A Database Connection 2022?

Affiliate disclosure: In full transparency – some of the links on our website are affiliate links, if you use them to make a purchase we will earn a commission at no additional cost for you (none whatsoever!).

In this blog, I will teach you how to fix the error by establishing a Database connection.

How to Fix the Error Establishing a Database Connection in WordPress? That’s your problem? Well, you’re perfectly placed on this piece. Let me be honest, I was on Amazon AWS servers till the last week, and this was the one problem that showed itself up every second day.

So I had to find a way to counter the WordPress error establishing a database connection problem, right? And so I did. And when I did, it occurred to me, it’s simple enough, why not share it with you guys over here.

At first glance, it’s clear enough that it’s a SQL error, or at least something concerning itself with the database, right?

Well yeah, it is, but then again, MySQL databases aren’t everyone’s specialty, just because you don’t need them often, and so you don’t spend time learning it, right? The good news is you don’t need to!

Okay so hey let’s look at it from the source of origin.

Why Does This Error Occur?

It’s clear that the “WordPress SQL was unable to establish a connection with your server database”, right?  Now, that’s the question, why is it unable to do so?

Now there are quite a number of possible reasons for that to happen. I’ll just list out the most common ones for you:-

  • Your database username/password is wrong.
  • Your Server isn’t responding.
  • Your Database might be screwed up and corrupted.

Now, those are the reasons why WordPress gets an error establishing a database connection WordPress, so let’s solve the problem.

How to Fix the Error Establishing a Database Connection in WordPress:-

#1:- WpConfig File Edits:-

I’d say, start by checking your wp-config file. This is the most important file in your whole WordPress directory. It’s also the most lightweight file, meaning it doesn’t have thousands of lines of codes, but still, it’s of extreme importance.

It’s what holds all your file connections, special permissions, access rights, and whatnot! So yeah, if something’s wrong with your Database, this is where it most probably can be spotted.

Login to your Cpanel.

Click on File Manager > Public HTML

CPANEL

Choose the website for which the problem is occurring.

CHOOSE WEBSITE

Inside there, you should see the wp-config file easily enough.

WP CONFIG

Right-click on it, and click Edit.

CLICK AND EDIT

It should present you something like this

ERROR ESTABLISHING DATABASE

Check if your Username, Password, and HOST are what they should be. Now one thing I just have to mention is that the HOST isn’t always Localhost.

So in case you aren’t with GoDaddy, HostGator, Bluehost, or such popular servers, there might be a need for you to change your Host. And you can find the right host from Google. (Ofocurse!).

#2:- Is Your SQL Server Responding?

The other possible cause of this problem is when your SQL server crashes! Well not the whole server, just the MySQL server.

Now, it might happen for several reasons but the most common reason is huge traffic. Meaning, more traffic than the site can handle by default.

In that case, the best bet for you is to give a call to your hosting provider’s support team and ask them if your server is running fine or if you need to upgrade it.

Although if you want to check your SQL server’s health all by yourselves, that’s totally possible. You can do so b:-

  • Checking your other websites on the same account! Well yeah, if other sites are working fine, then you’d need to check the database user permissions. (I’ll show you how below!)
  • Connect to PhpMyAdmin. Well by connect, I just mean click on the phpMyAdmin button in your Cpanel! If it opens, meaning it’s working fine too.

Checking the database user permissions:-

Just create a new file, and name it “testconnections.php”.

Paste this code in the file :-

<?php
$link = mysql_connect(‘localhost’, ‘root’, ‘password’)
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;

mysql_close($link);
?>

And then edit the “localhost’ “root” “password” values. Change them to your server details. And then reconnect to the SQL server. If it connects successfully, well then you’re a go! Or else, check out the below solutions.

#3:- Running a SQL Query:-

It’s pretty simple, all you have to do is run a SQL query from your Cpanel. Well, what this does is checks your website URL’s validity.

Well, it’s not as simple as that, so ignore the science and let’s work on the solution? Yeah, good decision. So if the above two solutions haven’t worked for you:-

Login to your Cpanel, and click on PhpMyAdmin.

CPANEL

Then click over SQL.

SQL

And then paste this code into the box.

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

(Replace the Your Site URL and SiteUrl values with your URLs). And click go.

FIX THE ERROR

Quick Links-

Final Words:- How To Fix The Error By Establishing A Database Connection?

So yeah folks that were it, on How to Fix the Error Establishing a Database Connection in WordPress. Those methods I’ve discussed above will most probably fix your headache.

In case I missed something, or there’s something more you’d like me to add or a solution that worked for you? Feel free to share it down in the comment box, I’ll add it here with proper credits to you.

And of course, a share wouldn’t hurt either.

Vivek Dhiman

Leave a Comment