Connect WordPress to a MySQL Database: Setting Up wp-config.php Correctly
WordPress requires a MySQL or MariaDB database to store content, users, settings, comments, menus, and plenty of plugin data. The connection between WordPress and your database is managed via the wp-config.php file.
If these credentials are incorrect, you will frequently encounter the error message "Error establishing a database connection".
wp-config.php file with great care. A single incorrect character, a missing quotation mark, or a wrong password can completely prevent WordPress from connecting to your database.What Data Does WordPress Need?
To establish a database connection, WordPress requires four central pieces of information: database name, database user, password, and database host.
| WordPress Entry | Meaning | Example |
|---|---|---|
DB_NAME |
Name of the database | user_wordpress |
DB_USER |
Database user | user_wpuser |
DB_PASSWORD |
Password of the database user | Your configured database password |
DB_HOST |
Database server | localhost |
localhost. Only use a different value if it is explicitly specified by your hosting environment.Do Not Forget the cPanel Prefix
In cPanel, database names and database users are often automatically assigned a prefix. This prefix is usually based on your cPanel username, followed by an underscore.
For example, if you enter wordpress as the database name in the database wizard, the full name in cPanel might look like this:
user_wordpress
This exact full name must be entered into your wp-config.php file later on. The same rule applies to the database user.
| Wizard Input | Full cPanel Name | Use in WordPress? |
|---|---|---|
wordpress |
user_wordpress |
Yes, use the full name |
wpuser |
user_wpuser |
Yes, use the full name |
Editing wp-config.php
The wp-config.php file is typically located in the root directory of your WordPress installation. You can edit it via the cPanel File Manager or using FTP.
- Log in to cPanel.
- Open the File Manager.
- Navigate to the directory of your WordPress website, often
public_htmlor a specific subdirectory. - Locate the
wp-config.phpfile. - Create a backup copy of the file before making any edits.
- Open the file for editing.
- Check or modify the database entries.
- Save the file and test your website.
wp-config-backup.php. However, ensure that such backup copies are not left publicly accessible on a permanent basis.Example of Database Entries in wp-config.php
The relevant lines in your wp-config.php typically look like this:
define( 'DB_NAME', 'user_wordpress' );
define( 'DB_USER', 'user_wpuser' );
define( 'DB_PASSWORD', 'YourDatabasePassword' );
define( 'DB_HOST', 'localhost' );
Replace these example values with the actual data of your database. Make sure you match the exact spelling and casing shown in cPanel.
Assigning the Database User to the Database
A frequent mistake is that although the database and database user have been created, the user has not yet been linked to the database. In this case, WordPress will fail to connect despite having the correct name and password.
How to check the assignment:
- In cPanel, open the MySQL Databases tool.
- Look for the section titled Add User To Database.
- Select the appropriate database user.
- Select the correct database.
- Click Add.
- For WordPress, generally select ALL PRIVILEGES.
- Save your selection.
Without the correct privileges, WordPress cannot read, write, or update tables. This can lead to installation failures, update issues, or database connection errors.
Common Errors with the WordPress Database Connection
| Error | Possible Cause | Solution |
|---|---|---|
| Error establishing a database connection | Wrong database name, username, or password | Verify entries in wp-config.php |
| Access denied | User is not assigned or the password is incorrect | Check user privileges in MySQL Databases |
| Unknown database | Database name is wrong or entered without prefix | Use the full database name from cPanel |
| Website remains blank (White Screen of Death) | PHP error, plugin conflict, or configuration mistake | Check error logs and revert recent changes |
Checks to Perform After a WordPress Migration
After a migration or a WordPress restore, your database and files must match up perfectly. It is highly critical that the imported database is also the one configured in your wp-config.php.
- Database imported? Check phpMyAdmin to verify that the tables are present.
- Correct database configured? Double-check
DB_NAME. - Correct user configured? Double-check
DB_USER. - Password up to date? If you changed the password in cPanel, it must also be updated in WordPress.
- User privileges correct? The user must be mapped to the database.
- Domain changed? If you changed your domain name, additional adjustments to the WordPress URLs in the database may be required.
Table Prefix in WordPress
In addition to the database access credentials, wp-config.php also contains the table prefix. This determines the prefix with which WordPress tables are created in your database.
$table_prefix = 'wp_';
The table prefix must match the tables inside your database. For instance, if your tables are named wp_posts, wp_options, and wp_users, then wp_ is correct. If the tables use a different prefix, this value must be adjusted accordingly.
Security Around wp-config.php
The wp-config.php file contains sensitive database credentials. Treat this file with extreme caution.
- Never share the contents of this file publicly.
- Do not save database passwords in unprotected notes or public files.
- Use strong, unique database passwords.
- Delete any unneeded copies or backups of
wp-config.phponce you are done. - Only change your database password if you are ready to update it in WordPress immediately after.
Summary
The connection between WordPress and your MySQL or MariaDB database is entirely managed through the wp-config.php file. The full database name, the full database user, the correct password, and the right database host are critical to making it work.
When using cPanel hosting, it is particularly important not to forget the automatic prefix for database names and users. Additionally, the database user must be properly assigned to the correct database with sufficient privileges.
Is your WordPress website showing a database connection error or failing to work correctly after a migration?
The CURIAWEB Technical Team is happy to assist you with checking your wp-config.php, database access, and user privileges.