Almost every dynamic website keeps its content in a database. WordPress stores posts, pages, users and settings there; a webshop adds products, orders and customers. Before you can install such an application by hand, or move one from another host, you need an empty database and a user that may work with it.
This guide shows you how to create both in cPanel, connect them to your site, work with the data in phpMyAdmin and fix the connection error that almost everyone meets once.
The three parts of a cPanel database
A working database in cPanel is always three things together:
- The database — an empty container for tables.
- A database user — a username and password the application logs in with. This is not your cPanel login.
- Privileges — the link that says which user may do what in which database.
If any of the three is missing, the application can't connect. cPanel also adds your account username and an underscore to every name. If your cPanel user is acme and you type shop, the database is called acme_shop, and a user shopuser becomes acme_shopuser.
| You type | cPanel creates |
|---|---|
shop (database) | acme_shop |
shopuser (user) | acme_shopuser |
Tip: Give each website its own database and its own user. If one site is compromised, the attacker's database login doesn't open the others.
Create a database with the MySQL Database Wizard
The wizard takes you through all three parts in one go.
- Open Databases › MySQL Database Wizard.
- Step 1: Create A Database. Enter a short name, such as
shop, and click Next Step. - Step 2: Create Database Users. Enter a username, then click Password Generator to create a long random password. Copy it somewhere safe before you continue, and click Create User.
- Step 3: Add User to the Database. Tick ALL PRIVILEGES. WordPress and most other CMSs need the full set to install and update themselves. Click Make Changes.
- Step 4: Complete the task. cPanel confirms the database and user. Write down the full names with prefix.
You now have the four details every application asks for:
| Setting | Example value |
|---|---|
| Database name | acme_shop |
| Username | acme_shopuser |
| Password | The one you generated |
| Host | localhost |
Manage databases afterwards
For later changes, use Databases › Manage My Databases. Older cPanel versions call this page MySQL Databases. It is split into sections:
- Create New Database and Current Databases — add, rename or delete databases. The Privileged Users column shows who has access.
- Add New User and Current Users — create users, Change Password or Delete them.
- Add User To Database — link an existing user to a database and pick privileges.
Renaming a database or changing a password breaks the site until you update its configuration file with the new value. Change one, update the other, and test right away.
Which privileges to grant
ALL PRIVILEGES is the practical choice for WordPress, Joomla, Drupal and most shop systems, because their updates create and alter tables. For a read-only reporting tool or an external dashboard, create a separate user and tick only SELECT. That way a leaked password can be used to read data, but never to change or delete it. You can adjust privileges at any time by clicking the user's name in the Privileged Users column.
Connect your website to the database
If you install WordPress through an installer in cPanel, it creates the database and fills in the settings for you; see our guide to installing WordPress with cPanel. For a manual installation or a move, you enter the details yourself.
In WordPress they live in wp-config.php in the site's folder:
define( 'DB_NAME', 'acme_shop' );
define( 'DB_USER', 'acme_shopuser' );
define( 'DB_PASSWORD', 'YOUR_GENERATED_PASSWORD' );
define( 'DB_HOST', 'localhost' );
Many modern PHP frameworks, such as Laravel, use a .env file instead:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=acme_shop
DB_USERNAME=acme_shopuser
DB_PASSWORD=YOUR_GENERATED_PASSWORD
Make sure a .env file is never inside a publicly reachable folder, or that the server blocks it. It contains your password in plain text.
Work with data in phpMyAdmin
Databases › phpMyAdmin opens a web interface where you can browse tables, edit rows, run SQL and move data in and out.
Import a database, for example when moving a site:
- Create an empty database and user with the wizard first.
- In phpMyAdmin, select the new database in the left column.
- Open Import, choose your
.sqlor.sql.gzfile and click Import (or Go).
Export a database, for example before a big update:
- Select the database and open Export.
- Choose Quick and the SQL format, then start the export.
Our backup guide covers larger databases and the command line, and the guide to moving WordPress puts import and export into a full migration.
A quick SQL query is useful for checking things, for example which address WordPress thinks it lives at:
SELECT option_name, option_value
FROM wp_options
WHERE option_name IN ('siteurl', 'home');
Warning: The Drop button and the Operations tab in phpMyAdmin delete tables or whole databases immediately, with no trash bin. Export first if you are unsure.
Remote access to the database
By default only the server itself may connect to your databases. That is deliberate: a database open to the whole internet is a target for password guessing.
If you really need to connect from a desktop tool or another server, open Databases › Remote Database Access, add the exact IP address that will connect and click Add Host. Avoid wildcards like %, and remove the entry when you are done. For most tasks, phpMyAdmin is simpler and safer.
Fix "Error establishing a database connection"
This WordPress message, and similar ones in other apps, nearly always comes down to one of four things:
| Check | What goes wrong |
|---|---|
| Database name | The cPanel prefix is missing: shop instead of acme_shop |
| Username and password | Typo, or the password was changed in cPanel but not in the config file |
| Privileges | The user exists but was never added to the database — look at Privileged Users in Manage My Databases |
| Host | Something other than localhost, often copied from the old host |
Fix the value, save the file and reload the page. If all four are correct and the error stays, the database server itself may be struggling; our guide to fixing a website that isn't working walks through the next steps.
In short
- A database needs three things: the database, a user and privileges linking them.
- Use MySQL Database Wizard for new databases and Manage My Databases for changes.
- cPanel prefixes names with your username — always use the full name.
- The host is
localhost, and each site gets its own database and user. - Use phpMyAdmin for import, export and quick queries, and export before deleting anything.
Databases on Hostigan hosting
Hostigan web hosting comes with cPanel, so you can create databases, users and privileges yourself as described here. If your site runs on WordPress, our WordPress hosting sets up the database for you with the one-click install.
Frequently asked questions
On almost all cPanel hosting it is localhost, because the website and the database run on the same server. Only use something else if your host tells you to.