In the Digital and modern world, over 60 million users use WordPress, and most of them suffered from different errors in daily tasks. Research shows that Errors waste precious time and distract you from your purposeful path. In those errors, the most important Errors is the “error establishing a database connection WordPress”. Have you seen this error? No; it’s ok, but if you are a WordPress User you may face this error in the future.
Read this Article carefully; to learn how to fix error establishing a database connection.Before we dive into this error it is important to under how WordPress works?
How WordPress Functions?
WordPress uses wp-config.php and MySQL for functionality. PHP as the primary language used to get data from MySQL,
Ifyou get this error message establishing a database connection on the screen, it means that “ WordPress is unable to connect with database. In simple words, wp-config.phpis not connecting with your Site’s MySQL database to get the required information which PHP needs to put on a page”.
What are the causes of the error establishing a database connection?
- Due To wrong database Credentials: WordPressuses PHP to connect with the database serverto get the data, it requires some login credentials to connect with the database. That information is;
- Database Username
- Database Name
- Database Password
- Database Host Name
If any of the above mentioned information is wrong or if you change it and you are entering wrong or old credentials then you see the error message.
- Due To Corrupt database: When we work on WordPress, we perform different activities like deactivating or deleting the theme, plugins, or install plugins. Sometimes the database gets corrupted due to this then you get this error.
- Because of Hosting Server Failure: Server failure is one of the reason for the error establishing a database connection, also it happens due to the server maintenance. Before fixing this error make sure to download a backup of your database.
How To Fix Error Establishing A Database ConnectionWordPress?
Following are the methods which you can use to fix the error;
- Fixing Through Login Credentials:
First of all, you need to check that database login information matches with your stored information that is available in the wp-config.php file. To access this file follow the following steps.
- Go to the cPanel and navigate to the File Manager and double click on this.
- Then find the Public.htmldirectory and look for wp-config.php
- Now open this file by clicking on Edit option and check that the login credentials.
- If you want to confirm this information from the database, then you should navigate to MySQL and look for your current database name, users, and list of your current MySQL.
- If this informationisthe same so good and if different from each other, then, modify this information in the wp-config.php file.
- After that reload your website to check error has fixed or not. If you are still facing this error, it means something else is wrong.
Repair WordPress database:
Follow below steps;
- Go to the browser and enter the URL https://www.yourwebsite.com/wp-admin/maint/repair.php and then you will get this view
- Note and in this site, your website means your own website.
- Copy this define(‘WP_ALLOW_REPAIR’,true); and then openwp-config.php file and edit it.
- Navigate in the WP_config.Php file and paste this line (define(‘WP_ALLOW_REPAIR’, true);at the end of this file and click on save the change.
- After this Go to the browser and enter https://www.yourwebsite.com/wp-admin/maint/repair.php , this time you will get this below page.
There are two options Repair Database, Repair and optimize database. Its your choice on which you want to click.
- After clicking on anyone,it will repair it and put results in front of you.
NOTE: When you have a repaired database, go back to your File wp-config.php and Remove the inserted line.
Go back to your page and refresh it, the error must be gone. If still the error persists then do not worry and Read the next option.
Create ANew Database:
If you have completed all the above techniques, it means that your database is entirely corrupt. In this case, you just need to create a new MySQL database and user.
Once you have created a new database, drift your database backup to prevent everything from scratch.
Check Your Database Server:
If you have followed all of the above methods and still confronted the error. It is valid that your MySQL server is down. In this situation, you should contact the hosting provider.
If they permitted to establish a connection then;
· Make a new file of any name extension *.php which you can remember! Like testconnection.php file and add the following code in that file.
- <?PHP
- $link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
- if (!$link) {
- die(‘Could not connect: ‘ . mysqli_error());
- }
- echo ‘Connected successfully’;
- mysqli_close($link);
?>
- When you insert this code, you must change the name and password of your Database information.
- Upload this file to your website through a web browser.
- If it successfully works, it means the user has permission, and there is something more or else which is establishing error.
Conclusion
We have mentioned all the possible way to fix error establishing a database connation in WordPress
WordPress is one of the most used CMS platforms in the world. If you encounter any wordpress errors such as http error 500 WordPress, WordPress 403 Forbidden Erorr or HTTP error established when upload images. For more information, please contact us