PHP/MariaDB/MySQL

Test MySQL Connection

Updated November 2026 10,467 views 3 min read
PHP/MySQL | Updated November 2026

If your website shows database connection errors, you need to verify that your database credentials are correct and the server is reachable. Here is how to test your MySQL/MariaDB connection.

01. PHP Connection Test Script

Create dbtest.php in public_html:

<?php
$host = 'localhost';
$user = 'your_db_username';
$pass = 'your_db_password';
$db   = 'your_db_name';

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
    echo 'Connection FAILED: ' . $conn->connect_error;
} else {
    echo 'Connection SUCCESSFUL to database: ' . $db;
    $conn->close();
}
?>

Visit yourdomain.com/dbtest.php in your browser. You will see either "Connection SUCCESSFUL" or an error message explaining what failed.

Important

Delete dbtest.php immediately after testing. It contains your database credentials in plain text. Never leave database test files on a live server.

02. Finding Your Database Credentials

  • WordPress: Open wp-config.php and look for DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST
  • Other apps: Check the application's configuration file (usually config.php, settings.php, or configuration.php)
  • cPanel: Go to Databases > MySQL Databases to see your database names and users

03. Common Connection Errors

  • "Access denied for user" - Wrong username or password, or the user is not assigned to the database. In cPanel > MySQL Databases, check that the user is added to the database with "All Privileges." See How to Add a User to a Database.
  • "Unknown database" - The database name is wrong. Database names on cPanel are prefixed with your username (e.g., cpuser_dbname).
  • "Can't connect to MySQL server" - The host should be localhost on shared hosting, not an IP address or remote hostname. See Cannot Connect to MySQL.
  • "Connection refused on port 3306" - MySQL is running but only accepts local connections. Use localhost as the host. See What Port is MySQL On?

Database Connection Issues?

If you cannot get your database connection working, open a ticket with the error message (but not your password).

Open a Support Ticket

Frequently Asked Questions

How do I test my MySQL database connection?

Create a small PHP script that connects with your database name, user, password, and host (localhost), and reports success or the error. If it connects, your credentials are right; if not, the error message tells you what is wrong.

What host should I use to connect to my database?

Use localhost for applications running on the same hosting account, which is the normal case. Only use a remote host with the Remote MySQL setting when connecting from an external server, and add that server's IP to the allowed hosts.

Why does my database connection fail?

Almost always wrong credentials in the config: a mismatched database name, user, or password, or missing the cPanel account prefix. Confirm them against cPanel MySQL Databases, make sure the user is assigned to the database, and use localhost as the host.

Where do I find my database credentials?

In cPanel under MySQL Databases you can see the database and user names (with your account prefix) and reset the user's password if needed. Put those exact values, plus localhost as the host, into your application's config file.

Quick Recap

  1. Create a test PHP script with your credentials
  2. Host should be localhost on shared hosting
  3. Check cPanel > MySQL Databases for correct names and user assignments
  4. Database names are prefixed with your cPanel username
  5. Delete the test file immediately after testing

Database troubleshooting · Last updated November 2026 · Browse all PHP/MySQL articles

Share: X in Reddit f Email
  • 121 Users Found This Useful

Was this answer helpful?

Related Articles

Register Globals On Off

PHP/MySQL | Updated November 2026 register_globals was a PHP setting that automatically...

Increase PHP Memory

PHP/MySQL | Updated January 2026 If your website shows a "Fatal error: Allowed memory size...

Clean - Shrink ibdata1

PHP/MySQL | Updated November 2026 The ibdata1 file is the InnoDB system tablespace used by...

Cannot Connect to MySQL on Port 3306

PHP/MariaDB/MySQL | Updated November 2026 If you're getting connection errors when trying to...

What is the proper MariaDB - MySQL import and export format?

PHP/MySQL | Updated November 2026 When importing or exporting MariaDB/MySQL databases, the...



Save 30% on web hosting - Use coupon code Hosting30