Tutorials:How to Overcome Error Establishing A Database Connection (WordPress)

in utopian-io •  7 years ago 

Error establishing a database connection.

The above sentence is probably a nightmare for WordPress users and is one of the most common WordPress errors.

If you get this message, your website is no longer communicating with or having access to your database, which certainly causes your website down.

Most ordinary people panic when they get this message when they access their website. In fact, if you happen to get this message, you must move quickly in the process of handling because this issue can affect your traffic, sales, and analysis.

Maybe you are confused how to fix it? Maybe you do not understand why you got such a message? Do not worry! In this article, we will discuss some scenarios that usually cause this to happen and how to fix this issue.

But before we discuss how to solve it, we need to understand first what is the establishment of a database connection and why do we sometimes get that message when it comes to accessing a website, from both front-end and back-end?

What is an error establishing a database connection?

All information available on your WordPress site, such as post data, page data, meta information, plugin settings, login information, etc. stored in your MySQL Database. Some data that is not stored in the database is just media content such as pictures and themes or your plugins, as well as core files like index.php, wp-login.php, and so on.

When someone visits your website, PHP will execute code on the page and request data from the database, which will then be presented to the visitor's browser.

If PHP can not request data from the database, you will receive an "Error establishing a database connection" message. The whole page will be empty because there is no data obtained to show on the browser screen. Not only disrupt the front end of your website, it can also make you unable to access your WordPress dashboard.

When someone visits your website and gets the message, it will issue a 500 HTTP status code in your logs. This status code will appear when your website is experiencing "internal server error". This means there is something wrong on your server and the requested resource is not sent by the server. If all works well, your website will issue 200 HTTP status codes, which means everything is fine.

The cause of an error establishing a database connection

Before we discuss how to solve it, surely we need to first identify what causes the emergence of an error message establishing a database connection. Here are some of the most common causes found when someone receives this message:

  • Invalid login database credentials.Your WordPress website uses a separate login information to connect with MySQL Database.

  • The database is corrupt.With the ever-changing number of sections with themes, plugins, and users constantly removing and installing it, sometimes the database becomes corrupted. This can be caused by tables that are missing or corrupted individually, or maybe some information was accidentally deleted.

  • There is a corrupted file with WordPress installation.This problem can also occur due to hackers.

  • There is a problem with your database server.There are some things that are actually the fault of hosting, such as database overload from traffic being interrupted or the server becomes unresponsive because it gets too many requests. Such problems are common with websites that use shared hosts because they use multiple resources along with other users on the same server.

How to Overcome Error Establishing a Database Connection

Before you start the troubleshooting process, we recommend that you backup. The ways that will be discussed below will manipulate the information in your database. Therefore, you should backup your site data before you fix your WordPress website. WordPress also offers backup plugins like UpdraftPlus or Duplicator to backup your files and databases.

If you want to backup files manually, you can backup files using phpMyAdmin in your cPanel. After you finish backing up your website, you just start the troubleshooting process of your database connection problem.

1. Check Database Login Credentials

The first thing you should check is that your database login credentials are correct. This is one of the most frequent reasons why an error message establishing a database connection appears on a site. Especially after someone migrates their website to a new hosting.

Your WordPress site connection details are stored in a file called wp-config.php, which is usually in the root of your WordPress site. This file contains four important information that all must be confirmed is correct for the connection to work properly.

  • Database Name

// ** MySQL settings ** //

/ ** The name of the database for
WordPress * / define ('DB_NAME',
'xxxxxx');

  • MySQL Database Username

    / ** MySQL database username * /

    define ('DB_USER', 'xxxxxx');

  • MySQL Database Password

    / ** MySQL database password * /

    define ('DB_PASSWORD',
    'xxxxxxxxx');

  • MySQL HostName (Server)

    / ** MySQL hostname * /

    define ('DB_HOST', 'localhost')

To access the wp-config.php file, you can connect to your site via SFTP and browse the root of your site. Or if you use cPanel, you can click "File Manager", browse the root of your site, and right click to edit the file.

Select public_html on the left side of your screen then right click on wp-config.php file.

The wp-config.php file will look like below

You now need to check the current values ​​against those on your server to make sure they are correct. You need to check your credentials database in cPanel. All you need to check first is the database name. To do this you need to login to phpMyAdmin in cPanel under the Databases section.

On the left side, you will see your database name at the very bottom. You do not need to ignore the database "information_schema" because this is something used by the host. You then want to see your server name with the name that is in DB_NAME in the wp-config.php file. If they are the same, then your website is fine. If different, you need to update your wp-config.php file.

You can also verify that this is the correct database by ensuring that the site contains the URL of your WordPress site. To do this, simply click on the database, then click on the wp_options table (this may be named something slightly different for security purposes, such as wpxx_options). At the top of the table, you'll see a value for the URL and name of your site. If this matches your current site, you can be sure that you are in the right place.

