Fix Error Establishing a Database Connection on Hostinger

by Fahim Mahmud Chisti

Error establishing a database connection on Hostinger means WordPress cannot log in to its MySQL database. After a migration, the cause is almost always one wrong value in wp-config.php: a database name or user without Hostinger’s u123456789_ prefix, an old password, or the wrong database host. This guide shows you how to find the wrong value, test the connection, and get the site back.

Quick answer: Make the database values in wp-config.php match exactly what hPanel shows under Databases, including the u123456789_ prefix, and keep DB_HOST set to localhost.

  1. In hPanel, open Databases and note the full database name and user.
  2. Compare them with DB_NAME, DB_USER and DB_HOST in wp-config.php.
  3. If you are not sure of the password, change it in hPanel and paste the new one into DB_PASSWORD.
  4. Test the login with a small PHP script or wp db check over SSH.
  5. If it connects but WordPress offers a fresh install, fix $table_prefix.
In this guide
  1. Before You Start
  2. Why This Error Appears After Moving to Hostinger
  3. Step 1: Check the Database Values in wp-config.php
  4. Step 2: Compare Them With hPanel (Watch the Prefix)
  5. Step 3: Try 127.0.0.1 If localhost Fails
  6. Step 4: Test the Login With a Standalone PHP Script
  7. Step 5: Test From SSH With mysql and WP-CLI
  8. Step 6: Fix a Table Prefix Mismatch
  9. Step 7: Re-Import or Repair Damaged Tables
  10. Step 8: Disable a Leftover Object Cache Drop-In
  11. If It Still Doesn’t Work
  12. Frequently Asked Questions
  13. Next Steps
Terminal screen showing MySQL database connection verification and wp-config setup for WordPress on Hostinger

Before You Start

  • Access: your Hostinger hPanel login. SSH access is optional, but it makes the tests in Step 5 much faster.
  • Back up wp-config.php: download a copy, or duplicate it as wp-config.php.bak, before you edit it. One missing quote in this file takes the whole site down with a different error.
  • Time: fixing credentials usually takes 10 to 15 minutes. Re-importing a database takes longer.
  • Make sure you are on the new server: if DNS has not fully switched, your browser may still reach the old host. Compare the output of dig +short A yourdomain.com with the IP address hPanel lists for your site.

Why This Error Appears After Moving to Hostinger

WordPress shows this error whenever PHP fails to log in to MySQL or MariaDB. When you migrate across hosts, database naming conventions change. Your old server might have let you name your database wp_site with a user like root or admin. Hostinger’s hPanel assigns an account-specific prefix to every database and user on shared and cloud hosting plans, usually formatted like u123456789_dbname.

Whether you used a migration plugin or followed our guide to migrate a large WordPress site to Hostinger with SSH and WP-CLI, the SQL import might have succeeded while your config file was left pointing to old or malformed credentials.

First, check where the error happens. If /wp-admin/ loads but says “One or more database tables are unavailable”, MySQL is alive, but your tables are corrupted or missing (go to Step 7). If you see “Error establishing a database connection” everywhere, PHP can’t connect to MySQL at all; start with Step 1.

Step 1: Check the Database Values in wp-config.php

Open wp-config.php in your site’s root folder (public_html), either with File Manager in hPanel or over SSH. Look for these four lines:

/** The name of the database for WordPress */
define( 'DB_NAME', 'u123456789_production' );

/** MySQL database username */
define( 'DB_USER', 'u123456789_dbuser' );

