Are you seeing the ‘Error establishing a database connection’ notice in WordPress?
This is a fatal error that makes your site inaccessible to users. It happens when WordPress is unable to make a connection to the database. Since multiple factors can affect this connection, the error can be a bit difficult to troubleshoot for beginners.
In this article, we will show you how to easily fix the ‘Error establishing a database connection’ on your WordPress site, step by step.
What Causes the Error Establishing a Database Connection in WordPress?
A database is software that makes it easy to store, organize, and retrieve data from other software.
As a content management system, WordPress uses a database to store all your content and other website data. It then connects to the database each time someone visits your website.
WordPress needs the following information for connecting to the database:
- Your database name
- Database username and password
- Database server
This information is stored in your WordPress configuration file, which is called wp-config.php.
If any of this information is incorrect, then WordPress will fail to connect to your database server, and you will see the ‘Error establishing a database connection’ error.
It is one of the most common WordPress errors. Apart from incorrect credentials, this error can also appear if the database server is down or the database files are corrupt.
Having said that, let’s take a look at how to fix the ‘Error establishing database connection’ issue in WordPress with step-by-step troubleshooting.
1. Check Your WordPress Database Credentials
The most common reason for the database connection error is incorrect credentials. If you have recently moved your WordPress site to a new host, then this is likely the problem.
Your WordPress database credentials are stored in the wp-config.php file. It is the WordPress configuration file that contains important settings, including database information.
If you have not edited a wp-config.php file before, then take a look at our beginner’s guide on how to edit the wp-config.php file for more instructions.
You will be looking for the following lines in the wp-config.php file:
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );
Here, you must make sure that the information for the database name, username, password, and database host is correct.
You can confirm this information from your WordPress hosting account dashboard. For this tutorial, we will be using Bluehost, but the process will be similar for other hosting panels as well.
Upon logging in, click on the ‘Website’ tab in the left column and then switch to the ‘Advanced’ tab. Then, scroll down to the ‘Database’ section and click the ‘Manage’ button next to it.
This will open the MySQL Databases page in cPanel in a new window.
Here, you can find your database name and username in the ‘Current Database’ section. Keep in mind to copy and paste these details into a notepad so you can add them to the wp-config file.
Next, scroll down to the ‘Current Users’ section, where you can click on the ‘Change Password’ link next to your database name.
This will take you to a new screen, where you can change the database password according to your liking.
Once you have confirmed all your database details, you can change that information in your wp-config.php file if needed.
After that, try visiting your website again to see if the database connection error has gone.
If you can still see the error, then this means that something else is wrong.
2. Check Your Database Host Information
If you are confident that your database name, username, and password information are accurate, then you will want to make sure you are using the correct database host information.
Most WordPress hosting companies use localhost as your database host. However, some managed WordPress hosting companies use separate servers to host databases. In that case, your database host information will not be localhost.
This means that you will need to contact your WordPress hosting company to confirm these details.
3. Repair WordPress Database
Now you may be getting a different error in the wp-admin dashboard like ‘One or more database tables are unavailable’ or ‘The database may need to be repaired’. In that case, you need to repair your database.
You can do this by adding the following line in your wp-config.php file. Make sure to add it just before the ‘That’s all, stop editing! Happy blogging’ line:
define('WP_ALLOW_REPAIR', true);
Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php. Just make sure to replace yoursite.com with your own domain name.
Here, you must click the ‘Repair Database’ button to start the process.
Note: The user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this line to code from your wp-config.php.
However, if you don’t want to add any code to your site, then you can also repair your database from your hosting account’s cPanel.
Go ahead and visit your hosting account, where you must open the ‘Websites’ page from the left column. Then, switch to the ‘Advanced’ tab and click the ‘Manage’ button in the Database section.
This will open cPanel in a new tab. Here, scroll down to the ‘Modify Databases’ section and choose your database’s name from the ‘Repair Database’ dropdown menu.
Once you do that, simply click the ‘Repair Database’ button to start the process.
The hosting panel will now automatically repair your database for you.
Once the process is complete, you will also see a success message.
You can now visit your WordPress site to see if the error has been resolved or not.
4. Check If Your Database Server Is Down
If everything seems to be correct, and WordPress still cannot connect to the database, then your database server (MySQL server) may be down.
This could happen due to heavy traffic on a server. Your host server just cannot handle the load (especially when you are on shared hosting).
Due to this, your site will get slow, and may even output the error for some users. In this case, you should get on the phone call or live chat with your hosting provider and ask them if your MySQL server is responsive.
Additionally, if you have other websites running on the same server, then you can check those sites to confirm that your SQL server is down.
If you do not have any other site on the same hosting account, then simply go to your hosting dashboard and switch to the ‘Advanced’ tab.
After that, click the ‘Manage’ button next to the phpMyAdmin section.
This will open phpMyAdmin in a new window, where you must click the ‘Database’ option at the top.
After that, click on your database’s name to access its settings. If you can do that, then it is time for you to check if your database user has sufficient permissions.
To do this, you need to create a new file called testconnection.php and paste the following code into it:
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>
Upon pasting the code, make sure to replace the username and password with your own. You can now upload this file to your website and access it via a web browser.
If the script connected successfully, then it means that your user has sufficient permissions, and something else is causing the error.
Now, you must go back to your wp-config file and scan it to ensure that all the details are accurate and without any typos.
Additional Solutions That Have Worked for Users
If the above-mentioned troubleshooting tips fail to fix the database connection error on your website, then you may try these additional steps.
As reported by our users, these steps have helped some of them resolve the database connection error on their websites.
1. Update the WordPress Site URL
You can try updating the WordPress site URL using phpMyAdmin in cPanel.
Simply access phpMyAdmin from your hosting account dashboard and select your WordPress database from the list.
After that switch to the SQL menu on the top and enter the following MySQL query:
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
It should look like this:
Don’t forget to provide your own site URL and change wp_options
to your own table name, as you may have changed the WordPress table prefix.
2. Rebooting Web Server
Users on dedicated servers, local servers, and virtual private servers (VPS) can try rebooting their servers.
This will restart your web and database server, which may fix some temporary glitches causing the error.
3. Ask For Help
If everything else fails, then you may need to contact your web hosting company. All good WordPress hosting companies will help you troubleshoot the problem, point you in the right direction, or even fix it for you.
You can also hire WordPress developers using a platform like WPBeginner Pro Services to help you fix this issue for reasonable rates. We have a dedicated team of expert developers that you can trust to resolve any of your WordPress issues.
Plus, you can use the service for your site maintenance, hacked site repair, speed optimization, website rebuilding, and SEO optimization.
We hope this article helped you fix the ‘Error establishing a database connection’ issue in WordPress. You may also want to see our WordPress troubleshooting guide for tips on resolving WordPress issues on your own, or our expert picks of the best tools for WordPress freelancers and developers.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
cgjam says
had this happen to me when trying to install word press, didnt realise i hadn’t actually enabled user privileges! thanks for this helpful article
Moogie1947 says
Just had this happen after I backed up my files and had set up a password to access and backup my database before I upgraded.
Thanks for your site realised that I had accidentally replaced the autogenerated password (as I didn’t know it existed) . So I found it in the config.php file in my backup set and reset it in the admin panel on my host’s site. All now OK.
Can’t thank you enough for a brilliant article.
ValentinaMercenaro says
I’ve tried them all! Eventually I solved by creating a
different DB Username in my host cpanel (giving it all the privileges of
course) and also changing the DB Host to my server IP in the wp-config.php file.
I’ve got no clue why that would solve the issue but perhaps
it could be that the DB Name and DB Username cannot have the same name. (You may think: “duh! Of course not!!!”, well, my host’s “DB Creation Wizard” had auto generated the two fields and, believe it or not, it wouldn’t let me change it at the beginning). I had to create a new user
separately later on to solve the issue.
Thanks for this great post. x
bfadmin1 says
You’re the best ….
I didnt know that changing the db password would drop the database connection to wordpress ….
Solution 2 was brilliant ……. 2 min max.
Thanks alot!!!
needhelpquik! says
hello…i am getting this error. it just happened out of the blue today! nothing was changed other than some text on a page. i logged out. now i got the error.
in one of the paragraphs you state: “First thing you should do is to make sure that you are getting the same
error on both the front-end of the site, and the back-end of the site
(wp-admin). If the error message is the same on both pages “Error
establishing a database connection”, then proceed onto the next step.” what is the next step? i get the error on both sides.
this was confusing to me. sorry. so what is the next step so i can try to figure out how to fix this!
thanks for your help!
PeterBestel says
This error happened to one of my membership sites, except when you logged in, the site looked and acted normally.
After following the usual routes, including changing the database user and accusing the theme of being the culprit, I discovered an extra “?>” at the end of the wp-config.php file. Once I deleted that everything worked.
I have no idea how that ended up there. Hours of frustration sorted by deleting two characters!!
Jayden Lawson says
Thanks guys! One of my tables failed to repair, so I followed this advice to repair that table in phpMyAdmin http://wordpress.org/support/topic/wp_options-table-is-marked-as-crashed-10?replies=6
Ed Luvables says
This worked perfectly, I forgot to change my password in the config file. Thanks!
George Stroud says
With this problem I found that it was a database corruption but that it resulted in my not being able to access the WordPress repair tool – however, I could effect a repair by using the mySQL section in my hosts control panel (cPanel) and then the site came straight back up.
Jenerwin says
just encountered this type of error..and i be able to fixed this through database optimize and repair..i like this post..thumbs up..
Carrie Case says
I had the Permalinks set to Post Name. I switched it back to Default and the error went away and my pages re-appeared. I then switched back to Post Name because I prefer this setting, and it still worked. Good to go. Only took several hours to figure that crap out. Lol.
Joninashby says
Saved my life – changed the database password within the hosting control panel, had no idea I had to update it in wp-config. Hours of stress avoided – thanks!
K.C. Bateman says
thank you
This was very helpful!
Ayo Akinbode says
It worked! tnx so much
BillR says
I had this issue today.
Server had crashed last night and was attended to fairly quickly when the issue was known.
All worked for a while but today WordPress couldn’t make a connection to the database.
The Simple Machines Forum database was working ok and the forum would load.
What I did was:
1. Create a new database user
2. Assigned that user to the WP database with full privileges.
3. Updated WP_config to use that user
After that all was good.
Seems that after a server issue the WordPress user privileges can become corrupt or changed even though the user is shown with full privileges.
Thanks for this post. It pointed me in the right direction.
Alex says
Thank you very much!… hours and hours seeking the answer and your’s worked just fine!…
Chris says
This worked for me as well. Very strange.
Enhow says
Hi! I having constantly this issue, I go to the webpage and it shows this error, but then I refresh some minutes later and the error it’s gone. It stopped happening so I even forgot about it. But today I asked my work partners (4 people) to enter the site at practically the same time to see the new mobile appearance and some got the error and others didn’t (it wasn’t exactly at the same time since I asked to do it in their phones and everyone had different speed) this concerns me a lot. I read you told someone else who also had this issue but it fixed by itself that it was because of his server. Should I call my server and ask what’s going on? Or is there a chance I did something wrong? Almost everyday it’s weird and it just go away. Thanks!
WPBeginner Support says
It seems like your database server is not working properly. Please contact your web host.
Admin
Mike says
Thank you! Edited the .php file and fixed my problem. Awesome.
Terry says
Sorry guys,but for a real beginner your information appears to assume a greater knowledge on how to do stuff than perhaps some of us have.It’s ok to say,check this or that, but when we have no idea how to get there, it is not much help.I know you are saying that we should not be here if the tech. level is not there, but that does not help.One can follow a more step by step procedure but perhaps that is asking too much.Thanks.
WPBeginner Support says
We understand, and we are sorry that you didn’t find this information easy. Please let us know which part you are having trouble with and we will try to explain it more.
Admin
Henri says
Thanks – Your article helped me to fix the problem in minutes!
Chuck says
Last night my Centos server crashed and when it came back up I had the error.
What worked for me was to rename the file /var/lib/mysql/mysql.sock, start the mysql service with service mysqld start, shutdown the server with shutdown -h now, then bring the server back up.
Basically, the crash had locked up MySql so it thought it was running when it really wasn’t.
Bart says
Thank you very much!
In my case I fixed it by
1) deleting the current user that was connected to my database
2) creating a NEW user with all privileges
3) adding this user to the database
4) refreshing my details in config.php (user and password)
5) refresh my site (F5)
Furthermore, although I hadn’t changed anything about this, when I reloaded my WP admin page, a Fatal error occured which told me that the Facebook plugin was creating some sort of memory exhaustion. I disabled the plugin by renaming it in wp-content/plugins and got rid of my problem.
This, by the way, also solved some display issues on my homepage.
Hope this feedback helps and again, thank you very much for sharing this article!
Bart
lubna says
I cant log i to my wp after successful installation of db
Ruthie says
When I went to login to my company’s dev site this morning, I got this message (on the back end and on the front end), but 10 minutes later I refreshed the page and it wasn’t a problem anymore. On the one hand, I’m glad it resolved itself, but on the other hand, I’d really like to know why it happened and how it got fixed so it doesn’t happen again. Any ideas?
WPBeginner Support says
It is possible that the database server was down at the time you got this error. The issue resolved when the database server came back online again.
Admin
Derek Smith says
I am having a database error that I need help with. I bought 40 WP sites and transferred them from one host to another (GoDaddy). I made the mistake of switching some content on the sites before switching the DNS. I switched all of the domains last night to match the DNS of Godaddy, not I am getting database error problems with all of the sites that I worked on.
I’m kind of a newbie on the backend side, but I’m thinking that changing the files and then pointing the DNS after this somehow messed everything up, this is the only thing that I could come up with since the sites I didn’t touch still work. Thanks
WPBeginner Support says
First you should have backedup your old databases using phpMyAdmin and then import them to your new webhost’s database server using phpMyAdmin. You should then change your DNS settings to point to your new web host. After that you need to edit wp-config file on all your sites, change the database name, password, host, settings to match your new databases.
Admin
Derek Smith says
What do I do to fix it now that it is at this point? All the DNS settings are changed and I keep getting the error messages. Is there a way to go back and install the original files ?
Tony says
Hi there…
I’ve been working with a local install for a while now and wanted to change the url of my local install to something else, so I went ahead and changed quite a few times my wp-config database name, my mamp settings of the database in “wp-options”..plus some others I don’t know remember. This resulted in “error establishing database connection”. I troubleshooted for a whole day, (returning back to the same nightmare I got when I first installed mamp and wordpress locally). I changed everything back the way it was to no avail, making me feel soooo frustrated and stupid again…! So, I read this article to the end and where it says “siteurl” solution in the “Solutions that worked for others” section and went back in phpmyadmin to check this…surely enough…in the “wp-options” of the particular database, in the “site url” field, it had the name of my database, (which I had stupidly changed previously without realising its impact), I also checked it against other databases I could access with no problems and the field indeed said, “siteurl” instead of the actual db name!
I changed it immediately and phewww….got my database back and working and logging in with no problems..!!
People, if you did something similar, check this first…it surely made me feel stupid…at least I have another troubleshooting trick up my sleeve now…thank you for your input..!
WPBeginner Support says
Glad that you found the solution. Thanks for sharing it with other users.
Admin
Thouhedul Islam says
Well, thanks for details. But I think, it is very simple. Don’t take it complicated like you. Just open wp-config.php and change the database username and password. Hope it will be fix.
—
Bamanya Brian says
Thanks man..i followed all thru and now my site is up..Godbless
Tamas says
Hello,
Thanks for this description.
I just moved my site from BlueHost to DreamHost.
When going to my site I get an EMPTY (really empty, white) page. Nothing else.
However, when I go to wp-admin… “Error establishing a database connection” is displayed.
I can log in into the MySQL (going to msql.remete.org) and it works.
All four parameters in wp-config seem to be oké, as well:
/** The name of the database for WordPress */
define(‘DB_NAME’, ”);
/** MySQL database username */
define(‘DB_USER’, ”);
/** MySQL database password */
define(‘DB_PASSWORD’, ”);
/** MySQL hostname */
define(‘DB_HOST’, ”);
Here I had replaced the “localhost” by the “msql.remete.org”, as it should be with DreamHost.
My site is remete.org.
I moved already several WP sites … and some worked well, some had the white page at the root, however, this is the first and only one with “Error establishing a database connection” error.
How may I make the DB connection work?
How can the root made work, as well (instead of the white page)?
I would really appreciate your help.
Thanks a lot,
Tamas from Budapest
d4drdave says
Ace post, worked a treat
Shlomi says
for a multisite deployment, you also need to check the table wp_blogs and make sure the correct URL appears there, otherwise wordpress will say “error-establishing-a-database-connection”
Matt says
DING DING! I had pulled down a copy of production data and was suddenly getting this error with a multisite instance. Updated the “domain” column in wp_blogs and it works again. Thanks for pointing it out!
Olga says
Thank you for producing this quality post. Your instruction worked perfectly! and saved me lots of hassle
Bulbul says
Thanks! it solved my problem in few minutes..
Alex says
Hey man. I just wanted to thank you for this GOOD post. It didn’t work and I didn’t know why.
I read your blog and followed everything and now it works again.. after 11 HOURS of work.
This is a sign of appreciation.. If you were here I’d get you a huge beer. Thanks so much for your clear help!
Alex
Travis says
Oh my goodness. Typically posts like these usually don’t pan out for me after following directions… but you sir have saved the day here. Thank you!
Allen says
Thanks so much. It worked.
Tish says
Thank you so much for this! I definitely thought I had lost my site completely until I followed this. My only hangup was that I had no idea where to find the wp-config.php file without being able to get into wordpress. I eventually found my file manager through my hosting, but it took me a while to figure that part out. Keep the great info coming!
Zewdu says
Why do I get this message and how can I fix it
Downloading update from http://wordpress.org/wordpress-3.8-new-bundled.zip…
Unpacking the update…
Could not create directory.: /home/mrc-et.com/www/wp-content/upgrade/wordpress-3.tmp
Installation Failed
WPBeginner Support says
Zewdu this could happen when your file permissions are not properly set, or some configuration issue on your hosts end. Please contact your web host.
Admin
Michael says
OR….
*IF* you were messing around in the editor with any of your .php pages/code in the editor and your copy/paste back to the original did not work on this database connection error… the work-around is to then copy/paste back via FTP to the exact folder of the .php file rewriting over the edited one and this will for sure work to get your site to show back up online again.
superfunkie says
Hi! Im facing the same problem with the wordpress. I did copy and paste the testconnection.php and it showed “Access Denied”. But when I edited the line – “mysql_connect” to “mysqli_connect” database was connected successfully. How do I implement “mysqli_connect” in the wordpress config file?
WPBeginner Support says
Superfunkie please check your php version if it is PHP 5.5 then contact your webhost and let them know about this error.
Admin
Aaqil Mahmood says
I am seeing “a secure data connection could not be established” on android browsers for my site
, only when reading posts not on homepage.
marcel says
Hi there,
I am desperately trying to launch a new install of WordPress with MAMP, both Apache en MySQL give green lights, but when starting up in the browser I get the “Error establishing a database connection” message.
I am relatively new and most of the options above seem rather advanced, and for websites that have already been working, is there a simple way to tackle this for new installations of WordPress?
WordPress was supposed to be not too difficult to install….
Thanks in advance!
WPBeginner Support says
marcel open the wp-config.php file in your WordPress install folder. And check your database username and password. Usually on fresh install of MAMP database username should be root and password should be left blank.
Admin
patel sumit says
very help full thanks
Gautam Sharma says
i am a newbie & my site data access via filezilla, Please tell how to test connection as i have not seen any phpmy admin in hosting control panel.
WPBeginner Support says
If your web host is using cPanel you will see phpMyAdmin under database section. For other hosting control panels, you can ask your web host about it.
Admin
Hamza says
Thanks, worked!
Jen Barnes says
Pardon my seemingly dumb question, but what do I put in for “root” in the MySql test?
$link = mysql_connect(‘localhost’, ‘root’, ‘Q3dDZE3PiUpQ’);
Thanks!
WPBeginner Support says
replace root with your MySQL username (unless your MySQL username is already root then don’t change it) and then your password. Let’s assume your mysql host is localhost, username is jenbarnes and password is Q3dDZE3PiUpQ then you would use it like this
$link = mysql_connect('localhost', 'jenbarnes', 'Q3dDZE3PiUpQ' );
Admin
lee getty says
Thanks dude you saved my website
Kasper says
Hey, I can’t launch my site as wp-admin or just visit the site?
what to do? It’s all my 4 domains there’s been down by this error (below)
“Error establishing a database connection”
Please help me!
– Kasper
WPBeginner Support says
Please try the steps mentioned in the article above.
Admin
Faysal Shahi says
Bro, add Google translator in this blog. I need this post in Bengali.
Peter says
Thanks mate !
Helped a lot, keep up the good work
Asif says
Oh! thank you guys! I almost had a heart attack.
This was the first time I was backing up my data (so that my website doesnt crash or even if it did I could recover it) logged in to PHPadmin panel changed the password caz the original one was auto generated. logged in and backed up. relieved I went back to my website and boom! these big letters: Error Establishing a Database Connection
man, you can understand I flatlined for a few seconds..
Anyway thanks alot!!
onel says
Thank you so much!!! It works!!!