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

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.

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

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.

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

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

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

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.

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

670 CommentsLeave a Reply

  1. Barry

    Under CentOS 7, I saw: Could not connect: Can’t connect to MySQL server on ‘xyz’ (13)

    The problem was due to SE Linux, as documented at

    The problem was corrected using the command:

    sudo setsebool -P httpd_can_network_connect_db=1

  2. neneh

    Thanks, very helpful

  3. Alberto

    After installing and setting up Mamp and a fresh Wordpress on a friends machine, had the dreaded blank page and “error establishing a connection” in the logs as well.

    After pulling my hair for a while, I noticed that the quotation marks in the wp-config.php file had been changed from straight ones ‘ to curvy ones ’ in the few settings one has to edit, and this was causing the errors, the culprit?

    -> TextEdit <- I had used it to make those changes. In it's preferences, the "clever quote marks" setting should be turned off if you want to have those quotation marks remain as they should be.

    • ka

      Thanks Alberto. Textedit has automatically overwritten the straight quotation marks with curly ones. replacing them with straight ones and saving the php file again did the trick. :)

  4. Bastien

    This error can also be triggered because of a DOS XMLRPC attack :

    • Jesus

      How can I fix it?

  5. Tejas Waghmode

    HI

    I’m confuse, how to solve the problem?

  6. Zoe

    Hello, I’m new to wordpress and need some more help.
    My wp-admin page shows the “One or more database tables are unavailable. The database may need to be repaired” message…. so I know I need to add that bit of code. But how and where do I add it? I can’t login… I do not know what address to type in to access this php file. Help please :)
    My website is

  7. Pavanswin

    I am a wp beginner, you guys are awesome, helped me building my careers through this kind of articles. I migrated my website to and struggled with the error for many days and finally found a solution in the article and is very helpful. thank you all

  8. Julio Sanchez

    The original name of the file wp-config is wp-config-sample, I just wrote the original name and vuala no “Error establishing a database connection” message.

    • WPBeginner Support

      wp-config-sample.php file is a sample file not the actual configuration file. The actual configuration file is wp-config.php.

      Admin

  9. Sruly

    I have WordPress with Digital Ocean. I just restarted my server and that fixed the issue.

  10. Rudy

    Thanks, really thanks

  11. Nicholas

    If all fails, also try a swap file:

  12. Brendan

    “replacing localhost with the IP”

    This worked! Thank you so much for saving my ass

  13. Guy Pribyl

    Is there a way to have wordpress when posting this error message do a redirect to a non-wordpress page? I JUST WANT A BACK UP FOR WHEN THIS HAPPENS IN THE MIDDLE OF THE NIGHT.

    Thanks for your help

  14. Avioconsys

    Thanks Syed Balkhi and his team. It took me 5 minutes to solve the issue with this error. It was a bad update of the password for the general user.

    Fast and working. Very nice of you sharing this information.

    M.

  15. Suhel

    i am getting the different error but its something some like this,
    wpsa_options: Table ‘aamtechn_wp949.wpsa_options’ doesn’t exist
    wpsa_postmeta: Table ‘aamtechn_wp949.wpsa_postmeta’ doesn’t exist

    i am getting this message and i am not able to access the dashboard.please help me soon.

  16. Camaal Mustafa Sikander

    Thank you so much, this one fixed the Database error for me in the first step itself.

    Best wishes,
    Camaal

  17. Dudley Rees

    Thank you for your help. The username and password part of wp-config.php fixed it for me.

  18. Robert Varga

    I had this problem, and I spent 2 much hours on fixing it … Finally I solve that mistery. My problem was in htacces file, it only had Rewrite Engine line , I just c/p this “default” htaccess content in it and replace it with file on server, and tadaam it work now :D

    “default” htaccess content :

    # BEGIN WordPress

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

    # END WordPres

  19. Daniel M

    how do i solve depreciation errors for db connection using either PDO or MYSQLi? please help

  20. prince vashisht

    I changed the username (wp_users) from phpmyadmin and now I am getting the same issue.Reverting the changes is also not helping me out.Please help.

  21. Wayne

    When I first experienced this error I was so devastated and I wonder how will I resolve this issue. I did some research and noticed that my database name was the problem. So I went into MYSQL and change the database name. This article explains it all

  22. Kevin Wells

    For whatever reason, I ended up having to recreate my MySQL user in the database. After I did that, my website functioned normally again.

    • Maja

      Change of user also helped me after hours of debbuging

  23. Christine F

    Thanks so much for this!

    I encountered the same issue earlier. I changed the database password and then this error appeared. We have GoDaddy so what I did was just go to the File Manager, open the WP-Config.php and changed the (‘DB_PASSWORD’, ‘database-password’); to the one I recently used. Real simple but wouldn’t have done it without this article.

    Thanks again!

    • Matthew

      Thank you for this reply, Christine. I, too, changed the password and found that to fix the issue as well. Perhaps MySQL had reset it without notifying me.

    • Dutch

      Thanks for this tip! Don’t know how you figured this out from the article. Was about to reload everything..and tried this just in case and it worked. Also on Godaddy, used Installatron to start the installation and uploaded the rest manually.

  24. Alexander

    In my case I solved the problem just updating the password of the DB.
    It seems the password was to weak.

    ciao
    Alexander

  25. sonu arora

    hey i m facing a weird issue with my website, i.e. when i try to check in pingdom then the screenshot generated there shows me data connection error, while when i am opening my website in normal tab or iphone, it gets open. could you please suggest me what to do? i am worried whether my website is showing to public or not. url is thanks in advance!

    • WPBeginner Support

      Try changing server locations in Pingdom test settings. Also try other tools like Google Page Speed tool.

      Admin

  26. Angel

    You say to put this in your wp-config.php file….. define(‘WP_ALLOW_REPAIR’, true);
    you don’t say a single word as to where to put it. I dropped it in right at the top and got a HUGE WP error message. You had it on line 1 without anything to indicate context of where to put it. So most would assume it would go on line 1 at the top. If it doesn’t go there, why not give helpful and specific instructions?

    • WPBeginner Support

      You need to add this just before this line:

      /* That's all, stop editing! Happy blogging. */

      Thanks for bringing this to our attention. We have updated the article.

      Admin

  27. Nica

    when i try this:

    I get an error:
    Could not connect: Lost connection to MySQL server at ‘reading initial communication packet’, system error: 95 “Operation not supported”

    When i test this to php:

    It shows:
    This site can’t be reached

    The connection was reset.
    ERR_CONNECTION_RESET

  28. samchief

    Problem resolved. DB_host was the problem on ipage, it’s quite different from others

  29. RINKU MEHAR

    how could i solve this errors:

    Warning: require_once(C:\xampp\xampp\htdocs\wordpress\wp-admin\admin.php): failed to open stream: No such file or directory in C:\xampp\xampp\htdocs\wordpress\wp-admin\index.php on line 10

    Fatal error: require_once(): Failed opening required ‘C:\xampp\xampp\htdocs\wordpress\wp-admin\admin.php’ (include_path=’.;C:\xampp\xampp\php\PEAR’) in C:\xampp\xampp\htdocs\wordpress\wp-admin\index.php on line 10

  30. FranciscoMary Aghogho

    Please I need an assistance here. I tried changing my site’s url from http to https and now cannot view my site. It say a coonection could not be established. Please what could I do in this instance. Thanks.

    • sourcebreak

      Issue
      If you update from php 5.2 to 5.4 or 5.5 then you will get this error database connection error in wp.

      Cause
      If your password is Pre mysql 4.0 then you will get this error

      Fix
      Login to phpmyadmin >> user >> change password
      type your mysql password >> select >> MySQL 4.1+ compatible and go

  31. Kanif

    I was also facing this issue. I googled it and come to this post. It is really awesome, deep thinking on what all problems may come with this error.
    Mine problem is resolved by doing database repair and optimization.

    Thanks buddy,
    Kanif

  32. ScienceMan

    Thanks for the great post. My situation was interesting, I migrated my server to another account with the same provider. The result was the dreaded “Error Establishing a Database Connection”. Thanks to this article, I knew to have a look at the wp-config.php file.

    When I logged into my new server cPanel and checked the MySQL databases, I immediately spotted the problem. Due to the server migration, the name of the database and user had changed. The databases names at my provider all default to “accountname_databasename”. Also, the database username defaults to “accountname_username”. I suspect other servers are similar.

    Since my server account name changed with the server migration, so did the name of all my databases and database usernames.

    So to fix it, all I did was change these two lines in the wp-config,php file:

    /** The name of the database for WordPress */
    define(‘DB_NAME’, ‘XXXXXX_databasename’);

    /** MySQL database username */
    define(‘DB_USER’, ‘XXXXXX_username’);

    Basically, you want to check carefully to make sure the “XXXXXX” in the wp-config.php file matches what’s at your server.

    Thanks again!

  33. igwe simon

    I mistakenly changed my site and wordpress url to one of my domain so i had to go and change the dns and park it on the website, so i waited for some hours but the domain was still propagating then i read a tutorial (codex.wordpress.org/Changing_The_Site_URL) and followed an option there by editing my functions.php and the next thing i saw was “One or more database tables are unavailable. The database may need to be repaired.”, so i followed the instructions here and still the problem is still active. I need an assistance please.

  34. hafsa munir

    I have following error in my xammp server,how can i resolve it
    he following error was encountered while trying to retrieve the URL: localhost/phpmyadmin/

    Unable to determine IP address from host name .localhost

    The DNS server returned:

    Name Error: The domain name does not exist.
    This means that the system was not able to resolve the hostname presented in the URL. Check if the address is correct.

  35. val girich

    I am seeking free videos on how to use W.P. once it is set up ready to edit, ad docs and so on.

  36. S

    hai
    i done everything as you mentioned in the post but when i opened wp-admin/ then again this error was coming “One or more database tables are unavailable. The database may need to be repaired.”
    So plz help me what to do

  37. Manny

    Thanks!

    I just logged in through FileZilla and checked out the wp-config.php file and double checked everything and realized that I updated my database password but it still showed the previous default password on wp-config.php. So after changing it in the file and saving the changes I was able to access my site again. Your help allowed me to solve my problem, thank you!

  38. Jeremy Smith

    One very important that’s been left out in this post is the “turn it off and turn it back on again” approach.

    If you haven’t actually changed the wp-config file I don’t think you should be going in and fiddling around with it, at least not in the first instance.

    What you should do first is attempt to restart the mysql server.

    To do this (In Terminal)
    1. SSH into your website (you will need your key ‘.pem’ file)*
    2. To restart your mysql type “sudo service mysqld restart”

    This has worked every-time for me.

    *On a Mac
    1. Get your ‘.pem’ file. I got mine from Amazon Web services (hosting the site)
    2. Enable SSH on your Mac – Google it
    3. Open Terminal
    4. Change directory to where you have stored your ‘.pem’ file. -It’s probably easiest to save this on your desktop, then you can just type ‘cd Desktop’ and it should take you there.
    5. Type ‘chmod 400 yourpemfilename.pem (allows you to use this key to get in)
    6. Type ‘ssh -i “yourpemfile.pem” remote_username@remote_host – type the speech marks here
    7. Once logged in (You may have to type y a few times to get you in) type
    sudo service mysqld restart

    • Jcirni

      Restarting works but why does this error keep happening? Happens once a week.

  39. Malkesh

    Hi, my wordpress site gets this error very often,
    we are using AWS, so i need to run this command to restart mysql :

    sudo restart mysqld restart

    the same issue i have also on digital ocean too..

    so i am sure not this is not an server issue, it’s should be some thing in wordpress, that’s put down mysql server..

    Thanks

    • dino

      Same problem here. At least two times per day my site , which is in digital ocean , loose sql connection.
      I have try to optimize my database, bvut nothing works. Any idea?

  40. Glenn

    Thanks,

    When WordPress dies it is scary for a WordPress newbie. I found your solution easy to understand, implement and worked a treat.

  41. Faiza

    Hi,

    This blog was very helpful but I still haven’t been able to figure out this problem. My WP-Config.php file shows me this localhost:

    I am using GoDaddy, I have tried replacing this with local host, my IP address, and (godaddy). Nothing has worked :( Can I get some help here?

  42. Claire

    would the problem be that my domains dns settings haven’t updated yet?

  43. Anupam

    Updating the WP-Config file works for me. Thanks a lot.

  44. Brandon

    WOW!!!! Came across this site because my wife’s business site went down and I was able to fix it in minutes! Thank you!

  45. Cidina

    Thank you soooo much. This post saved my 6-month large project in Wordpress. Trying to switch directories I lost connection to the database. I’m not a .php programmer but the topic is so well explained here that it took me five minutes to fix it after finding this blog. Your are awesome!!!

  46. Nino F

    If you changed your password in phpmyadmin, you have to change it in your wp-config.php aswell, if you don’t do that the website doesn’t work. You have to link the two with eachother.

  47. Eddie O'Hagan

    I was getting this error I think because of the theme I had, I changed the theme and I haven’t gotten the error since, I noticed this because when I ran the repair, the comments tables were the ones that had the errors.

    • dino

      how often did you have this problem?
      I have the same , and i loose connection at least twice per day.
      I tried many things but nothin works. If it is the theme i will try it..

  48. samuel nmeje

    this was indeed helpful. thanks a lot.

  49. Ram

    if you use xampp for wordpress offline then the error is removed by following process:
    open in Xampp folder “\xampp\phpMyAdmin\config.inc.php” and then check username and password
    set the same password to wp-config then the error is not appear

  50. Micah

    This is very helpful. Thank you very much!

Leave A 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.