Every dynamic website needs a database. Whether you are installing WordPress, building a custom PHP application, or setting up a staging site, the first step is creating a MySQL database and a user to access it. This tutorial walks you through the entire process using cPanel, which is the control panel included with most shared and cloud hosting plans.
Even if you have never touched a database before, you can follow these steps. By the end, you will have a working MySQL database, a user with the correct privileges, and you will know how to import data and connect from PHP.
What Is MySQL?
MySQL is a relational database management system (RDBMS) that stores data in structured tables. It is the most widely used database system on the web, powering WordPress, WooCommerce, Joomla, Drupal, and millions of custom applications. When your WordPress site loads a page, it sends queries to MySQL to retrieve posts, settings, user data, and menu structures. The database returns the requested data, and WordPress assembles it into the HTML page your visitors see.
MariaDB is a fork of MySQL that is functionally identical for most use cases. Many hosting providers (including Serverlys) use MariaDB as a drop-in MySQL replacement. The cPanel interface and all commands work the same way with either system.
Step 1: Create a New Database
- Log into cPanel. Your hosting provider gives you a cPanel URL (typically
yourdomain.com/cpaneloryourdomain.com:2083). Enter your username and password. - Navigate to the Databases section. In the cPanel dashboard, look for the "Databases" category. Click on "MySQL Databases" (not phpMyAdmin — we will use that later).
- Enter a database name. In the "Create New Database" section, type a name for your database. cPanel automatically prefixes it with your account username, so if your username is "example" and you type "mysite," the full database name will be
example_mysite. - Click "Create Database." You will see a success message confirming the database was created.
- Click "Go Back" to return to the MySQL Databases page.
Naming conventions: Use descriptive names that identify the project. For WordPress, names like wp_main or wp_blog are clear. Avoid generic names like db1 or test — when you have multiple databases, you want to know at a glance which one belongs to which project.
Step 2: Create a Database User
A database needs a user account to access it. For security, each application should have its own database user with access limited to only its database.
- Scroll down to "MySQL Users" on the same MySQL Databases page.
- Enter a username. Like database names, cPanel prefixes this with your account username. Choose something descriptive (e.g., "wpuser" for your WordPress database user).
- Generate a strong password. Click the "Password Generator" button to create a secure random password. Copy this password and save it — you will need it when configuring your application. Use at least 16 characters with a mix of uppercase, lowercase, numbers, and symbols.
- Click "Create User." You will see a confirmation message.
- Click "Go Back" to return to the main page.
Step 3: Assign the User to the Database
Creating a user and a database are separate actions. You must explicitly grant the user access to the database.
- Scroll down to "Add User to Database."
- Select the user you just created from the dropdown.
- Select the database you created from the dropdown.
- Click "Add."
- On the privileges page, select "ALL PRIVILEGES." This grants the user full control over the database. For WordPress and most CMS applications, you need all privileges. Click the "ALL PRIVILEGES" checkbox at the top, which automatically checks all individual privilege boxes.
- Click "Make Changes."
Understanding MySQL Privileges
| Privilege | What It Allows | Needed For WordPress? |
|---|---|---|
| SELECT | Read data from tables | Yes |
| INSERT | Add new rows to tables | Yes |
| UPDATE | Modify existing data | Yes |
| DELETE | Remove rows from tables | Yes |
| CREATE | Create new tables | Yes (for installation and updates) |
| DROP | Delete tables | Yes (for plugin/theme cleanup) |
| ALTER | Modify table structure | Yes (for updates) |
| INDEX | Create and remove indexes | Yes (for performance) |
| LOCK TABLES | Lock tables during operations | Yes (for backups and imports) |
| REFERENCES | Create foreign key constraints | Rarely |
For most applications, granting ALL PRIVILEGES is the simplest and most reliable option. If you are building a security-sensitive application and want to follow the principle of least privilege, you can grant only the specific permissions your application requires.
Step 4: Import an SQL File
If you are migrating a site, restoring a backup, or setting up a pre-built application, you will need to import an SQL file into your new database.
Using phpMyAdmin (Recommended for files under 50 MB)
- Open phpMyAdmin from cPanel (click the phpMyAdmin icon in the Databases section).
- Select your database from the left sidebar. Make sure you click on the correct database name — importing into the wrong database is a common and destructive mistake.
- Click the "Import" tab at the top.
- Click "Choose File" and select your .sql file.
- Leave the default settings (format: SQL, character set: utf-8). These defaults are correct for 99% of imports.
- Click "Import" at the bottom of the page.
- Wait for the import to complete. phpMyAdmin will display a success message with the number of queries executed.
Using SSH Command Line (For large files)
If your SQL file is larger than phpMyAdmin's upload limit (typically 50–512 MB depending on your host's configuration), use the command line:
- Upload the SQL file to your server via FTP or the File Manager.
- Connect via SSH (if your hosting plan includes SSH access).
- Run the import command:
mysql -u username_user -p username_database < /path/to/backup.sql - Enter the password when prompted.
Serverlys Tip: All Serverlys cloud hosting plans include SSH access and support for large database imports. If you are migrating from another host, our free migration service handles the database transfer for you.
Step 5: Connect to Your Database from PHP
Once your database is set up, your application needs to connect to it. Here is how to configure the connection for the two most common scenarios.
WordPress (wp-config.php)
When installing WordPress, the setup wizard asks for your database details. If you are configuring manually, edit the wp-config.php file in your WordPress root directory:
define( 'DB_NAME', 'username_mysite' ); — The full database name (including the cPanel prefix).
define( 'DB_USER', 'username_wpuser' ); — The full username (including the cPanel prefix).
define( 'DB_PASSWORD', 'your_strong_password_here' ); — The password you generated in Step 2.
define( 'DB_HOST', 'localhost' ); — On most cPanel hosts, the database server is on the same machine, so use "localhost."
Custom PHP Application (PDO)
For custom PHP applications, use PDO (PHP Data Objects) for secure database connections:
$dsn = 'mysql:host=localhost;dbname=username_mysite;charset=utf8mb4';
$username = 'username_wpuser';
$password = 'your_strong_password_here';
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC];
$pdo = new PDO($dsn, $username, $password, $options);
Always use prepared statements with PDO to prevent SQL injection attacks. Never concatenate user input directly into SQL queries.
Managing Your Database with phpMyAdmin
phpMyAdmin is a web-based MySQL administration tool included with cPanel. It provides a graphical interface for common database tasks:
Common Operations
- Browse data: Click a table name to view its contents. You can sort, search, and filter rows.
- Run SQL queries: Click the "SQL" tab to execute raw SQL queries against your database.
- Export (backup): Click "Export" to download a copy of your database as an SQL file. Use "Quick" export for small databases or "Custom" for more control over what is included.
- Optimize tables: Select tables, choose "Optimize table" from the dropdown. This reclaims unused space and defragments data, which can improve query performance.
- Search across tables: Use the "Search" tab to find specific data across all tables in a database. Useful for finding hardcoded URLs when migrating or for troubleshooting.
Database Security Best Practices
- Use unique, strong passwords. Database passwords should be at least 16 characters with mixed case, numbers, and symbols. Never use the same password for your database user and your cPanel login.
- One user per application. Do not share a single database user across multiple websites. If one site is compromised, the attacker gains access only to that site's database, not all of them.
- Regular backups. Export your database regularly (daily for active sites) and store backups off-server. Serverlys includes automated daily backups on all plans.
- Limit remote access. By default, cPanel databases only accept connections from localhost (the same server). Do not enable remote access unless you specifically need it, and if you do, restrict it to specific IP addresses.
- Change the default table prefix. WordPress uses
wp_as the default table prefix. Changing this to a custom prefix (e.g.,s7f_) during installation adds a small layer of security against automated SQL injection attacks.
Troubleshooting Common Errors
"Error establishing a database connection" (WordPress)
This is the most common database error. It means WordPress cannot connect to MySQL. Check these things in order:
- Verify credentials in wp-config.php. The database name, username, and password must exactly match what you created in cPanel (including the username prefix).
- Check that the user is assigned to the database. Creating the user and creating the database are not enough — you must also add the user to the database with privileges (Step 3).
- Check DB_HOST. On cPanel hosting, this should be "localhost." Some hosts require a different value (e.g., a specific IP or hostname). Check your host's documentation.
- Check if MySQL is running. Contact your hosting provider if the above checks pass — the MySQL service itself may be down.
"Access denied for user" Error
This means the username or password is wrong, or the user does not have permission to access the specified database. Double-check that the cPanel username prefix is included (e.g., example_wpuser, not just wpuser).
"Table already exists" During Import
You are trying to import data into a database that already has tables. Either drop the existing tables first (be sure you have a backup!) or check the import settings for a "DROP TABLE IF EXISTS" option.
Import File Too Large
phpMyAdmin has an upload limit. Solutions include compressing the SQL file (gzip), using the SSH command-line import method described above, or splitting the SQL file into smaller chunks using a tool like BigDump.
Frequently Asked Questions
How many databases can I create?
This depends on your hosting plan. Most Serverlys cloud hosting plans allow unlimited MySQL databases. Budget shared hosting plans may limit you to 1–10 databases. Check your plan's features page or ask your host.
What is the difference between MySQL and MariaDB?
MariaDB is a community-developed fork of MySQL. For web hosting purposes, they are functionally identical. The same SQL syntax, the same tools (phpMyAdmin, mysqldump), and the same WordPress configuration work with both. Serverlys uses MariaDB for its performance optimizations while maintaining full MySQL compatibility.
Do I need to create a database for WordPress?
Yes. WordPress requires a MySQL database to store all its content, settings, users, and configuration. If you use a one-click WordPress installer (like Softaculous in cPanel), it creates the database automatically. If you install WordPress manually, you need to create the database and user first using the steps in this guide.
How do I back up my database?
Three methods: (1) Use phpMyAdmin's Export feature for a manual backup. (2) Use cPanel's Backup Wizard for a full account backup including databases. (3) Use a WordPress plugin like UpdraftPlus for automated, scheduled backups. For the most reliable protection, use your host's automated backup system combined with an off-site backup plugin.
Can I access my database from outside cPanel?
Yes, but you need to enable remote MySQL access in cPanel first. Go to "Remote MySQL" in cPanel and add the IP address of the computer that needs access. For security, only add specific IP addresses — never use the wildcard (%) to allow access from any IP unless absolutely necessary.