DigitalBerg
  • HOME
  • WINDOWS VPS
  • VIRTUAL SERVER
    • Virtuozzo VPS
    • OpenStack VPS
    • Amazon Lightsail VPS
    • VMware VPS
  • DEDICATED SERVER
    • INSTAND DEDICATED SERVERS
    • HOSTING DEDICATED SERVERS
    • DEDICATED ENTERPRISE SERVERS
    • INFRA. DEDICATED SERVERS
    • DEDICATED GAME SERVERS
  • MORE
    • SSL SHOP
    • FREE SSD WEBHOSTING
  • LOGIN
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

We talking about Cloud

Web Hosting & Cloud Services
Celebrate the power and freedom of Open Source
with Open Source Cloud Servers starting at only £10/mo £5/mo.

How to Connect to a MySQL Database: 7 Essential Steps

To connect to a MySQL database, your application needs the correct database hostname, database name, username and password. The database user must also have permission to access that database.

Applications such as WordPress, Joomla and WHMCS store these details in a configuration file. If a connection fails after a migration or password change, those settings are a good place to start.

This guide explains how to connect to a MySQL database through your application, check permissions in cPanel and troubleshoot common problems.

1. Find Your Application’s Configuration File

Before editing anything, download a backup of the configuration file and store it securely outside your website’s public directory.

Common locations include:

Application Typical configuration file
WordPress wp-config.php in the WordPress installation directory
Joomla configuration.php in the installation directory
WHMCS configuration.php in the installation directory

Custom applications may use environment variables, a .env file or another configuration method. Follow the application’s documentation rather than creating a new file based only on its name.

For related guidance, see Location of Script Configuration Files.

Keep configuration files private. They can contain database credentials and other sensitive settings.

2. Check Your MySQL Connection Details

You usually need these four values:

Setting What to enter
Database hostname The hostname supplied by your hosting provider
Database name The full name of the database your application uses
Database username A database user assigned to that database
Database password The password belonging to that database user

On many cPanel accounts, database names and usernames include an account prefix:

Database name: account_website
Database user: account_siteuser

Copy the complete names shown in cPanel. Do not assume the prefix is enabled or that it matches an old hosting account.

Is the Database Host Always localhost?

No. Use localhost only when your hosting provider specifies it.

A local database service may use localhost, while a separate database server requires its own hostname. Your application may also need a port or socket setting.

Do not replace localhost with 127.0.0.1 simply because a connection fails. Depending on the client, those values can use different connection methods.

3. Create a Database User and Assign Access

In cPanel, open Databases → Manage My Databases. Older versions may label this interface MySQL Databases.

If you are setting up a new application, cPanel’s Database Wizard can guide you through creating the database, creating a user and assigning access.

For an existing setup:

  1. Confirm that the database exists.
  2. Confirm that the intended database user exists.
  3. Find Add User to Database.
  4. Select the correct user and database.
  5. Click Add.
  6. Select the privileges required by your application and save the changes.

Use the permissions specified by the application’s installation documentation. An installer or updater may need permissions that a read-only reporting tool does not.

Creating a user does not automatically give it access to every database.

For details, see cPanel’s Manage My Databases documentation.

4. Enter the Settings in Your Application

To connect to a MySQL database, update the application’s existing settings with the values you have confirmed. Preserve the file’s syntax and avoid adding duplicate settings.

WordPress Database Connection Example

In wp-config.php, find the existing database constants:

define( 'DB_NAME', 'account_website' );
define( 'DB_USER', 'account_siteuser' );
define( 'DB_PASSWORD', 'REPLACE_WITH_YOUR_DATABASE_USER_PASSWORD' );
define( 'DB_HOST', 'localhost' );

These are placeholders. Replace them with your actual details and use the hostname supplied by your provider.

A few checks help prevent simple mistakes:

  • Use the complete database name and username.
  • Enter the database user’s password, which may differ from your WordPress administrator password.
  • Keep straight quotation marks and the semicolon at the end of each line.
  • If a value contains a quotation mark or backslash, ensure it is correctly represented in PHP.
  • Edit the existing constants instead of adding a second copy.

See the official WordPress configuration guide for application-specific details.

Use a Dedicated Database User

Use a database user created for the application rather than relying on your main hosting-account credentials.

A dedicated user makes access easier to manage and limits the databases that those credentials can reach.

If you change a database user’s password, update every application that uses that user. Changing the password in cPanel does not rewrite your application configuration files.

5. Test the MySQL Database Connection

After saving the configuration, test the application itself.

For a website, check:

  • The homepage.
  • A page that reads information from the database.
  • The administrator login.
  • An appropriate application function that saves data.

Use a test or staging environment for changes that could affect orders, payments or customer records.

Optional Command-Line Test

If your account provides a MySQL client and terminal access, you can test the credentials with:

mysql -h DATABASE_HOST -u DATABASE_USER -p DATABASE_NAME

Replace the placeholders with your settings. Enter the password when prompted; do not place it directly in the command.

Once connected, a simple read-only check is:

SELECT DATABASE(), CURRENT_USER();

Exit with:

EXIT;

A successful terminal connection is useful evidence, but the application can still fail if it uses different credentials, runs from another server or lacks the required database driver.

