I have a site hosiped.com hosted on GCP Compute Engine.
I migrated the site using All in one WordPress migration plugin.
It has been running alright but now will show either error establishing database connection or is offline.
How can I troubleshoot this.
I have removed the MX, TXT records that I had updated for creating email accounts.
Hello James_Njenga
There are a few different scenarios in which the error appears. The most common ones include:
With so many MySQL operations being conducted, occasionally, the tables may become corrupted, which could lead to another variation of the “Error establishing database connection” – “One or more database tables are unavailable”.
At first glance, the error sounds quite intimidating and hard to resolve. However, it usually boils down to a few key settings, which can be easily checked and fixed.
The settings for the database are stored in the configuration file of every WordPress installation. It is called wp-config.php and is located in the root directory of your website.
To check the file, log into the website folders via FTP or your hosting’s panel. If you are a SiteGround user, navigate to Site Tools > Site > File Manager.
The root folder of every website is the directory yourdomain.com/public_html. In our example, the website name is sg-testing.com, so the folder is sg-testing.com/public_html.
Before making any changes, it’s best to keep a backup of the file just in case. To do that, right-click on the file, select Copy, and rename the copied version to wp-config-backup.php.
After creating the backup copy, select the file wp-config.php, and choose Edit.
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' );
The values of each setting must match the existing database, user and password in the MySQL server of the website. After applying the changes, press the Save (floppy disk) icon.
The MySQL server is defined by the line of code:
define( 'DB_HOST', 'localhost' );
Typically, the database host is on the same server as the website’s files. Therefore, the value is “localhost” or “127.0.0.1” and the entire line will read:
define( 'DB_HOST', 'localhost' );
or
define( 'DB_HOST', ‘127.0.0.1’ );
If you’ve set a remote database host, you must replace these values with the correct hostname of the remote server.
After the host has been established, check the database details in the MySQL server. SiteGround users can easily access the MySQL database settings from Site Tools > Site > MySQL.
To compare the settings from the wp-config.php file with the MySQL database details more easily, open the MySQL section in a new browser tab by right-clicking on it and selecting Open in a New Tab.
The existing databases on the MySQL server are listed in the Databases tab under Manage Databases.
The database listed there must match the one defined in the wp-config.php file.
Copy the database’s name from MySQL > Databases and paste it in the line of code in wp-config.php:
define( 'DB_NAME', 'database_name_here' );
To see the user for the database, click on the link under the Users column of the database.
A popup window will appear listing all users with privileges to modify the website’s database. Highlight the username under the User column and copy it.
Go back to editing the wp-config.php file and paste the name in the line:
define( 'DB_USER', 'username_here' );
If there is no user attached to the database, there will be no link under the Users column. Click on the Add New User icon under the Actions column to add an existing user to the website’s database.
In the popup window, select one of the names from the Users drop-down menu, check the radio button All Privileges, and press Confirm.
After the user is attached to the database, you can copy their name and paste it in the wp-config.php file.
If there are no existing users, open the section Site Tools > Site > MySQL > Users. You can create a new user by pressing the button Create User.
The new username will appear under the Manage Users section in the Users tab. Click on the icon Add new database to attach the user to the website’s database.
In the pop-up window, select the website’s database, check the radio button All privileges, and hit Confirm.
You can then copy the new username and paste it in the respective line in wp-config.php.
After adding the user in wp-config.php, you must ensure that the user’s password in the file matches its counterpart in MySQL. Copy the password string from the line in wp-config.php:
define( 'DB_PASSWORD', 'password_here' );
After the password is copied, go back to Site > MySQL > Users, press the kebab menu, and select Change Password.
Paste the password in the New password field and hit Confirm.
The error should disappear when reloading the website if all credentials are set correctly.
To check the user’s privileges, go to Site Tools > Site > MySQL > Users, and find the user in the section Manage Users. Click on the link under the Database Access column, which opens a new pop-up window.
Press the Manage Access icon to open a new window where the access options are managed.
To set all necessary permissions, select the radio button All Privileges and click on the button Confirm.
If your database is damaged, you need to repair it, and thankfully, WordPress has a built-in repair mode.
To activate it, you need to edit your website’s wp-config.php file via FTP or your hosting control panel.
SiteGround users can edit the file by navigating to Site Tools > Site > File Manager. The file is located in the website’s root directory, which is yourdomain.com/public_html (where you need to replace yourdomain.com with your own domain).
Select wp-config.php and press Edit.
Just above the line “/* That’s all, stop editing! Happy publishing. */”, add the following code:
define('WP_ALLOW_REPAIR', true);
Confirm the changes by pressing the Save icon.
To load repair mode, open the page https://yourdomain.com/wp-admin/maint/repair.php in your browser, where yourdomain.com is replaced with your website’s name. For example, our name is sg-testing.com, so the address would be https://sg-testing.com/wp-admin/maint/repair.php.
Choose between the two options Repair Database and Repair and Optimize Database, and once the maintenance is over, reload your website’s homepage. Once you confirm that the error is fixed, remove the code you added in wp-config.php so no one else can run repair mode.
The error will prevent you from logging into the dashboard where the website’s URL is set. However, you can use phpMyAdmin to edit the tables where these settings are defined.
SiteGround users can reach the tool by navigating to Site Tools > Site > MySQL > PHPMYADMIN > ACCESS PHPMYADMIN.
Click on the website’s database in the column on the left and open the wp_options table.
The website URLs are defined in the options siteurl and home. Press Edit on each, set the correct website URL in the option_value field, and confirm the change with the button Go.
You can check this documentation for reference.