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

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

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 using a service like Seahawk to help you fix this issue for reasonable rates. The best part is that Seahawk has a dedicated team of expert developers, 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, or our expert picks of the best tools for WordPress freelancers and developers.

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

661 CommentsLeave a Reply

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Riya Mehta says

    Hey,

    Today I got the same error and I fixed this by giving all privilege(Permissions) to user under the database defined in configuration file that is wp.config file.

  3. bog says

    What happened to me was that my hard drive (linux) spontaneously went into read-only mode. Out of nowhere. I know this is an obscure cause, but nonetheless I thought I’d pitch in. (usually the sign of a dieing drive, I’m told).

  4. Masood Ahmad says

    Hi everyone,
    In my case it was very simple. The password for MySQL database and the password in the wp_config.php must be same, ignorant of the fact, I changed the password for mysql database and remained upset the whole day. Until by chance I reverted the change. My site was up and live in the next second.

  5. Jamie says

    When i rename index.php to index_.php — then the back end of the website loads up fine

    Pages on the website give a normal ‘Not Found’ Apache error

    ——

    When i restore the index.php filename — then nothing works, and I receive ‘error establishing a database connection’ on public facing pages. And wordpress’ own ‘error establishing a databse connection’ text box when I visit /wp-admin

    Any help you can offer will be much appreciated!

  6. Tony thomas says

    When you are operating in google cloud, you may often receive this error if your RAM is using more resources.

    login to google cloud

    Find the instance from ‘compute engine’. Click on the top menu to find ‘compute engine’

    login to SSH

    type sudo bash (to enter as root)

    Then type/copy paste :

    systemctl start mariadb.service (in centos 7)

    error establishing database will be solved instantly.

  7. Tim says

    Thank you for this article…it’s the most useful on the web I would say.

    I still have the problem anyway (host is Godaddy)…I created the testconnection.php page and i get this error message :

    Warning: mysql_connect(): Access denied for user ‘username’@’hostIP’ (using password: YES) in /home/content/n3pnexwpnas01_data02/52/3027952/html/wp-config.php on line 106
    Could not connect: Access denied for user ‘username’@’hostIP’ (using password: YES)

    – username being the username I use to connect to database via Phpmyadmin
    – password being the password I use to connect to database via Phpmyadmin
    – hostIP being the hostname value I found in the database variables in Phpmyadmin

    I have been stuck for hours now…can you help me on this ? I have no idea what to do and how to do it…

    Thank you in advance

    • WPBeginner Support says

      The most likely reason for this is incorrect information in your wp-config.php file. This could be your MySQL host, username, password, or database name. IF you are using hostname IP, then try adding port to it. Ask your web host for support.

      Admin

  8. Prashant says

    Dear Friends,

    I M New and very little to know about wordpress today i face a prob

    when i try to publish a new post or update the older one and push the button publish or updates its automatically redirected to home page nothing happen not change in updated post not publish a new post please help me if anybody know about such an issue.

    thanks

  9. Robert DeRobertis says

    Thank you, Thank you, Thank you.

    I was hacked recently and changed the passwords to my website to something more secure. I missed updating the config file. This saved the day.

  10. Cássio Sá says

    My friend, i’m having tears of joy right now, you saved my life. The problem was in the config file. I imported the old database and changed the credencials on it, but haven’t changed the credencials in the config file. Thank you very much for the tip.

  11. Ali says

    I just change http to HTTPS Now my site is not opening and showing same error please tell me what to do now ?

  12. Paul says

    Hey great article about how to troubleshoot database connection errors.

    Might be good idea to link to your old article about making a custom dB error page. Otherwise if utilising good caching program it can take a while before actually realise something is wrong. This article and the mail function means you can be notified instantly and provide a nice image and custom text which is more professional than the default message.

    https://www.wpbeginner.com/wp-tutorials/how-to-add-a-custom-database-error-page-in-wordpress/

  13. stephanie wandke says

    Thanks for this article! It was definitely a database user issue and when I added a new user and password, changed the wp-config file, BAM the site was back. You made it easy and I solved my problem in under 15 minutes. Thank you!

  14. Edward says

    Godaddy customer. Getting this same issue.

    Database was working fine. What worked for me was to go into wp_config and change the following line:

    define(‘DB_HOST’, ‘localhost’); [the db_host was something different, not localhost]

    to

    define(‘DB_HOST’, ‘[my IP address]’) and it worked fine

    Thanks for the tip!

  15. Shakti says

    Sir help me please
    My website is loading, and working fine but I get error establishing database connection when I tried wp-admin and again I reloaded page 3-10 times and my site is working. Again I get that error unexpectedly.

  16. Shelly says

    My site goes down and I get this same error message at the same time every night. It lasts for about an hour and a half, and then the site is back up again. Would that indicate an issue with my hosting service?

  17. Liesje says

    Thanks a lot for this article ! Piece of cake this way :-)
    Saved me a lot of work and headaches.

    Kinders regards,
    Liesje

  18. praveen says

    i have error in wp-admin page. its says Error establishing a database connection. how to solve it .

  19. Ian Wells says

    This occurred once for me on my EC2 AWS instance. The cause was I am using a small t2. micro server, and I sometimes got too many httpd requests coming in, especially when I turned on my AWS health checks which increase traffic. I reconfigured my /etc/httpd/conf/httpd.conf per instructions here: https://wordpress.org/support/topic/how-to-optimize-apache-for-wordpress and so far, so good. I no longer see 20 or more httpd processes running at once, followed by out of memory and data base disconnected.

  20. Ian Wells says

    This error kept occurring. I could clear it by rebooting EC2 but not very satisfactory. Then I noticed my EC2 installation was out of date, so I ran “sudo yum update” on the server. Looking much more stable now.

  21. ani says

    nice …exhaustive ..detailed information …i fought with my hosting guys and demanded a root cause for slow DB performance /connectivity…these guys kept recommended to upgrade hosting account to dedicated setup ..POST gave insights on probable problems and ways to tackle .

    Thanks !

  22. alexsbutler says

    You can also try Kernel for Word file repair tool to repair your corrupted Word file easily.

  23. Ken Pyle says

    Thanks for the article. Lunarpages is my host and around 2:30 AM (thanks Jetpack for monitoring my site), my site quit working and I also had the “Error Establishing Connection to the Database Error.”

    How I fixed the problem was to re-enter the password into the MySQL database (got there via Cpanel). I used the same password that was already in the config file, so that way I only had to change one variable.

    I have tried it on 3 WordPress installs so far and it seems to work, as Jetpack informed me that my site is back up after being down for 16 hours.

    It is a mystery as to why it disconnected.

    • Jason Teale says

      I am on Lunarpages as well. So basically, I just have to re-enter the the blog password and that is it? What was the one variable that you changed?

      • Mike B says

        I also am on Lunarpages. I also needed to re-enter my password in C-Panel. Something must have happened with Lunarpages this morning.

  24. Harm says

    I made some changes in wp_user and wp_metadata to create a new admin. Then I get this message. I tried to look at the wp-config-file, but get the same message! What can I do more?

  25. Scott says

    Sort of silly looking back at this, but it might help someone else so I think it’s worth sharing. Recently had a site that was set up to use Cloudflare as it’s CDN. All the DNS “A” records were set except for the one for “mysql.domain.com”, so even using the testconnection.php trick didn’t work (on Dreamhost that doesn’t use localhost). Simply added the “A” record in Cloudflare and life was good again.

  26. Mohamed Azazy says

    You can also restart your MySQL service if you’re using VPS.
    – sudo service mysql restart

    Worked for me!

  27. Michael D says

    Thanks for this article, it helped!

    We had the error occurring in both the root domain and wp-login. Changing ‘localhost’ to the IP in wp-config.php fixed it.

  28. Rav says

    It appeared my ‘localhost’ was not working so I just added the direct address of my hosting provider (mysql.hostinger.co.uk) and everything started working again!

    Thanks for the help!

  29. Christopher says

    Actually, I’m getting “Error establishing a database connection” in my home page only. All other pages are working fine. Please let me know what to do.

  30. Adrienne says

    My old client has this issue now, and her site is about 3 years old and hasn’t been updated. I think the theme and all the changes has corrupted this site. It needs to be scrapped and redone…. now, I have to find a simple way to do it. lol

  31. Nicole says

    I was moving a multisite system- I had to switch out the old website name in my database, and in the .htaccess and wp-config files

  32. m3d01 says

    Hello guys,

    I found the problem that my hosting service made update and that lead to change in the DB_HOST number so i change it back by the new one by edit the config file and the site is back again to work finally :)

  33. John Bert says

    Recently I have transferred from Arvixe to Phi9 web host.
    Actually I forgot to change the previous host “MySQL Connections Strings” in wp-config.php. Now it’s working.

    Thank you for your great help I really appreciate it! :)

  34. Micky says

    I have seen this error. I was putting the wrong MySQL port in connection line. Your post helped me fix it. :)

  35. Carl says

    This seems logical. But I go through each step and nothing changes. Why is this so complex?
    I liked the idea of the database repair but it appears that WordPress would need to be up and running to hit the Repair Database button. I’ve been at this for 2 days. Is there a place to go where I can find the problem by looking at some code? This all seems so vague.

  36. Leonard Rattini says

    As I worked in computer application design for 30-years, and I’m also registered as a Certified Computer Professional, many people, to impress, use the “database” term incorrectly.

    Database is a different data retaining methodology than what was use before its time. Back in the earlier days, data was retained on liked related 80-column cards and collectively they were know as a file. For instant many invoice records, collectively were known as the Invoice File. Medical records collectively, were known as a Medical file, etc.

    In early big main frame computer days, before PC’s that came along, they processed these “80-column fixed-records.” With the advent of the Internet and distributed multiple keyboards, data then was recorded as “data packets” and they are stored in a “database,” which is a three-dimensional format structure stored in the computer. Then at the right time, to process, data items needed “linked’ data packets then allowed for this to happen which then looked like a record we are familiar with.

    An application must be designed as a database base application that includes a Database Administrator person, otherwise its application is not a database application.

    A database application served two main purposes versus the old file methods used in the olden days before the Internet and distributed keyboards. One was to capture data items at first point of (data) entry. And secondly, it is intended to eliminate “data-redundancy.” Leonard Rattini, CCP (to know of my CCP credentials, google ICCP)

  37. Toby Harding says

    I’ve seen this error a few times now, with my WordPress that is hosted on an Amazon EC2 box in their AWS cloud. Simple way to fix the issue is to restart the instance from the AWS admin console.

  38. Unknown says

    Hey, i am getting this error while installing wordpress “Error establishing a database connection” what should i do to get rid of this error .I’ve tried everything like changed database name , deleted previous databases but still wordpress is not is not installed yet. Please help !

  39. Friday says

    I can’t help it, since i created my site i haven’t touched that area can you help me to do it

  40. mikhail says

    Thanks for the article, it helps.
    For me it’s because I change my database user password from CPanel but apparently the wpconfig.php file won’t change automatically when i changed my database user password .

    I have to change it manually in
    define(‘DB_PASSWORD’, ‘[insert your password here]’);

  41. James Kalmakoff says

    The article was very useful and I resolved my Database connection problem. A further place to look for errors is in the MySQL database. Be sure that the ‘database_username’ and the ‘database_name’ are correctly assigned to each other and that there are no typos!

  42. Dawn says

    I just got this error, I copy and Pasted a page to a word document, I saw that there was a paragragh added with ads and links to sites like viagra ect. I removed the ad/links paragragh and re-pasted the text back into my webpage. I was on the second page and posted the updates, when I received this error…any one else had this?

    • J says

      Hey – this won’t help your problem, but you shouldn’t be using Word to edit these files. You want to use a simpler program like Notepad/TextEdit etc.

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

WPBeginner Assistant
How can I help you?

By chatting, you consent to this chat being stored according to our privacy policy and your email will be added to receive weekly WordPress tutorials from WPBeginner.