WordPress beginner might be a little overwhelmed by WordPress database link errors. Here many reasons for database link errors in WordPress are listed and how to fix them.
Note: Before making any database changes, make sure to take a backup.
Why does this database link error occur?
In short, you receive this error because WordPress cannot establish a database connection. Now, the reasons why WordPress cannot establish a database connection may be different. It may be that your database login credentials are wrong or have been changed. It may be that your database server is not responding. It may be that your database is damaged. In our experience, this error occurs most of the time due to some kind of server error, but there may be other factors as well. Let’s see how to solve this problem.
Does the problem also appear in / wp-admin /?
The first thing is to ensure that the error is seen on the front end of the site and the back end of the site (wp-admin). If the error message on both pages is the same, “An error occurred while establishing a database connection,” continue to the next step. If another error is encountered on wp-admin, such as “One or more database tables are not available”. The database may need to be repaired “, and then go ahead and repair the database.
Use edit plus software to open the downloaded wp-config.php file, and add the following code before “OK, don’t edit anymore”
define (‘WP_ALLOW_REPAIR’, true);
Then save it, go back to the root directory of the website, and overwrite the original file.
After finishing, you can visit this page to check the settings:
http://www.yoursite.com/wp-admin/maint/repair.php
Repair database in WordPress
Remember, after setting this definition, users can access this feature without logging in. This is because its main purpose is to repair the damaged database, and users usually cannot log in when the database is damaged. Therefore, once you have completed repairing and optimizing the database, make sure to delete it from wp-config.php.
If this fix does not solve the problem, or if another problem is encounter while running the fix, please continue reading this article because there are other solutions.
Check the WP-Config file
WP-Config.php is probably the most important single file in the entire WordPress installation. Specifying the details of WordPress connection to the database can be done here. If the root password is changed or the database user password, change this file. The first thing to always check is whether everything in the wp-config.php file is the same.
define (‘DB_NAME’, ‘database-name’);
define (‘DB_USER’, ‘database-username’);
define (‘DB_PASSWORD’, ‘database-password’);
define (‘DB_HOST’, ‘localhost’);
Remember, DB Host value may not always be localhost. Depending on the host, it will be different.
For some hosts, it is localhost. Some people suggested that they solve their problems by replacing localhost with IP. This problem is common when running WordPress in a local server environment. For example, on MAMP, the DB Host value when changing to IP seems to work properly.
define (‘DB_HOST’, ‘127.0.0.1:8889’);
IP will vary depending on the online web hosting service. If everything in this file is correct (make sure to check for spelling errors)
Check your web host (MySQL server)
When a website is full of traffic, there is usually this error to establish a database connection. Basically, the hosting server cannot handle the load (especially when on shared hosting).
website will become very slow. Therefore, the best thing to do is to communicate with the hosting provider and ask them if MySQL server is responding. For users who want to test whether the MySQL server is running on their own.
Test other sites on the same server to see if they encounter problems. If they get the same error, then there must be a problem with MySQL server. Or just go to cPanel and try to access phpMyAdmin and connect to the database. If it connects, then verify that the database user has sufficient permissions.
Create a new file named “testconnection.php” and paste the following code in it:
<? php
$ link = mysql_connect (‘localhost’, ‘root’, ‘password’);
if (! $ link) {
die (‘Could not connect:‘. mysql_error ());
}
echo ‘Connected successfully’;
mysql_close ($ link);
?>
Make sure to replace the username and password. If the connection is successful, it means that the user has sufficient permissions and there are other errors. Return to the wp-config file to make sure everything is correct (rescanning for spelling errors).
If the database cannot be connected to by accessing phpMyAdmin, then it is related to the server. It does not necessarily mean that MySQL server is down. This may mean that the user does not have sufficient permissions.
“In one example, MySQL server is running. Except for the website in question, all other sites on the server is working fine. When trying to use phpMyAdmin, it ends up with the error:
# 1045-Access denied for user ‘foo’ @ ’%’ (use password: YES)”
After communicating with the hosting company, they quickly discovered the problem. Somehow, user permissions were reset. Not sure how it happened, but obviously this is the reason. They re-entered and restored permissions, and the website returned to normal operation.
Therefore, if an access denied error is encounter when connecting to phpMyAdmin or passing testconnection.php results, then immediately contact the host provider to have them fix it.
Solutions for others
It is important to note that these may not work for you. Use this risk at your own risk. If there are any problems, please make sure you have enough backups. In another example, a client is receiving an error that the database needs to repair. Even after repairing the database, the error did not disappear. After trying various things, finally, the problem was the website URL. Obviously, it has changed, causing the error to persist. And had to run SQL queries by accessing phpMyAdmin:
UPDATE wp_options SET option_value = ’YOUR_SITE_URL’ WHERE option_name = ’siteurl’
Please be sure to replace “YOUR_SITE_URL” with the actual URL such as: https://spankradio.uk. If the default WordPress database prefix is set, wp_options will be different.
Hope this help fix the issue of database link errors