Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Fix the Error Establishing a Database Connection in WordPress

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

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.

Fix the Error Establishing a Database Connection in WordPress

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.

Database connection error in WordPress

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.

Click the Manage button next to the Database section

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.

Find your database name and username in the Current Databases section

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.

Change your database password

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.

Repair WordPress database

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.

Click the Manage button next to 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.

Click the Repair database button

The hosting panel will now automatically repair your database for you.

Once the process is complete, you will also see a success message.

Database repair 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.

Click the Manage button next to the PHPMyAdmin option

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.

Connect PHPMyAdmin with your database

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:

Change site URL

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.

WPBeginner Pro Services

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

663 CommentsLeave a Reply

  1. 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!

  2. Riya Mehta says

    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.

  3. bog says

    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).

  4. Masood Ahmad says

    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.

  5. Jamie says

    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!

  6. Tony thomas says

    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.

  7. Tim says

    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 says

      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

  8. Prashant says

    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

  9. Robert DeRobertis says

    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.

  10. Cássio Sá says

    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.

  11. Ali says

    I just change http to HTTPS Now my site is not opening and showing same error please tell me what to do now ?

  12. Paul says

    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/

  13. stephanie wandke says

    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!

  14. Edward says

    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!

  15. Shakti says

    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.

  16. Shelly says

    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?

  17. Liesje says

    Thanks a lot for this article ! Piece of cake this way :-)
    Saved me a lot of work and headaches.

    Kinders regards,
    Liesje

  18. praveen says

    i have error in wp-admin page. its says Error establishing a database connection. how to solve it .

  19. Ian Wells says

    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.

  20. Ian Wells says

    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.

  21. ani says

    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 !

  22. alexsbutler says

    You can also try Kernel for Word file repair tool to repair your corrupted Word file easily.

  23. Ken Pyle says

    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 says

      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 says

        I also am on Lunarpages. I also needed to re-enter my password in C-Panel. Something must have happened with Lunarpages this morning.

  24. Harm says

    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?

  25. Scott says

    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.

  26. Mohamed Azazy says

    You can also restart your MySQL service if you’re using VPS.
    – sudo service mysql restart

    Worked for me!

  27. Michael D says

    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.

  28. Rav says

    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!

  29. Christopher says

    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.

  30. Adrienne says

    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

  31. Nicole says

    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

  32. m3d01 says

    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 :)

  33. John Bert says

    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! :)

  34. Micky says

    I have seen this error. I was putting the wrong MySQL port in connection line. Your post helped me fix it. :)

  35. Carl says

    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.

  36. Leonard Rattini says

    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)

  37. Toby Harding says

    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.

  38. Unknown says

    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 !

  39. Friday says

    I can’t help it, since i created my site i haven’t touched that area can you help me to do it

  40. mikhail says

    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]’);

  41. James Kalmakoff says

    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!

  42. Dawn says

    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 says

      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.

Leave a Reply to Carrie Case Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.