WordPress Made Easy

How to Fix the Error Establishing a Database Connection in WordPress

Is your WordPress website displaying the message “Error establishing a database connection”? It’s a fatal error that prevents users from accessing your WordPress site.

When WordPress is unable to connect to the database, this error occurs. A variety of factors might affect your WordPress database connection, making it tough to troubleshoot for newcomers.

We’ll teach you how to fix the issue by creating a database connection in WordPress in this article.

In WordPress, how to fix the database connection error

What Causes is WordPress Error Establishing a Database Connection?

Incorrect database information in your WordPress settings, a faulty database, or an unresponsive database server can all cause the ‘Error establishing a database connection error.

WordPress database connection error

A database is a piece of software that makes it simple to store, organize, and retrieve data for use in other programs.

WordPress uses a database to store all of your content and other website data as a content management system. Each time someone visits your website, it connects to the database.

For connecting to the database, WordPress requires the following information:

  • Name of the database
  • Username for the database
  • Password for the database
  • Server for databases

This data is saved in the wp-config.php file in your WordPress installation.

If any of these items are incorrect, WordPress will be unable to connect to your database server, and you will receive an error message stating “Error establishing a database connection.”

One of the most common WordPress mistakes is this. This error can also display if the database server is offline or the database files are faulty, in addition to invalid credentials.

Let’s look at how to troubleshoot the error establishing database connection issue in WordPress step by step.

1. Make sure your WordPress database credentials are correct.

Incorrect database credentials are the most prevalent cause of database connection errors in WordPress. This is most likely the case if you have recently relocated your WordPress site to a new host.

The wp-config.php file stores your WordPress database credentials. It’s the WordPress configuration file, and it’s where you’ll find all of your critical WordPress settings, including database information.

The following lines should be found 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 must double-check that the database name, username, password, and database host are all valid.

This information may be verified through the dashboard of your WordPress hosting account. Simply connect to your hosting account and go to the database section to find MySQL databases.

Information from a database

This will send you to your hosting dashboard’s database management page. You may get the name of your database and the username from here.

Username and database name

The database users are listed below, along with links to update the user password.

Changing the password of a database user

You can alter your database name, username, and password in your wp-config.php file if necessary once you’ve checked your database name, username, and password.

After that, return to your website to see if the database connection error has been resolved.

If the mistake is still visible, it suggests that something else is incorrect. For more troubleshooting tips, keep reading.

2. Verify the host information for your database

If you’re positive that your database name, username, and password are accurate, you should double-check that you’re using the correct database host information.

Localhost is the database host used by the majority of WordPress hosting businesses. Some managed WordPress hosting companies, on the other hand, host databases on different servers. Your database host information will not be localhost in that scenario.

To confirm your database host details, contact your WordPress hosting company.

3. Restore the database of WordPress

If you see a different issue in wp-admin, such as “One or more database tables are unavailable,” here is what you should do. If you receive the message “The database may need to be repaired,” you must repair your database.

Add the following line to your wp-config.php file to do this. Make sure it’s right before ‘That’s it, no more editing!’ wp-config.php wp-config.php wp-config.php wp-config.php wp

define('WP_ALLOW_REPAIR', true);

Once you’ve done that, go to this page: http://www.yoursite.com/wp-admin/maint/repair.php to see the settings.

WordPress database repair

To access the database repair page, the user does not need to be logged in. Remove this code from your wp-config.php once you’ve finished restoring and optimizing your database.

4. Determine if your database server is unavailable.

If everything appears to be in order but WordPress still can’t connect to the database, it’s possible that your database server (MySQL server) is down.

This could occur as a result of high traffic on a server. Basically, the demand on your host server is too much for it to handle (especially when you are on shared hosting).

Your site will become extremely slow, and some users may experience an error. As a result, the best thing you can do is call or chat with your hosting provider and ask if your MySQL server is responsive.

If you have other websites on the same host as your SQL server, you can check those sites to see if your SQL server is down.

If you don’t have any other sites on the same hosting account, go to your hosting dashboard and try to join the database using phpMyAdmin.

If you’re able to connect, we’ll need to see if your database user has enough permissions. Make a new file called testconnection.php and paste the code below into it:

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

Replace the username and password if necessary. This file can now be uploaded to your website and accessed with a web browser.

If the script connected successfully, that suggests your user has enough permissions and something else is wrong.

Return to your wp-config file and double-check that everything is in order (re-scan for typos).

Other Options That Have Proven Beneficial to Users

If the prior troubleshooting techniques failed to resolve the database connection error on your website, you can attempt the following actions.

These techniques, according to our customers, have helped some people fix the database connection error on their websites.

1. Change the URL of your WordPress site.

Using phpMyAdmin, try changing the URL of your WordPress site. Simply pick your WordPress database in phpMyAdmin from your hosting account dashboard.

In phpMyAdmin, you may run SQL queries.

Then, at the top of the page, select SQL and type the MySQL query below.

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Remember to supply your own site URL and update wp options to your own table name if the WordPress table prefix has changed.

2. Restarting the Web Server

Rebooting dedicated servers, local servers, and virtual private servers (VPS) is an option for users.

This will restart your web and database servers, which may resolve some of the error’s temporary issues.

3. Seek assistance

If all else fails, you might need to contact your web hosting provider. All reputable WordPress hosting companies will assist you in troubleshooting the issue, directing you in the proper direction, or even resolving it for you.

You can also engage WordPress developers from Codeable to assist you in resolving this problem at a fair cost. The best thing is that these developers have been thoroughly vetted by the Codeable team, so you can trust them.

We hope this post was helpful in resolving the WordPress database connection problem.

Comments are closed.