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

    Thanks it work.

  2. viraj

    Thank you, it worked for me while connecting database from phpmyadmin.

  3. Tom

    I’m trying to install wordpress on my local pc running window 10. I have installed Xampp, made all the changes to use port 8080. No problem starting Xampp. No problem starting Apache or Mysql. I can open phpmyadmin from the admin button without problem and was able to add a database. if I enter http://localhost:8080 it brings up the Xampp page. However when I enter http://localhost:8080/wp to install wordpress the web page times out, but does nothing. I extracted the wordpress files into the htdocs folder and made the changes to the wp-config.php file to use localhost:8080.
    At this point I have no other idea what to do to get the install done and could really use some help.

    • WPBeginner Support

      Hi Tom,

      You will need to copy paste WordPress files in /htdocs/wp/ folder instead of /htdocs/ folder.

      Admin

  4. Sandy

    Thank you! worked!

  5. Jude

    Unable to install word press on godaddy. Won’t even complete the installation. “Application has Errors”. – Need it urgently!! Please help

  6. Shubham Tiwari

    Hello WPBeginner,

    I am currently running Viral Website. Many viral websites have some plugins. I am attaching link of the screenshot Here

    In viral website niche, we need to see how many real time users are there on a particular story because, to see the viralness of new stories we need to see the real time users for a post. We use Google Analytics but Data Mining is too hard in google analytics to see if our story is going viral or not. By looking at the real time users for a particular story, we make conclusions that which of our story is going viral on social media.

    Can you please provide me details? How can i show my Live story readers on the story/article page. I want my users to see how many live users are reading this particular story.

    Is there any script code or any plugin for this type of feature?

  7. Richard

    Brilliant, this guided towards me getting this fixed!! Also had to change upload a new wp-settings.php file.

    Thanks

  8. Destiny

    To allow use of this page to automatically repair database problems, please add the following line to your wp-config.php file. Once this line is added to your config, reload this page.

    define(‘WP_ALLOW_REPAIR’, true);

  9. Ben Horle

    Massive thank you! 1st task fixed my site!

  10. Denis

    Thanks!!! Thanks to you my site work again!
    Happy

  11. Mike

    Had this error trying to install WP at home for testing on Windows laptop. Tried everything and then noticed that my character set in the wp-config.php file where i defined the parameters for accessing the database contained characters that were not standard. The apostrophe character was not the standard windows font character. Once i replaced and made them all the same character WP installed without issue.

  12. Nirav Jagad

    please someone connect my computer with teamwork and resolve my problem
    Error establishing a database connection

    • Bas Dost

      I have the same problem.
      Let me know if you have any suggestions.
      Thanks.

  13. ivano

    Worked for me by replacing “localhost” withe the IP you provided. Thank you!

  14. miranda

    i dont get it

  15. Michael

    [SOLVED] My databases were all working fine – turns out the problem was the password itself. The password contained three symbols &(= which I guess was messing with PHP. I’m not the best in PHP but I suspect the problem was with the symbol “=”. So once I changed the password, it worked just fine. Good luck, hope this helps.

  16. Wills Perkins

    Thanks guys, you’ve saved me from having a melt down

  17. murad

    Thanks. Solved my problem.

    • irnem

      What exactly helped? I checked everything – still doesn’t work. In the section “Check your Web Host (MySQL Server)” they are talking about testconnection.php file, what should I do after I created it and put on WP dir? how can i run this code? (sorry, i’m a beginner in this area)

  18. Ash

    What also can be really handy! Especially if you use Wordpress on IIS6/7/8 on Windows Server.. Check if the MySQL service is running.. And also if it is running on the correct user account! Normally this should be just the Local System!

  19. Gwyneth Llewelyn

    Even those who are not exactly absolute beginners to WordPress get often stumped by this error! Thanks to your systematic approach, I managed to track down the ‘error’ in my installation — a corrupt MySQL database, which was anything but apparent, since all tables seem to be in the right place, and checking/optimizing them would give no errors… but clearly there was something very, very wrong.

    Dumping the database and re-importing it from a backup did the trick!

    Thank you so much :)

  20. Jeff

    Thank you so much for writing this and all of the comments. My site was fixed by replacing the line
    /** MySQL database password */
    define(‘DB_PASSWORD’, ‘[some random numbers and letters]’);

    with
    /** MySQL database password */
    define(‘DB_PASSWORD’, [my usual wp-admin login password]’);

    Not sure if this is correct way to fix it but it did work.

  21. Brett

    I went back into my cpanel and re-inputted the same passwords for my 2 of 4 sites that weren’t working and that got them back up and running.

  22. STANISLAUS

    I am glad I was able to fix this issue on my site after 4 days. In my own case it said the wp-option table crashed. And I ran the repair, the result showed it was not successful but in actual sense, it was repaired.

    After I got that unsuccessful repair message, I initiated a like chat with my service provider but they checked the site and informed me that that my site was alive and I was happy.

    Thanks

  23. Jigar Shah

    I did tried all the above solution . But nothing worked out for me .
    Then I tried looking @apache logs . It was brute force attacks against xmlrpc.php from crashing my WordPress server.
    The simple solution that worked for me was to block the IP’s
    I found out here

  24. rahul

    My wordpress website is running but after some time it shows an error “Establishing database error” then i have to start “service mysqld start” please help me so that this type of error not come again. thanks

  25. Kristaps

    Thanks, i tried some things, but in the end, i found out, that my config file, generated random password, when i actually changed it to specific password.

  26. Jill

    This was so helpful. This along with people’s comments/experiences would be quite a handy resource or guide. I’ve noticed a few job posts for WordPress error testers and started researching and found this page.

  27. Lothre1

    Hello guys. Just because this is the first page of google on this subject I’m complementing the information. In my case i downloaded my wp-config from my prod website. At the end of the file right after “define(‘WP_DEBUG’, false);” i had the following configuration.

    define( ‘WP_ALLOW_MULTISITE’, true );
    define(‘MULTISITE’, true);
    define(‘SUBDOMAIN_INSTALL’, false);
    define(‘DOMAIN_CURRENT_SITE’, ‘MYDOMAIN.com’);
    define(‘PATH_CURRENT_SITE’, ‘/’);
    define(‘SITE_ID_CURRENT_SITE’, 1);
    define(‘BLOG_ID_CURRENT_SITE’, 1);

    I tried to keep everything as it was but I replaced the mydomain.com with my ip + port.
    An hour later I realize that commenting the lines below fixed the problem:

    //define(‘MULTISITE’, true);
    //define(‘SUBDOMAIN_INSTALL’, false);
    //define(‘DOMAIN_CURRENT_SITE’, ‘MYDOMAIN.com’);

  28. Karina Jaramillo

    I tried logging in to maintain site but I get this error “Error establishing a database connection”. I also get that error when trying to access wp-admin. Nothing has been changed on our end. Help!

  29. Shirley

    Help!
    We get an error message on our wordpress for “Error establishing a database connection:
    This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.

    Are you sure you have the correct username and password?
    Are you sure that you have typed the correct hostname?
    Are you sure that the database server is running?

    We’re puzzled how this happened. If you can help, that would be great. Thank you.

  30. jabr

    Thanks a lot. you really helped me get my website back to work

  31. SAMUEL

    Please, am having problems operating my wordpress dashboard. I have been updating my website through my dashboard for some days now. But just yesterday i began to receive this message…… Fatal error: Cannot call overloaded function for non-object in /home/content/n3pnexwpnas01_data02/34/3566434/html/wp-includes/capabilities.php on line 448……..can anyone tell me what to do or what was the cause of this plssssss.
    Sam.

    • WPBeginner Support

      Most likely this issue is caused by a WordPress plugin. Try these WordPress troubleshooting tips to find out the culprit.

      It is also possible that the error is caused due to incompatibility with the PHP version. Ask your WordPress hosting provider about the PHP version they are using and see if they can upgrade it. The minimum requirement is PHP version is 5.6 or greater for WordPress.

      Admin

      • SAMUEL

        thanks a lot, i only change the theme and that fix it, i so much appreciate.

        • WPBeginner Support

          Hey Samuel, we are glad you found it helpful :) Don’t forget to join us on Twitter for more WordPress tips and tutorials.

  32. Elijah

    I’m also having the same problem but mine is so terrible because I can’t access my cpanel. Please what should I do?

  33. James R

    After looking at this page and thinking “I have no idea what I’m doing”, I just rebooted the server and now everything’s hunky-dory again. Any idea why that was the case?

    The only thing I can think of that may have caused it is that I lost my internet connection whilst logging in, but why should it?

  34. Davo C

    THANK YOU VERY VERY MUCH. Had a corrupt db user, would have had no clue without this article.

    Life saver.

    Davo

  35. Sat

    When I cloned my MySQL database for setting up a staging site I received the same error. I found out that the MySQL user setup for the WP to access the database needs to be given permission to access the database through the CPanel -> Mysql databases -> add a user to the database option. Otherwise, you will get “Error Establishing a Database Connection” issue when trying to access the admin page through the URL.

  36. Anoop Arya

    Error establishing database connection .

    I did not work after trying few steps. I thought let’s wipe out all the information. S o reset MySQL deta base . Uploaded new WordPress. Now still it days error establishing database connection .any help and suggestions

  37. Ademide

    The second fix worked.
    Thank you so much.

  38. Karen

    This worked for me, thanks. I moved a five domain multisite with very little trouble. The only tweak was to the table_prefix of the database tables. For some reason mine weren’t prefixed with wpcc- . This was easily edited in wp-config.php. After that I had to do some wrangling with tech support to point the subdomains at the directory where wp was installed. They couldn’t understand why I’d want all of them pointing at the same place. Once they had given in and done as I asked it all worked fine.

  39. Manohar

    Thank you for providing this useful informatrion

  40. Emmanuel

    Thanks a whole bunch, You really saved my day

  41. Jeroen Thans

    I had the same problem,

    i also got after a restore from backup an error: Error establishing a database connection

    i checked the database with phpmyadmin under the database for wordpress and then wp_options. And there was no sign of something called siteurl

    So i pressed insert -> option_id = 1
    Option_name = siteurl
    Option_value = http://www.example.com

    and added it. Then went back to the website http://www.example.com/wp-admin and i could restore it

    hopefully it helps.

    Greetz J

  42. JCMill

    *Update to my earlier comment*

    I tried generating & applying new passwords via cPanel. On 3rd pw change I still got the can’t load the DB error but it allowed me to run the WP Repair.php !
    Everything showed as OK.

    Now the site is back.

    Highly irritating & weird.

  43. JCMill

    I just started seeing this error. None of the above works :(
    Also tried renaming the DB, & running the ‘check’ & ‘repair’ options in cPanel = ‘OK’ on all elements.
    I can access & edit the DB via phpmyadmin.
    wpconfig is byte for byte identical to my original (then working) backup.

    The testconnection.php has revealed this –
    ‘Could not connect: Connection using old (pre-4.1.1) authentication protocol refused (client option ‘secure_auth’ enabled)’

    Any ideas appreciated.

  44. Bim

    I seem to encounter the error on all other the pages except the main page. The main page loads fine but when i click a link to open another page that’s when I get the database connection error.

    Has anyone encountered this? Aren’t all the pages including the main page use the same database connection?

    • WPBeginner Support

      May be you are seeing a cached version of your main page? If not, then contact your web hosting service provider.

      Admin

  45. sohanpurohit

    Error 404 error403 same prob,how do repairinp this prob?

  46. djack

    it works inmediately after put the ip i’m receiving from the router 192.168.5.8 in my particular case.

    localhost, 127.0.0.1 weren’t working.

  47. David Draper

    I copied a PHP script I found online called testconnection.php. The script captures and echos the connection error. I ran the script from the command line using ‘php -f testconnection.php’. The error told me that there was a problem reading the mysql.sock file. When I checked the indicated location, there was no mysql.sock file. I created an empty file called mysql.sock in the indicated location using touch and ‘Voila!’ everything has worked fine since.

  48. SriMarv

    I had the same problem on my site too, i only changed the line for DN Host to

    define(‘DB_HOST’, ‘localhost’);

    in the config file, it was pointing to an IP address which happened to be the one for the cpanel.

  49. sudheer

    Please fix my issue. I am trying to restore a database backup from mysql server, But restore fails the following error32 will persists.

    TITLE: Microsoft SQL Server Management Studio
    ——————————

    Restore of database ‘PROD_SALES_DATABASE’ failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)

    ——————————
    ADDITIONAL INFORMATION:

    System.Data.SqlClient.SqlError: The operating system returned the error ’32(The process cannot access the file because it is being used by another process.)’ while attempting ‘RestoreContainer::ValidateTargetForCreation’ on ‘C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\PROD_SALES_DATABASE.mdf’. (Microsoft.SqlServer.SmoExtended)

    For help, click:

    ——————————
    BUTTONS:

  50. Barbara

    Thank you so much : it worked instantly !!!
    Cheers
    Barbara

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.