6. Configure Remote Access When Necessary

If the application and database run on different servers, you may need remote access.

In cPanel, the relevant interface is Databases → Remote Database Access. Older versions call it Remote MySQL.

Where your hosting plan supports it:

  1. Identify the application server’s outgoing public IP address.
  2. Add that specific host to the permitted access list.
  3. Confirm the database hostname and connection details with your provider.
  4. Check any required firewall rules and database-user permissions.
  5. Use the provider’s supported encrypted connection method.

Adding an access host does not replace the need for valid credentials and database privileges. It also does not automatically configure every firewall or network setting.

Avoid granting access from all addresses when a specific host will do. If direct remote access is unavailable, ask your provider about supported alternatives.

See cPanel’s Remote Database Access documentation.

7. Troubleshoot Common MySQL Connection Errors

The wording of the error usually helps you narrow down the problem.

Error or symptom What to check first
Access denied for user Username, password, permitted connection host and authentication requirements
Unknown database Database name, account prefix and whether the database exists
Connection refused Hostname, port, service availability and network restrictions
Connection timed out Routing, firewall rules, remote-access permissions and destination availability
Permission denied for a query Privileges assigned to the database user
WordPress reports a database connection error wp-config.php, database availability and the underlying error logs

The Database User Has No Privileges

Check whether the user is assigned to the intended database.

Return to Manage My Databases, review the user-to-database association and apply the permissions required by the application. Keep a record of the original grants before changing them.

Do not assume this is caused by an old cPanel bug. Incorrect assignments, migrations and permission changes are more useful starting points for investigation.

The Connection Works, but the Website Is Empty

A successful connection does not prove that the application is using the right database.

Check:

  • Whether the data import completed.
  • Whether the expected tables exist.
  • Whether the application points to the correct database.
  • Whether its table prefix matches the imported tables.

Do not reinstall the application over existing data simply to test the connection.

Connecting to MySQL After a Hosting Migration

Database credentials often differ between hosting accounts. After moving a website:

  1. Create the destination database and user using the hosting control panel.
  2. Import the database backup.
  3. Assign the user to the destination database.
  4. Update the application with the destination hostname and complete names.
  5. Enter the destination database user’s password.
  6. Test the application before completing the migration.
  7. Keep the previous hosting and backups available until the new setup is verified.

Do not rely on the old claim that a database username can only change when the cPanel username changes. Available naming and renaming options depend on the server configuration and permissions.

Frequently Asked Questions

Is My Database Password the Same as My cPanel Password?

Not necessarily. A dedicated database user has its own credentials. Use the password belonging to the user named in your application’s configuration.

Can I Connect to a MySQL Database Using phpMyAdmin?

phpMyAdmin lets you inspect and manage databases through cPanel. However, opening a database there does not prove that your application’s separate credentials are correct.

Do I Need to Create a New Database to Fix a Connection Error?

Usually, no. First check the existing hostname, database name, credentials and permissions. Creating another database will not repair an incorrect setting and may leave the application connected to an empty database.

Should I Share My Configuration File with Support?

Share the relevant error message and configuration details with passwords and other secrets removed. If support needs additional access, use the provider’s approved secure process.

Need Help Connecting Your Database?

Contact DigitalBerg support with the affected domain, application name, exact error and whether the connection is local or remote.

Include when the problem began and any recent migration, password or configuration changes. Do not include your database password in the message.

Get in Touch

Let me know if you got an interesting project and want to work together!

digitalberg.com

DigitalBerg fuels apps, websites, emails, and much more for numerous clients globally.

Virtual servers to dedicated servers at affordable prices that any hostinger student can afford.

Services

  • Web Hosting
  • Virtual Server
  • Instant Servers
  • Dedicated Servers
  • Enterprise Servers
  • Infrastructure Servers
  • Gaming Servers
  • SSL Shop

SUPPORT

  • Contact Us
  • Wiki
  • FAQ
  • Blogs

Company

  • About Us
  • Partners
  • Privacy Policy
  • Term of Service
  • Acceptable Use Policy
  • Green Hosting
  • Data center

CLIENT AREA

  • Registration
  • Client Area
  • Server Status
  • Support

OTHER SERVICES

  • Domain Registration
  • Transfer Domain
  • Add ons / Management
  • SSL Certificates
  • Server Monitoring
  • Server hardware
DigitalBerg review
DigitalBerg review
© Copyright - DigitalBerg | From January 1st 2015, in accordance with the amended Directive 2006/112/EC, prices inclusive of VAT may vary depending on the country of residence of the customer.
  • Link to Facebook
  • Link to X
  • Link to LinkedIn
Link to: Custom Error Pages in cPanel: 7 Essential Setup Steps Link to: Custom Error Pages in cPanel: 7 Essential Setup Steps Custom Error Pages in cPanel: 7 Essential Setup StepsCustom error pages workflow: select the domain, write a helpful response, save and test the HTTP status. Link to: Script Configuration: 7 Essential Tips for Safe Edits Link to: Script Configuration: 7 Essential Tips for Safe Edits script configuration guide: plan the change, use the right tools and verify the resultScript Configuration: 7 Essential Tips for Safe Edits
Scroll to top Scroll to top Scroll to top