Error Establishing a Database Connection
You will receive an error message stating “Error Establishing a Database Connection” if your WordPress website cannot connect to its database. Although only a few problems could be the cause, you can quickly remedy the issue with some inquiry and debugging.
This tutorial will define “Error Establishing a Database Connection,” list the most frequent causes, and show you how to fix each one.
Website Database
A website database refers to a structured collection of data that is stored and organized to support the functioning and content management of a website. It acts as a central location for managing, accessing, and storing numerous kinds of website-related data.
A website database typically stores information such as user accounts, content (articles, blog posts, images, etc.), comments, settings, and other relevant data. It allows for efficient storage, retrieval, and manipulation of data, enabling dynamic and interactive features on the website.
Database management systems like MySQL, PostgreSQL or SQLite which offer tools and functionality to construct, manage and interact with the database, are frequently used to build websites’ databases. The DBMS ensures data integrity, security, and efficient data handling.
By utilizing a website database, website owners and administrators can easily update and manage content, retrieve user information, track user activities, implement search functionality, and perform various other operations that require data storage and retrieval. It plays a crucial role in the functioning and interactivity of modern websites.
“Error Establishing a Database Connection” What does it mean?
We first need to comprehend how WordPress functions before attempting to fix the issue. It has two essential components, files and a database, like most CMS (Content Management System) programs.
- The files serve as the framework for the website and house the media, themes, and plugins, as well as the essential WordPress functionality.
- On the other hand, the database houses the particular settings and materials that make up the website. The tables contain each page, post, text, and design entries.
Through numerous PHP and MySQL routines, the files interact with the database and extract data that is then shown on the web pages. As a result, if your website’s connection to the database is unsuccessful, it cannot retrieve the information needed to render the pages. The screenshot below shows that WordPress alerts you to the issue by displaying the message “Error Establishing a Database Connection.”
Causes of “Error Establishing a Database Connection”?
WordPress users may get “Error Establishing a Database Connection” for a number of reasons, including:
Incorrect login credentials
WordPress won’t be able to connect to the database if the database login credentials [username and password] for your WordPress site are wrong.
Corrupted database
A corrupted or damaged database can prevent WordPress from establishing a connection to the database.
Web server issues
If there are issues with your web server or database server, such as high traffic, server overload, or connectivity issues, it can lead to database connection errors.
Incorrect database settings
Incorrect database settings such as database host, database name, or database table prefix can cause connection errors.
Plugin or theme conflicts
Some plugins or themes can conflict with your WordPress database connection, causing errors.
Hacked site
A hacked WordPress site can also lead to date
abase connection errors, as the hacker may have made changes to the database or removed essential database files.
Insufficient database user privileges
Even if the credentials are right, the error can still happen if the database user doesn’t have enough access. All forms of MySQL actions on the database must be permitted for the user.
Incorrect website URLs
In rare circumstances, wrong website URLs may be to blame for the “Error establishing a database connection.”
Corrupted WordPress files
Another explanation for the “Error establishing a Database Connection” warning is that the core WordPress files for your website are damaged. It could result from malware infection, an unsuccessful upgrade, or an unsuccessful file transfer while manually relocating your site.
How to Fix “Error Establishing a Database Connection”
The mistake sounds frightening and challenging to fix at first glance. However, it typically comes down to a few crucial parameters, which are simple to verify and adjust.
Inspect the wp-config.php File of your WordPress website
Every WordPress installation’s configuration file contains the database settings. It may be found in the root directory of your website and is called wp-config.php.
- Log in to the website directories using FTP or the hosting control panel to inspect the file. Go to Site Tools > Site > File Manager if you are a SiteGround user.
- Every website has a root folder, which is located at yourdomain.com/public_html. In our example, the folder is located at sg-testing.com/public_html because the website’s name is sg-testing.com.
- Keep a backup of the file before making any modifications. To accomplish this, right-click the file, choose Copy, and give the copied file the new name wp-config-backup.php.
- Select the file wp-config.php and click Edit after making a backup copy of it.
- The database name, user, password, and host are defined in the following lines of code:
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** Database username */
define( 'DB_USER', 'username_here' );
/** Database password */
define( 'DB_PASSWORD', 'password_here' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );
- Each setting’s values must coincide with the website’s MySQL server’s current database, user, and password. Once the modifications have been made, click the Save (floppy disc) icon.
Set the correct database hostname
The next line of code designates the MySQL server:
define( 'DB_HOST', 'localhost' );
Usually, the website’s files and the database host are on the same server. Since “localhost” or “127.0.0.1” is the value, the complete line will read as follows:
define( 'DB_HOST', 'localhost' );
or
define( 'DB_HOST', ‘127.0.0.1’ );
Replace these numbers with the right hostname of the remote server if you’ve specified a remote database host.
Set the correct database name
Check the database information in the MySQL server after the host has been established. Users of SiteGround can quickly access the MySQL settings by going to Site Tools > Site > MySQL.
Open the MySQL section in a new browser tab by right-clicking on it and choosing “Open in a New Tab” in order to compare the settings from the wp-config.php file with the specifics of the MySQL database more conveniently.
The Databases tab under Manage Databases contains a list of all the current databases on the MySQL server.
There, the database must be identical to the one specified in the wp-config.php file.
In the line of code in wp-config.php, copy the database name from MySQL > Databases, and then paste it:
define( 'DB_NAME', 'database_name_here' );
Set the correct database username
Click on the link in the Users column of the database to view the user for the database.
All users with the ability to change the website’s database will be listed in a pop-up window that will appear. Copy the username that is highlighted in the User column.
Return to the wp-config.php file editor and paste the name in the line as follows:
define( 'DB_USER', 'username_here' );
There won’t be a link under the Users column if there isn’t a user associated with the database. To add an existing user to the website’s database, click the Add New User icon in the Actions column.
Choose one of the user names from the Users drop-down menu in the popup window, check the All Privileges radio button, and click Confirm.
You can copy the user’s name and paste it in the wp-config.php file once they have been connected to the database.
Open the section Site Tools > Site > MySQL > Users if there are no users there. By clicking the Create User button, a new user can be created.
Note:
Remember that you cannot select the username for the database. It will be produced by the system automatically.
The new username will be displayed in the Users tab’s Manage Users section. To add the user to the website’s database, click the icon for Add new database.
Select the website’s database in the pop-up window, check the box next to All privileges, and click Confirm.
The new username can then be copied and pasted in the appropriate line in wp-config.php.
Set the correct database user’s password
You must confirm that the user’s password in wp-config.php and MySQL match after adding the user there. Take note of the password string in the wp-config.php file and copy it:
define( 'DB_PASSWORD', 'password_here' );
Return to Site > MySQL > Users, pick Change Password from the kebab menu after copying the password.
In the New password window, paste the password, then click Confirm.
If all credentials are set correctly, the problem should go away when you reload the webpage.
Check if the Database User has the Correct Privileges
Go to Site Tools > Site > MySQL > Users and locate the user in the Manage Users section to view the user’s permissions. The link in the Database Access column should be clicked to launch a new pop-up window.
To manage the access options, click the Manage Access icon to launch a new window.
Select the radio button for All Privileges and then click the Confirm button to set all necessary permissions.
Repair the Database
Fortunately, WordPress has an internal repair option if your database has to be fixed.
You must use FTP or your hosting control panel to edit the wp-config.php file on your website in order to activate it.
By going to Site Tools > Site > File Manager, SiteGround users can change the file. The file is situated in the root directory of the website, which is yourdomain.com/public_html (you must substitute your own domain name here).
Press Edit after deciding on wp-config.php.
Immediately after the phrase “/* That’s all, quit editing! */” and then enter the following code:
define('WP_ALLOW_REPAIR', true);
By clicking the Save icon, you can confirm the changes.
Open the page https://yourdomain.com/wp-admin/maint/repair.php in your browser to launch repair mode, replacing yourdomain.com with the name of your website. For instance, the address would be https://sg-testing.com/wp-admin/maint/repair.php if our domain name is sg-testing.com.
Select either Repair Database or Repair and Optimise Database, and then refresh your website’s home page after the maintenance is complete. Once the issue has been resolved, delete the code you inserted to wp-config.php to prevent future users from accessing repair mode.
Inspect the WordPress Site URL in the Database
You won’t be able to log in to the dashboard where the website’s URL is specified because of the problem. However, you can edit the tables where these parameters are specified using phpMyAdmin.
Users of SiteGround can use the tool by choosing Site Tools > Site > MySQL > PHPMYADMIN > Access PHPMYADMIN from the menu.
Open the wp_options table by clicking the website’s database in the left-hand column.
Note:
Although wp_ is the standard WordPress table prefix, it’s likely that your site’s prefix will be unique.
The variables siteurl and home specify the website URLs. Select Edit for each, enter the appropriate website address in the option_value column, and then select Go to save your changes.
Restore a Backup of the Site
Make sure to export a fresh full website backup from the old hosting and import it into the new hosting once more if you came into the error following a manual transfer.
The best option is to restore the website from a recent backup if it hasn’t been transferred and the problem just appeared. The backup tool from SiteGround is described in this tutorial.
Contact the Web Hosting Provider
If the problem is due to the setting of the website, the aforementioned fixes are effective. However, if the MySQL server is overcrowded or undergoing maintenance, WordPress may be unable to connect to the database.
Consider restarting the MySQL service if you are in charge of the server to get rid of any stalled database queries that are overtaxing it.
Users who have managed hosting are not permitted to access this way. Make contact with your provider’s support staff so they can assess the server’s condition and deal with the issues on their end.
FAQ’s
What is the URL for DB connection?
The URL for a database connection typically includes the following components:
1- Protocol: The URL starts with a protocol, such as “jdbc:” for Java Database Connectivity.
2- Database Type: The URL specifies the type of database being used, such as “mysql:”, “postgresql:”, or “oracle:”.
3- Host, Port, and Database Name: The URL includes the hostname or IP address of the server, the port number (if non-standard), and the name of the specific database.
For example, a MySQL database connection URL might look like: “jdbc:mysql://localhost:3306/mydatabase”, where “jdbc:mysql://” specifies the protocol and database type, “localhost” is the server, “3306” is the port number, and “mydatabase” is the database name.
How do I reset my WordPress database?
To reset your WordPress database, you can follow these steps:
1- Back up your current WordPress database to ensure you don’t lose any important data.
2- Use a WordPress plugin like “WP Reset” or “Advanced Database Cleaner” to reset the database, which will delete all existing content, settings, and customizations.
3- After resetting, you can reinstall WordPress or restore your backup to start with a fresh database.
How do I connect WordPress to my domain?
To connect WordPress to your domain, you can follow these steps:
1- Purchase a domain name from a domain registrar.
2- Sign up for web hosting and configure the domain settings to point to your hosting server.
3- Install WordPress on your hosting server and update the WordPress settings to reflect your domain name.
What if I accidentally deleted my WordPress database?
If you accidentally deleted your WordPress database, follow these steps:
1- Immediately stop using your website and make a backup of all remaining files and folders.
2- Contact your web hosting provider and explain the situation. They may have backups or solutions to help you recover the deleted database.
3- If backups are not available, you may need to recreate your WordPress website from scratch or consult with a professional data recovery service to attempt to recover the deleted database.
How to connect database to WordPress in Cpanel?
To connect a database to WordPress in cPanel:
1- Log in to your cPanel account and navigate to the “Databases” section.
2- Create a new MySQL database and user, and assign the user to the database with full privileges.
3- Update the WordPress configuration file (wp-config.php) with the database name, username, password, and host information provided by cPanel.
More Articles