Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Wordpress site use on GCP--Offline or shows error establishing database connection

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.

0 1 1,935
1 REPLY 1

Hello James_Njenga

 

There are a few different scenarios in which the error appears. The most common ones include:

  • Wrong database settings in the configuration file – Incorrect database credentials are arguably the most common reason for a disrupted database connection. If the database name, username, password or hostname are wrong, the website won’t connect to the database and will return “Error establishing database connection”.
  • Insufficient database user privileges – Another reason why the error occurs is if the database user doesn’t have sufficient privileges, even if the credentials are correct. The user must have permission to execute all types of MySQL operations onto the database.
  • Corrupted database tables – WordPress is a highly-dynamic system, and each time you make changes to your website, the database’s tables are modified. Adding or deleting plugins and themes creates new tables or deletes existing ones, new pages add new entries, etc.

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”.

  • Incorrect website URLs – On rare occasions, the “Error establishing a database connection” could be caused by incorrect website URLs.
  • Corrupted WordPress files – Another reason why you see the “Error establishing a Database Connection” message is that your website’s WordPress core files are corrupted. This could stem from a malware infection, failed update, or incomplete file transfer while moving your site manually.
  • The database server is down or overloaded.

How to Fix “Error Establishing a Database Connection”

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.

Inspect the wp-config.php File of your WordPress website

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.

dionv_0-1675185394876.png

 

After creating the backup copy, select the file wp-config.php, and choose Edit.

dionv_1-1675185396406.png

 

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' );
dionv_2-1675185396070.png

 

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.

Set the correct database hostname

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’ );
dionv_3-1675185396142.png

 

If you’ve set a remote database host, you must replace these values with the correct hostname of the remote server.

Set the correct database name

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.

dionv_4-1675185396358.png

 

The existing databases on the MySQL server are listed in the Databases tab under Manage Databases.

dionv_5-1675185395344.png

 

The database listed there must match the one defined in the wp-config.php file.

dionv_6-1675185396083.png

 

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' );

 

dionv_7-1675185394924.png

 

 

Set the correct database username

To see the user for the database, click on the link under the Users column of the database.

dionv_8-1675185396368.png

 

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.

dionv_9-1675185396352.png

 

Go back to editing the wp-config.php file and paste the name in the line:

define( 'DB_USER', 'username_here' );
dionv_10-1675185395453.png

 

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.

dionv_11-1675185396075.png

 

In the popup window, select one of the names from the Users drop-down menu, check the radio button All Privileges, and press Confirm.

dionv_12-1675185395898.png

 

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.

dionv_13-1675185396044.png

 

 

 

IMPORTANT! Keep in mind that you can’t choose the database’s username. It will be automatically generated by the system.

 

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.

dionv_14-1675185396048.png

 

In the pop-up window, select the website’s database, check the radio button All privileges, and hit Confirm.

dionv_15-1675185395197.png

 

You can then copy the new username and paste it in the respective line in wp-config.php.

Set the correct database user’s password

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' );
dionv_16-1675185394813.png

 

After the password is copied, go back to Site > MySQL > Users, press the kebab menu, and select Change Password.

dionv_17-1675185396079.png

 

Paste the password in the New password field and hit Confirm.

dionv_18-1675185394930.png

 

The error should disappear when reloading the website if all credentials are set correctly.

Check if the Database User has the Correct Privileges

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.

dionv_19-1675185395681.png

 

Press the Manage Access icon to open a new window where the access options are managed.

dionv_20-1675185395193.png

 

To set all necessary permissions, select the radio button All Privileges and click on the button Confirm.

dionv_21-1675185394403.png

 

Repair the Database

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.

dionv_22-1675185395060.png

 

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.

dionv_23-1675185395679.png

 

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.

dionv_24-1675185395349.png

 

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.

Inspect the WordPress Site URL in the Database

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.

dionv_25-1675185395678.png

 

Click on the website’s database in the column on the left and open the wp_options table.

 

 

Note that wp_ is the default WordPress table prefix, but in most cases, the prefix for your site will be custom.

 

dionv_26-1675185396192.png

 

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.

dionv_27-1675185396148.png

You can check this documentation for reference.