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.

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

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

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

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

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

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

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

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

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

    Best wishes,
    Camaal

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

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

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

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

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

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

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

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

    ciao
    Alexander

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

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

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

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

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

    • 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

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

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

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

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

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

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

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

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

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

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

  30. Thanks,

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

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

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

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

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

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

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

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

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