The ‘Error establishing a database connection’ notice in WordPress is a fatal error that makes your site inaccessible to users. It happens when WordPress is unable to connect to the database.
Since multiple factors can affect this connection, the error can be a bit difficult to troubleshoot for beginners.
We have been running our blog on WordPress for the past 16 years and have encountered a variety of errors and warnings, including the ‘Error establishing a database connection.’ Through our experience and lots of trial and error, we’ve learned how to troubleshoot these issues without disrupting our site.
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
Incorrect credentials are the most common cause of the database connection error. If you 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, such as ‘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 using 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.
You can also use the service for 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 tutorial on how to add a custom database error page in WordPress.
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.
Riya Mehta
Hey,
Today I got the same error and I fixed this by giving all privilege(Permissions) to user under the database defined in configuration file that is wp.config file.
bog
What happened to me was that my hard drive (linux) spontaneously went into read-only mode. Out of nowhere. I know this is an obscure cause, but nonetheless I thought I’d pitch in. (usually the sign of a dieing drive, I’m told).
Masood Ahmad
Hi everyone,
In my case it was very simple. The password for MySQL database and the password in the wp_config.php must be same, ignorant of the fact, I changed the password for mysql database and remained upset the whole day. Until by chance I reverted the change. My site was up and live in the next second.
Jamie
When i rename index.php to index_.php — then the back end of the website loads up fine
Pages on the website give a normal ‘Not Found’ Apache error
——
When i restore the index.php filename — then nothing works, and I receive ‘error establishing a database connection’ on public facing pages. And wordpress’ own ‘error establishing a databse connection’ text box when I visit /wp-admin
Any help you can offer will be much appreciated!
Tony thomas
When you are operating in google cloud, you may often receive this error if your RAM is using more resources.
login to google cloud
Find the instance from ‘compute engine’. Click on the top menu to find ‘compute engine’
login to SSH
type sudo bash (to enter as root)
Then type/copy paste :
systemctl start mariadb.service (in centos 7)
error establishing database will be solved instantly.
Jarrett
If you’re using mysql, you can do this
sudo service mysql restart
Thanks for the pointer!
Sabir Guiri
It worked perfectly. thank you so much!
Yudha Haris
Thanks So Much IT’S WORK..
Tim
Thank you for this article…it’s the most useful on the web I would say.
I still have the problem anyway (host is Godaddy)…I created the testconnection.php page and i get this error message :
Warning: mysql_connect(): Access denied for user ‘username’@’hostIP’ (using password: YES) in /home/content/n3pnexwpnas01_data02/52/3027952/html/wp-config.php on line 106
Could not connect: Access denied for user ‘username’@’hostIP’ (using password: YES)
– username being the username I use to connect to database via Phpmyadmin
– password being the password I use to connect to database via Phpmyadmin
– hostIP being the hostname value I found in the database variables in Phpmyadmin
I have been stuck for hours now…can you help me on this ? I have no idea what to do and how to do it…
Thank you in advance
WPBeginner Support
The most likely reason for this is incorrect information in your wp-config.php file. This could be your MySQL host, username, password, or database name. IF you are using hostname IP, then try adding port to it. Ask your web host for support.
Admin
Prashant
Dear Friends,
I M New and very little to know about wordpress today i face a prob
when i try to publish a new post or update the older one and push the button publish or updates its automatically redirected to home page nothing happen not change in updated post not publish a new post please help me if anybody know about such an issue.
thanks
WPBeginner Support
Try updating permalinks by visiting Settings->Permalinks page and just click on the update button.
Admin
Abel
Hey, thanks, this worked for me, thumps up!
Robert DeRobertis
Thank you, Thank you, Thank you.
I was hacked recently and changed the passwords to my website to something more secure. I missed updating the config file. This saved the day.
Cássio Sá
My friend, i’m having tears of joy right now, you saved my life. The problem was in the config file. I imported the old database and changed the credencials on it, but haven’t changed the credencials in the config file. Thank you very much for the tip.
Ali
I just change http to HTTPS Now my site is not opening and showing same error please tell me what to do now ?
Ryan
Have you changed the site url in the database in WP_Options table?
feizel
Thank you so much. i have been searching for the solution like all night.
Paul
Hey great article about how to troubleshoot database connection errors.
Might be good idea to link to your old article about making a custom dB error page. Otherwise if utilising good caching program it can take a while before actually realise something is wrong. This article and the mail function means you can be notified instantly and provide a nice image and custom text which is more professional than the default message.
https://www.wpbeginner.com/wp-tutorials/how-to-add-a-custom-database-error-page-in-wordpress/
stephanie wandke
Thanks for this article! It was definitely a database user issue and when I added a new user and password, changed the wp-config file, BAM the site was back. You made it easy and I solved my problem in under 15 minutes. Thank you!
Edward
Godaddy customer. Getting this same issue.
Database was working fine. What worked for me was to go into wp_config and change the following line:
define(‘DB_HOST’, ‘localhost’); [the db_host was something different, not localhost]
to
define(‘DB_HOST’, ‘[my IP address]’) and it worked fine
Thanks for the tip!
Daryl
This worked for me on my GoDaddy Site
Shakti
Sir help me please
My website is loading, and working fine but I get error establishing database connection when I tried wp-admin and again I reloaded page 3-10 times and my site is working. Again I get that error unexpectedly.
WPBeginner Support
It seems that your database server is not responding. Please contact your web host and tell them what’s happening.
Admin
Shakti
Ok Thankyou Sir
Shelly
My site goes down and I get this same error message at the same time every night. It lasts for about an hour and a half, and then the site is back up again. Would that indicate an issue with my hosting service?
Harinath
Did you happen to find any resolution for it?
Liesje
Thanks a lot for this article ! Piece of cake this way
Saved me a lot of work and headaches.
Kinders regards,
Liesje
praveen
i have error in wp-admin page. its says Error establishing a database connection. how to solve it .
Ian Wells
This occurred once for me on my EC2 AWS instance. The cause was I am using a small t2. micro server, and I sometimes got too many httpd requests coming in, especially when I turned on my AWS health checks which increase traffic. I reconfigured my /etc/httpd/conf/httpd.conf per instructions here: https://wordpress.org/support/topic/how-to-optimize-apache-for-wordpress and so far, so good. I no longer see 20 or more httpd processes running at once, followed by out of memory and data base disconnected.
Ian Wells
This error kept occurring. I could clear it by rebooting EC2 but not very satisfactory. Then I noticed my EC2 installation was out of date, so I ran “sudo yum update” on the server. Looking much more stable now.
ani
nice …exhaustive ..detailed information …i fought with my hosting guys and demanded a root cause for slow DB performance /connectivity…these guys kept recommended to upgrade hosting account to dedicated setup ..POST gave insights on probable problems and ways to tackle .
Thanks !
alexsbutler
You can also try Kernel for Word file repair tool to repair your corrupted Word file easily.
Ken Pyle
Thanks for the article. Lunarpages is my host and around 2:30 AM (thanks Jetpack for monitoring my site), my site quit working and I also had the “Error Establishing Connection to the Database Error.”
How I fixed the problem was to re-enter the password into the MySQL database (got there via Cpanel). I used the same password that was already in the config file, so that way I only had to change one variable.
I have tried it on 3 Wordpress installs so far and it seems to work, as Jetpack informed me that my site is back up after being down for 16 hours.
It is a mystery as to why it disconnected.
Jason Teale
I am on Lunarpages as well. So basically, I just have to re-enter the the blog password and that is it? What was the one variable that you changed?
Mike B
I also am on Lunarpages. I also needed to re-enter my password in C-Panel. Something must have happened with Lunarpages this morning.
NickP
Thank you, Ken! Exactly what I needed to know. Back up and running in minutes…
Harm
I made some changes in wp_user and wp_metadata to create a new admin. Then I get this message. I tried to look at the wp-config-file, but get the same message! What can I do more?
Scott
Sort of silly looking back at this, but it might help someone else so I think it’s worth sharing. Recently had a site that was set up to use Cloudflare as it’s CDN. All the DNS “A” records were set except for the one for “mysql.domain.com”, so even using the testconnection.php trick didn’t work (on Dreamhost that doesn’t use localhost). Simply added the “A” record in Cloudflare and life was good again.
Sharad Gupta
An awesome solution for a very difficult and annoying problem .
MaSilat
Awesome. It helped me and solve DB problem.
Mohamed Azazy
You can also restart your MySQL service if you’re using VPS.
– sudo service mysql restart
Worked for me!
Dustin Overbeck
Thanks for this suggestion. Restarting mysql was the magic bullet in my situation.
Michael D
Thanks for this article, it helped!
We had the error occurring in both the root domain and wp-login. Changing ‘localhost’ to the IP in wp-config.php fixed it.
Rav
It appeared my ‘localhost’ was not working so I just added the direct address of my hosting provider (mysql.hostinger.co.uk) and everything started working again!
Thanks for the help!
Christopher
Actually, I’m getting “Error establishing a database connection” in my home page only. All other pages are working fine. Please let me know what to do.
Adrienne
My old client has this issue now, and her site is about 3 years old and hasn’t been updated. I think the theme and all the changes has corrupted this site. It needs to be scrapped and redone…. now, I have to find a simple way to do it. lol
Nicole
I was moving a multisite system- I had to switch out the old website name in my database, and in the .htaccess and wp-config files
m3d01
Hello guys,
I found the problem that my hosting service made update and that lead to change in the DB_HOST number so i change it back by the new one by edit the config file and the site is back again to work finally
John Bert
Recently I have transferred from Arvixe to Phi9 web host.
Actually I forgot to change the previous host “MySQL Connections Strings” in wp-config.php. Now it’s working.
Thank you for your great help I really appreciate it!
Micky
I have seen this error. I was putting the wrong MySQL port in connection line. Your post helped me fix it.
Carl
This seems logical. But I go through each step and nothing changes. Why is this so complex?
I liked the idea of the database repair but it appears that Wordpress would need to be up and running to hit the Repair Database button. I’ve been at this for 2 days. Is there a place to go where I can find the problem by looking at some code? This all seems so vague.
Leonard Rattini
As I worked in computer application design for 30-years, and I’m also registered as a Certified Computer Professional, many people, to impress, use the “database” term incorrectly.
Database is a different data retaining methodology than what was use before its time. Back in the earlier days, data was retained on liked related 80-column cards and collectively they were know as a file. For instant many invoice records, collectively were known as the Invoice File. Medical records collectively, were known as a Medical file, etc.
In early big main frame computer days, before PC’s that came along, they processed these “80-column fixed-records.” With the advent of the Internet and distributed multiple keyboards, data then was recorded as “data packets” and they are stored in a “database,” which is a three-dimensional format structure stored in the computer. Then at the right time, to process, data items needed “linked’ data packets then allowed for this to happen which then looked like a record we are familiar with.
An application must be designed as a database base application that includes a Database Administrator person, otherwise its application is not a database application.
A database application served two main purposes versus the old file methods used in the olden days before the Internet and distributed keyboards. One was to capture data items at first point of (data) entry. And secondly, it is intended to eliminate “data-redundancy.” Leonard Rattini, CCP (to know of my CCP credentials, google ICCP)
Toby Harding
I’ve seen this error a few times now, with my Wordpress that is hosted on an Amazon EC2 box in their AWS cloud. Simple way to fix the issue is to restart the instance from the AWS admin console.
Jake B
Thanks for the article. I was able to work through each step until I found my issue.
Unknown
Hey, i am getting this error while installing wordpress “Error establishing a database connection” what should i do to get rid of this error .I’ve tried everything like changed database name , deleted previous databases but still wordpress is not is not installed yet. Please help !
David Clarke
Solved my issue in minutes thanks to this helpful article, thanks a lot!
Friday
I can’t help it, since i created my site i haven’t touched that area can you help me to do it
mikhail
Thanks for the article, it helps.
For me it’s because I change my database user password from CPanel but apparently the wpconfig.php file won’t change automatically when i changed my database user password .
I have to change it manually in
define(‘DB_PASSWORD’, ‘[insert your password here]’);
Juan Loayza
Thks so much! It was a headache … until I found your solution…
Tapiwa
Dude! Thank you so much! I just fixed my website thank to this article and your comment.
Max Demian
Really useful, thank you.
James Kalmakoff
The article was very useful and I resolved my Database connection problem. A further place to look for errors is in the MySQL database. Be sure that the ‘database_username’ and the ‘database_name’ are correctly assigned to each other and that there are no typos!
Dawn
I just got this error, I copy and Pasted a page to a word document, I saw that there was a paragragh added with ads and links to sites like viagra ect. I removed the ad/links paragragh and re-pasted the text back into my webpage. I was on the second page and posted the updates, when I received this error…any one else had this?
J
Hey – this won’t help your problem, but you shouldn’t be using Word to edit these files. You want to use a simpler program like Notepad/TextEdit etc.
Peter Gough
Many thanks for this article. Very, very useful.