/** MySQL database password */
define( 'DB_PASSWORD', 'YourStrongPassword123!' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

As noted in the official WordPress wp-config.php documentation, a single stray space, an unclosed quote, or a typo in these definitions will break the connection. Make sure the password string is wrapped in single quotes and isn’t truncated.

Step 2: Compare Them With hPanel (Watch the Prefix)

Don’t rely on memory for the database name or password. Check them in hPanel:

  1. Log into Hostinger hPanel and open your website dashboard.
  2. Go to Databases and open the MySQL databases page (labelled Management in some hPanel versions).
  3. Scroll down to List of Current MySQL Databases And Users.
  4. Check that the full MySQL Database name matches DB_NAME in your config.
  5. Check that the full MySQL User name matches DB_USER.

The most common mistake here is dropping the account prefix. If hPanel lists your database as u492019482_wp814, putting define('DB_NAME', 'wp814'); will fail every time. You need the full string with the prefix.

If you aren’t sure about the password, click the three dots next to the database user in hPanel, choose Change Password, set a new one, and paste that exact string into DB_PASSWORD inside wp-config.php. Save the file and reload the site.

Step 3: Try 127.0.0.1 If localhost Fails

On Hostinger shared and cloud plans, MySQL runs on the same server as PHP, so define('DB_HOST', 'localhost'); is the standard value. With localhost, PHP connects through MySQL’s local socket file. With 127.0.0.1, it connects over TCP on port 3306 instead. If the socket setting is wrong, localhost can fail while 127.0.0.1 works.

If localhost isn’t connecting, try one of these values in wp-config.php:

/** Alternative DB_HOST configuration */
define( 'DB_HOST', '127.0.0.1' );

/** Or with an explicit MySQL port (keep only one DB_HOST line) */
define( 'DB_HOST', '127.0.0.1:3306' );

If you’re running WordPress on a Hostinger VPS (using Docker or separate database containers), localhost resolves inside the web container. In that case, you’ll need to supply the container service name or internal bridge IP instead.

If neither value connects, the host is probably not the problem. Recheck the name, user and password from Step 2, or test them directly with Step 4.

Step 4: Test the Login With a Standalone PHP Script

Refreshing WordPress doesn’t tell you much, because caching and plugins can hide the real error. A tiny standalone script tests the database login on its own.

Create a temporary file named db-test.php in your public_html/ folder (File Manager or SSH) with this content, and fill in your four values:

<?php
// Temporary test file. Delete it as soon as you are done.
$host = 'localhost';
$user = 'u123456789_dbuser';
$pass = 'your-database-password';
$name = 'u123456789_production';

// Report failures through connect_error instead of throwing exceptions
mysqli_report(MYSQLI_REPORT_OFF);
$conn = new mysqli($host, $user, $pass, $name);

if ($conn->connect_error) {
    echo 'Connection failed (error ' . $conn->connect_errno . '): ' . htmlspecialchars($conn->connect_error);
} else {
    echo 'Database connection established. MySQL server version: ' . htmlspecialchars($conn->server_info);
    $conn->close();
}

Load https://yourdomain.com/db-test.php in your browser. A working login prints Database connection established. MySQL server version: followed by a version number. If it fails, the page shows an error number; match it below or in the PHP mysqli connect error documentation to decode the error code:

  • Error 1045 (Access denied): Bad username or password.
  • Error 1049 (Unknown database): Database name doesn’t exist or wasn’t created in hPanel.
  • Error 2002 (Connection refused / socket error): MySQL isn’t running or DB_HOST isn’t resolving properly.

Important: Delete db-test.php immediately after testing so you don’t expose your database credentials to the web.

Step 5: Test From SSH With mysql and WP-CLI

If you have SSH enabled on your Hostinger plan (found under Advanced → SSH Access), you can test the database login faster from the terminal, without a browser.

Connect over SSH and run the commands below on the server. On Hostinger, each site’s document root lives under ~/domains/yourdomain.com/public_html:

# Test direct MySQL login
mysql -u u123456789_dbuser -p -h localhost u123456789_production

# If using WP-CLI, run it from the site's document root
cd ~/domains/yourdomain.com/public_html
wp db check

The mysql command asks for the password. If the login works, you get a mysql> prompt; type exit to leave. An ERROR 1045 here means the user name or password is wrong, independent of WordPress.

If wp db check lists your tables with an OK status and ends with Success: Database checked., the database connection is working. If it throws an error, check which values WP-CLI is actually reading from your configuration:

wp config get DB_NAME
wp config get DB_USER
wp config get DB_HOST

Each command prints the value stored in wp-config.php. Compare them with hPanel once more.

Step 6: Fix a Table Prefix Mismatch

Sometimes the credentials match, the standalone script connects, and WordPress still shows the installation screen (or a “One or more database tables are unavailable” message) instead of your site. That usually means your $table_prefix doesn’t match the table names in your imported SQL dump.

Check the prefix line in wp-config.php:

/**
 * WordPress database table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

Now open phpMyAdmin from hPanel and look at your actual table names:

  • If your tables are named wp_posts, wp_options, etc., the prefix must be wp_.
  • If your old host or security plugin renamed them to something like wp_xyz123_posts, update your config to match:
$table_prefix = 'wp_xyz123_';

If the prefix doesn’t match character-for-character, WordPress connects to MySQL, looks for wp_options, fails to find it, and offers to run a fresh installation. Don’t run it; fix the prefix instead.

Step 7: Re-Import or Repair Damaged Tables

If an SQL import was interrupted, some tables will be missing or incomplete, and the fix is to re-import the dump; a repair cannot bring back data that never arrived. For dumps too big for phpMyAdmin, see how to import a large WordPress database via SSH. (An import that stops with an “Unknown collation” error such as utf8mb4_0900_ai_ci needs that collation replaced in the dump before you re-import.) If instead a table is marked as crashed, which mostly affects older MyISAM tables, WordPress has an emergency repair utility built right in.

Add this line to wp-config.php just above /* That's all, stop editing! Happy publishing. */:

define( 'WP_ALLOW_REPAIR', true );

Save it, then open https://yourdomain.com/wp-admin/maint/repair.php in your browser and click Repair Database.

WordPress will loop through your core tables and execute repair statements. As soon as it finishes and the site is accessible, remove that line from wp-config.php immediately. Leaving it active lets anyone trigger database repairs without logging in.

Step 8: Disable a Leftover Object Cache Drop-In

If the site used Redis or Memcached on the previous server, WordPress may still try to reach a cache service that no longer exists. The resulting timeouts can look like a database failure.

Look in your wp-content/ folder for a leftover object-cache.php file. Rename it over SSH (or in File Manager):

# Temporarily disable object cache drop-in
cd ~/domains/yourdomain.com/public_html/wp-content
mv object-cache.php object-cache.php.bak

If the site loads after the rename, the old cache drop-in was the problem. If you are on a Hostinger VPS and want Redis back, see how to configure Redis object cache on a Hostinger VPS.

If It Still Doesn’t Work

  • The error comes and goes under traffic. The database user may be hitting its connection limit (MySQL error 1203, max_user_connections). Check resource usage in hPanel, look for plugins that run heavy or frequent queries, and ask Hostinger support to confirm the limit for your plan.
  • You edited the wrong wp-config.php. If you have several sites or WordPress lives in a subfolder, open the file in that site’s own folder under ~/domains/yourdomain.com/public_html.
  • The site is still served by the old host. If dig +short A yourdomain.com returns your previous host’s IP, you are looking at the old server. Wait for DNS to update, or test with the new server’s IP in your hosts file.
  • Error 1045 even after changing the password. Make sure you changed the password of the user that belongs to this database, and that no space was copied before or after it.

Frequently Asked Questions

Why does Hostinger show “Error establishing a database connection” only on wp-admin?

When the frontend works but /wp-admin/ shows the error, the frontend is almost certainly being served from a page cache (LiteSpeed Cache, a caching plugin, or Cloudflare) while the database connection is down for every uncached request. Work through the same checks above. If wp-admin instead reports that database tables are unavailable, go to the repair step.

Can SSL or mixed content cause a database connection error?

No. SSL handshake issues trigger browser security warnings or 502/521 errors, but they don’t break the local PHP-to-MySQL connection. Mixed-content warnings after the database is back are a separate HTTPS problem with old http:// URLs, not a database fault.

What MySQL privileges does the Hostinger database user need?

Your DB_USER needs full privileges: SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, and LOCK TABLES. In hPanel, a user created together with its database gets full permissions on that database automatically.

Is the database host ever a remote IP on Hostinger shared plans?

No. On shared and cloud plans, the database host is always localhost or 127.0.0.1. The Remote MySQL section in hPanel is only for letting external machines connect to Hostinger—your hosted WordPress site connects locally.

Next Steps

Once your site is back up, clean up: delete db-test.php, remove WP_ALLOW_REPAIR from wp-config.php if you added it, and delete the wp-config.php.bak copy from the server. Then log in to wp-admin and open a few posts and pages to confirm the content came across. Finally, automate WordPress backups with WP-CLI and cron on Hostinger so the next migration has a quick rollback.

all_in_one_marketing_tool