If your database name is correct and you still get an error to create a database connection message then you also want to check your username and password. To do this, you need to create a new PHP file in the root directory of your WordPress site, and input the following code. You can name it as you wish, such as checkdb.php. Just change the db_user and db_password values ​​to those in your wp-config.php file.

  • <? php

    $ test = mysql_connect ('localhost',
    'db_user', 'db_password');

    if (! $ test) {

    die ('MySQL Error:'. mysql_error ());

    }

    echo 'Database connection is
    working properly!'; mysql_close ($
    testConnection);

Then browse to the file on your WordPress site: https://yourdomain.com/checkdb.php. If you get "MySQL Error: Access denied", you know your username or password is wrong and you need to proceed to the next step to reset your credentials.

The message you want to see is the following message - "Database connection is working properly." But of course, if you were not here. Be sure to delete this file after completion of the test.

So next you need to reset your username and password. In cPanel, click MySQL Database under the Database section.

Scroll down and create a new MySQL user. Try and choose a unique username and password so it is not easy to guess. The keyword generator tool they provide is really good. Then click "Create User." You can change the password on this screen to the current existing database user.

Then scroll down and add your new user to the database. The next screen will ask you what privileges you want to add. Select "All Privileges".

Then grab new credentials and update your wp-config.php file. You want to update DB_USER and DB_PASSWORD values. You can also run the test file again from earlier. This should then resolve your credentials issue. If not, you may still have the wrong hostname (DB_HOST). Some hosts use different values, see a list of some common DB host values. Usually this will only be localhost. But you can always contact your hosting provider or check their documentation if you are not sure. Some may also use 127.0.0.1 instead of localhost.

2. Fixing the Damaged Database

In some cases, this message may appear because your database is corrupted. This can happen, though not often, because there are many tables in the database that are added and deleted every time a website owner activates new plugins and themes.

If you try to login to your WordPress dashboard and get an error message establishing a database connection, this means there is damage to your database. In addition, you will also get the message "One or more database tables are unavailable. The database may need to be repaired ". Keep in mind that you will only get this message in the back-end and you will get an error message establishing a database connection on the front-end.

WordPress actually has a database repair mode that you can use. You just need to add the code below at the bottom of your wp-config.php file.

  • define ('WP_ALLOW_REPAIR', true);

Then browse the following location on the WordPress site: https://yourdomain.com/wp-admin/maint/repair.php. There you will have the option to repair the database or repair and optimize the database. The repair the database option works faster because they do not do the optimization process. If you want to complete this troubleshooting process quickly, you can choose the first option.

After you have repaired your database, make sure that you delete the code that you previously entered into your wp-config.php file, otherwise anyone can fix it. If you use cPanel, you can also fix it via MySQL Database.

Or you can run the fix through phpMyAdmin. You just need to login to phpMyAdmin, click the database, and select all tables. Then from the dropdown option, click "Repair table". This is actually just running the Repair Table command.

3. Fixing Corrupted Files

The third reason that might cause the emergence of an error message establishing a database connection is the corruption of the file. This can happen because the file transfer process via FTP is less than perfect, a hacker, or a problem with your website host. Do not forget to backup your website before you fix this problem.

What you will do to fix this problem is to replace the core version of WordPress on your website. You will not make any changes to your plugin, theme or media, just WordPress installation. To do this, you need to download WordPress from WordPress.org.

Unzip the file from your computer. In it, you delete the wp-content folder, including the wp-config-sample.php file.

Then upload the remaining files via SFTP to your site, overwrite your existing files on the website. This will replace all the problem files and make sure that you have a clean and undamaged new file. You should also clear the browser cache after doing this. Then check whether the WordPress site to see if you still have this error problem.

4. Problem with database server

Jika tidak ada satupun dari cara-cara diatas yang membantu menyelesaikan masalah Anda, maka Anda mungkin harus memeriksa dengan penyedia hosting Anda karena hal itu bisa menjadi masalah dengan server database Anda.

Misalnya, jika ada banyak koneksi bersamaan ke database Anda sekaligus, itu bisa menghasilkan error ini. Hal ini karena banyak host memiliki batasan pada server mereka tentang berapa banyak koneksi yang diperbolehkan sekaligus.

Menggunakan plugin caching dapat membantu meminimalkan interaksi database di situs Anda.

Masalah ini bisa banyak terjadi pada shared hosting karena orang lain secara teori dapat memengaruhi situs Anda. Hal ini karena shared hosting menggunakan semua resource yang sama di server.

5. Restore Backup Anda

If you want the fastest way, you can always use backups if needed. In some cases, this may be a faster way to solve the problem if you are not worried about losing data. Many hosts have their own backup restore process. Remember that you may need to recover your database and files.

so much from me, thank you very much,..



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.

source

You can contact us on Discord.
[utopian-moderator]

Thank's @damla