Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Fix the Error Establishing a Database Connection in WordPress

Are you seeing the ‘Error establishing a database connection’ notice on your WordPress website? It is a fatal error that makes your WordPress website inaccessible to the users.

This error occurs when WordPress is unable to make a connection to the database. A number of things can affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.

In this article, we will show you how to easily fix the error establishing a database connection in WordPress.

Fixing the database connection error in WordPress

What Causes Error Establishing a Database Connection in WordPress?

The ‘Error establishing a database connection’ issue can be caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.

Database connection error in WordPress

A database is a software which makes it easy to store, organize, and retrieve data into 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:

  • Database name
  • Database username
  • Database password
  • Database server

This information is stored in your WordPress configuration file called wp-config.php.

If any of these items are incorrect, WordPress would fail to connect to your database server, and you’ll 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.

Let’s take a look at how to fix error establishing database connection issue in WordPress with step by step troubleshooting.

Video Tutorial

Subscribe to WPBeginner

If you’d prefer written instructions, just keep reading.

1. Check Your WordPress Database Credentials

The most common reason for database connection error in WordPress is incorrect database credentials. If you have recently moved your WordPress site to a new host, then this could be the most likely reason.

Your WordPress database credentials are stored in the wp-config.php file. It is the WordPress configuration file that contains important WordPress settings including database information.

If you have not edited wp-config.php file before, then take a look at our guide on how to edit wp-config.php file in WordPress.

You’ll 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' );

You need to 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. Simply log in to your hosting account and click on MySQL databases under the database section.

We’re using Bluehost dashboard in our screenshot, but the process will be similar in other hosting control panels.

Database information

This will take you to the database management page in your hosting dashboad. From here, you can find out your database name and the username.

Database name and username

Below that you will find the database users and links to change the user password.

Changing database user password

Once you have confirmed your database name, username, and password, 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. Continue reading for more troubleshooting steps.

2. Check Your Database Host Information

If you are confident that your database name, username, and password information is correct, then you may want to make sure that 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.

You need to contact your WordPress hosting company to confirm your database host information.

3. Repair WordPress Database

If you are getting a different error on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then 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 ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

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

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 code from your wp-config.php.

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. Basically, your host server just cannot handle the load (especially when you are on shared hosting).

Your site will get really slow and for some users it may even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.

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 try to access phpMyAdmin and connect the database.

If you can connect, then we need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Make sure to replace the username and password. You can now upload this file to your website and access it via web browser.

If the script connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong.

Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).

Other Solutions That Have Worked for Users

If the above-mentioned troubleshooting tips failed to fix the database connection error on your website, then you may try these additional steps.

Reported by our users, these steps have helped some users resolve the database connection error on their websites.

1. Update WordPress Site URL

Try updating the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your hosting account dashboard, and select your WordPress database.

Running SQL in phpMyAdmin

After that click on 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'

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 server, 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 from Codeable that can help you fix this issue for reasonable rates. The best part is that these developers are highly-vetted by Codeable team, so you know they can be trusted.

We hope this article helped you fix the error establishing a database connection in WordPress. You may also want to see our WordPress troubleshooting guide for tips on resolving WordPress issues on your own.

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.

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

648 CommentsLeave a Reply

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

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

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

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

    Thanks

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  18. 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’);

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

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

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

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

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

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

    Life saver.

    Davo

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

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

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

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

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

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

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

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

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

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

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

Leave a Reply to irnem 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.