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. hello I need your help please help me my website is showing Error establishing a database connection
    how can I solve I don’t know please help me

    • You would want to follow the troubleshooting steps in this article for that error.

      Admin

  2. I had this very same issue while using Xampp. After using the “testconection.php” file and through some further trouble shooting, I discovered that my user account was set to connect to “Any host” and by changing it to “Localhost” the problem was solved. I suspect this error came about when I updated Xampp and recreated the same named user.

    • For my case the correct response was by Daniel.
      …I discovered that my user account was set to connect to “Any host” and by changing it to “Localhost” the problem was solved.

      Thanks so much!

    • The methods in this article should be able to help with localhost sites as well as hosted sites.

      Admin

  3. After succeed the “Check your Web Host (MySQL Server)” test I change the host from 127.0.01 to localhost on the config.php file.

    That worked for me.

  4. hi, it is happening to my WordPress site only sometimes and when I reload the page site starts working again please suggest me a solution, for now, I am using Cloudflare as cdn and wp rocket for caching plugin, the theme is newspaper premium.

    • If it is intermittent like that, you would want to reach out to your hosting provider and let them know and they should be able to assist.

      Admin

  5. Same exact error i got “Error establishing a database connection” but not all of the sollution you mentíon helped me.

    For me the MySql services was disabled: I went to services and start the MySQL to automatic. The other thing was the ” log on ” to user was administrator and changed it to the account i created.

    Now my wordpress site is working again.

  6. I have a weird situation is, that the error establishing a database connection is only happening to one post in my blog while other posts are not showing this issue.

  7. I am receiving this error one time on 3 and not always! I do not understand why and how to solve the problem

    • You may want to try reaching out to your hosting provider to see if their error logs have information that can help find the root of the issue.

      Admin

  8. thanks so much, worked great, really appreciate how fast and to the point the video was, instead of a 7-10min extended one, boom, stright to the point!

  9. Thanks so much for this post. Saved me lots of time troubleshooting. I had changed my database password after a web hack and forgot to change wp-config file. Your instructions are spot on.

  10. I fixed it by deleting the MySql database and making a new by importing the backup. I did not need a fresh instance of WordPress.

    • Same here! After hours of debugging, reinstalling MySQL and WP, deleting the wordpress database was the solution. Thanks Parker!

  11. I’m not a developer or have any background in file managing and I could follow your instructions easily. Either I’m a genius or you’re awesome! Or maybe both!

  12. Thanks for the article.
    I had this error while setting up a local instance of WordPress running on Wamp server on windows.
    *For me, it got solved when I changed the blank password of the localhost from phpMyAdmin panel, * the password was blank so I suspect that’s the reason, or maybe changing the password updated some necessary files automatically.
    Just wanted to share this ^-^

  13. Dear sirs,
    I’m a beginner making a big mistake of using all-in-one migration to export my website to a whole package(a “.wpress” file, containing old database information inside) unless using separated database files and then restored it on my new host.
    After that my website display”Error establishing a database connection”.
    I have tried checking the config.php, but the database name is correct to my new host.
    I now understand that’ s a terrible way to migrate my web, however, I don’t have any backup. Is there any method can help save this problem?

  14. Thank you very much for the guide,
    I follow step by step, and now my web is better.
    The tutorial is very useful

  15. Thanks very much for the article. I’m new to WordPress, and this error was pretty disconcerting. I got the error after a simple page edit today – not even fiddling with back end files. Your advice on editing the wp config file fixed it straight away. Thanks again!

  16. Thanks immensely for the article. Although my problem wasn’t identical to the problems given, changing the hosts file managed to get me pointing at the new (unpropogated) version of the web site and replacing localhost with the ip address in wp-config took care of all the problems with pointing to the active version of the site. Now the new site is fully tested and I can shift servers safely. Well done!

  17. I am getting this message when trying to add photos to my product page

    Connection lost. Saving has been disabled until you’re reconnected. We’re backing up this post in your browser, just in case.

    Can please some one help me

  18. Thank you so much for sharing this step by step tutorial. Actually, I have installed WordPress first time, it shows me “Error Establishing A Database Connection” and I don’t know what to do. Then I have searched on Google and I found your this article and really it is
    very helpful for me and also many people who have no idea about this things.

  19. I am getting this quite often on my reseller account and it affects my websites. I honestly don’t know what to do at this point. While my host company has been very supportive, it happens just about every time: jiffynaija.com

  20. Hello People,

    Firstly i appreciate the effort taken by wpbeginner for creating all these articles and helping out people from their bad nightmares.

    Here come my question, i had the same trouble i tried all the steps which was given below and it worked, but after every few hours later this screen appears again and again, now what is did is i restarted the server it worked but still i’m getting the same page every time. please somebody gimme a permanent solution for this it will be much appreciated. TIA

  21. I had moved from one host to another. Was getting the error. I had used a different database prefix on the new host. Changing this in the wp-config.php solved the problem.

    /**
    * WordPress Database Table prefix.
    *
    * You can have multiple installations in one database if you give each a unique
    * prefix. Only numbers, letters, and underscores please!
    */
    $table_prefix = ‘newprefix_’;

  22. I battled with MAMP on MacOS Sierra ver. 10.12.6 for a couple of days. After trying what seemed like Everything, I finally gave up. With MAMP, MySQL would not start although Apache did & PHPMyAdmin was throwing the same error about MySQL. I uninstalled MAMP and installed DesktopServer and I’m so glad I did. It worked like a charm. It was incredibly easy!

  23. i have issue when open any post it shows this error what should i do how to fix this error—>

    Notice: getTemplate was called incorrectly. templates/single//layout.php does not exist. Please see Debugging in WordPress for more information.

  24. I’ve created a site on localhost. I am using MAMP. I tried to go live. When I changed the user password in mp-config file the error of not able to connect to serve appeared. I tried to change it back but if have been unable to get back to my login for wp. That’s where I’m not understanding. How do I know where the password and username should be matching up?

    • Pls am trying to install WordPress on localhost and after entering my database username instead of taking me to the installation page, it keep bring error that “Error establishing a database connection”. Pls what can cause this error? and have done all the necessary things

  25. As soon as I saw the error I was like oh god there goes my day trying to sort this out! I hadn’t touched wordpress or my hosting account for ages, had forgotten everything and was like okay back to square one here.

    BUT

    This article’s fix worked like a charm (changing the password to the database and making sure it was the same in wp-config). Saved me a huge headache.

    THANK YOU!

  26. hi, thanks for the effort to bring this post up.
    after some hours that i spent twiking and ftp-ing and phpmyadmin-ing, i finally got all my settings super-perfect, but still this anoyying message.

    so- i just openned my chat with bluehost, and once again they proved to be really fast in answering. with a click it was all up and alive.

    so- next time it will take me less time to open-up the chat box with them

  27. Now to find out why it does this on localhost when there is NO config file yet! Trying to install for first time in another folder, but get this error? Saying config in wp-config might incorrect? Well ya… there is no file!

  28. Thanks for the article and the video, I fixed my problem as well.

    But why does it happen? very strange.

  29. I don’t understand WHERE to find the file to access it and change it seeing as I can’t get into my WordPress account :(

  30. I’ve encountered the no database connection error. I can run a connect.php script using root and password which says connected successfully. To what is my question. Just to my localhost server? root and password I used were those for my phpMyAdmin login.

    A similar connect_db script using the actual wordpress database user and password fails. user and database password match the wp-config values.

    I’ve used the user root and its companion password in the wp db table but still get connection error.

    Any help would be appreciated.

  31. Has anyone seen this error? I’ve searched and searched, but can’t get an answer:

    INSERT INTOwp_contact_form_7(cf7_unit_id,title,form,mail,mail_2,messages,additional_settings) VALUES (1,’Contact form 1′,’Your Name (required)\r\n [text* your-name] </p

    #1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ”Your Name (required)\r\n [text* your-name] </p’ at line 1

    Thanks!

  32. its a nice read but having to call your provider to fix the permission issues fo r you… come on :)

  33. i found another solution.
    Host: localhost to Host: 127.0.0.1:8889
    but I also changed the wp-config-sample.php
    /** MySQL hostname */
    define(‘DB_HOST’, ‘127.0.0.1:8889’);

    and

    define(‘WP_ALLOW_REPAIR’, true);

    Save and reload tab.

  34. I have this issue once in a while on a local server after server reboot. The solution that has always worked is to stop httpd, then restart mysqld, then start httpd again. Had always fixed the problem. Probably not a solution if your site is hosted of site.

  35. Suddenly, my localhost stopped saving anything. Tried installing a new WordPress but whenever I’m to login to the back-end, the right or any password or login credentials won’t open it.
    What is the problem and what is the solution?

    • Also, Database repair didnt work for the project I had on. I’ve uninstalled xampp numerous times, still didnt work

  36. After doing the database repair, I got this:

    wp_1_posts: Table ‘nb_blog.wp_1_posts’ doesn’t exist
    wp_1_comments: Table ‘nb_blog.wp_1_comments’ doesn’t exist
    wp_1_links: Table ‘nb_blog.wp_1_links’ doesn’t exist
    wp_1_options: Table ‘nb_blog.wp_1_options’ doesn’t exist
    wp_1_postmeta: Table ‘nb_blog.wp_1_postmeta’ doesn’t exist
    wp_1_terms: Table ‘nb_blog.wp_1_terms’ doesn’t exist
    wp_1_term_taxonomy: Table ‘nb_blog.wp_1_term_taxonomy’ doesn’t exist
    wp_1_term_relationships: Table ‘nb_blog.wp_1_term_relationships’ doesn’t exist
    wp_1_termmeta: Table ‘nb_blog.wp_1_termmeta’ doesn’t exist
    wp_1_commentmeta: Table ‘nb_blog.wp_1_commentmeta’ doesn’t exist

    What can I do to fix this?

    Thanks,
    Nathan Pinno

  37. My WordPress site (2 server, One web and One db) works perfectly at the first run. if I reboot the web server it shows the database errore.

  38. will thi sbe fixed as you are saying in this article i will have to go for it than only i can say any thing about this site .

  39. mine also broke down without me making any edit on the website whatsover for like 2 months, i got scared… so this is how i ended up here looking what to do, but reading all this i just refreshed the page and its working, somehow automatically… i dont know how long i had this error was there for but its between minimum 10min and 2 hours max, the host im using is hostgator and just before i was to contact them i refreshed the page and its working, so my advice if anything you can atleast wait 2 hours and it may turn back on like mine, PEACE!

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