Overview
Every web application that stores data needs a MySQL database — WordPress, Joomla, Magento, Laravel apps, custom PHP projects, all of them. Before any installation wizard can run, you need to create the MySQL database itself, create a user, and grant that user access to the database. If you skip any of those three steps, your app will throw a connection error before it even loads.
On shared hosting and managed WordPress plans, this is typically done through a control panel like cPanel or Plesk. On a VPS or dedicated server where you have root access, you’ll do it directly from the command line. This guide covers all three paths. The SQL commands are the same regardless of which MySQL or MariaDB version you’re running (5.7, 8.0, 10.x MariaDB), but I’ll call out version-specific differences where they matter.
If you’re on one of our VPS SSD Hosting plans and have root SSH access, skip straight to the CLI section — that’s the fastest route and gives you the most control.
Prerequisites
- Active hosting account with cPanel, Plesk, or SSH root/sudo access to a VPS or dedicated server
- MySQL or MariaDB installed and running (any version from 5.7 onward)
- For cPanel: your cPanel login credentials
- For Plesk: your Plesk admin or domain owner credentials
- For CLI: SSH access and either root or a sudo-enabled user
- A name in mind for your database and user — most control panels enforce a prefix (e.g.
cpanelusername_dbname), so plan accordingly
Method 1: Create a MySQL Database and User in cPanel
cPanel’s MySQL Database Wizard is the simplest option for non-technical users. It walks you through database creation, user creation, and permissions in a single flow.
- Log in to cPanel and navigate to the Databases section. Click MySQL Database Wizard.
-
Create the database. Enter a name in the New Database field. cPanel automatically prepends your cPanel username — so if your account is
mysiteand you typewordpress, the database will be namedmysite_wordpress. That full name is what you’ll use in your app’s config. Click Next Step. -
Create the database user. Enter a username and a strong password. Same prefix rule applies — the user will be
mysite_wpuseror similar. Use the password generator if you’re not sure what qualifies as strong enough; cPanel will tell you if it’s too weak.📝 Note: Write down the exact database name, username, and password before you proceed. You’ll need all three when configuring your app.
- Assign privileges. On the next screen, check ALL PRIVILEGES unless you have a specific reason to restrict access. For most web apps, all privileges on a single named database is the correct and expected setup. Click Next Step.
- Done. cPanel confirms the user has been added to the database. You can verify under Databases > MySQL Databases — your new database should appear in the list with the user shown underneath it.
⚠ Warning: cPanel’s MySQL Databases page (not the Wizard) lets you create databases and users separately. If you use that route, it’s easy to forget the final step — actually assigning the user to the database. A user that exists but isn’t linked to the database will get Access denied every time. Always scroll down to the Add User To Database section and complete that step.
Method 2: Create a MySQL Database and User in Plesk
- Log in to Plesk and go to Websites & Domains. Select the domain you’re working with.
- Click Databases, then click Add Database.
- Enter a database name. Under Related site, confirm the correct domain is selected.
- Under Database user name, enter a username and password. Plesk creates the user and links it to the database in the same step — one less thing to forget.
- Click OK. The database will appear in the list immediately.
📝 Note: Plesk Obsidian (18.x) and later show a phpMyAdmin shortcut next to each database. You can use that to verify the database was created and import an SQL dump if you’re migrating from another host.
Method 3: Create a MySQL Database and User via CLI
This is the method you’ll use on a VPS or dedicated server with direct shell access. It’s faster than any control panel once you’re comfortable with it, and it’s the only option when there’s no panel installed.
Step 1: Log in to MySQL
On most systems you can log in as root like this:
sudo mysql -u root -p
On Ubuntu 20.04+ and some other distros, MySQL/MariaDB installs with socket-based authentication for the root user by default. This means the command above may drop you straight in without asking for a password — or it may fail if you try to log in with a password that was never set. If you hit Access denied running mysql_secure_installation or any other tool, this is usually why. Fix it like this:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourNewRootPassword';
FLUSH PRIVILEGES;
EXIT;
Then rerun mysql_secure_installation or log in normally with mysql -u root -p. Replace YourNewRootPassword with an actual strong password — never leave example passwords in a production system.
Step 2: Create the Database, User, and Grant Permissions
Once you’re at the MySQL prompt, run the following block. Replace myappdb, myappuser, and StrongPassword123! with your own values before running anything.
CREATE DATABASE myappdb CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myappuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON myappdb.* TO 'myappuser'@'localhost';
FLUSH PRIVILEGES;
A quick explanation of what each line does:
CREATE DATABASEwithutf8mb4— this charset handles emoji and multilingual characters correctly. The olderutf8in MySQL is actually a 3-byte subset and will silently fail on some characters. Always useutf8mb4for new databases.CREATE USER 'myappuser'@'localhost'— the@'localhost'part restricts this user to local connections only. If your app and database are on the same server, this is correct and more secure than@'%'.GRANT ALL PRIVILEGES ON myappdb.*— this grants full access to every table inmyappdbonly, not to all databases on the server. This is intentional.FLUSH PRIVILEGES— this reloads the grant tables so your changes take effect immediately. Some operations don’t require it in MySQL 8.0+, but running it never hurts and skipping it has caused me confusion before on older MariaDB setups.
Step 3: Verify the Setup
SHOW DATABASES;
SHOW GRANTS FOR 'myappuser'@'localhost';
EXIT;
You should see myappdb in the list and a GRANT ALL PRIVILEGES line for your user. If either is missing, recheck your commands for typos — MySQL won’t warn you if you accidentally create a database with a mismatched name.
Optional: Connect Your App
For a PHP application (like WordPress or a custom app), the database connection details would look like this in a config file:
<?php
define('DB_NAME', 'myappdb');
define('DB_USER', 'myappuser');
define('DB_PASSWORD', 'StrongPassword123!');
define('DB_HOST', 'localhost');
For a .env file used by Laravel or similar frameworks:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myappdb
DB_USERNAME=myappuser
DB_PASSWORD=StrongPassword123!
Common Issues & Troubleshooting
ERROR 1045 (28000): Access denied for user ‘myappuser’@’localhost’
This is the most common error after creating a user. Nine times out of ten, either the password in your app config doesn’t match what you set in MySQL, or the user wasn’t actually granted access to that specific database. Log back in as root and rerun the SHOW GRANTS command from Step 3 above. If no grants appear, run the GRANT ALL PRIVILEGES line again and flush privileges.
cPanel says the database name already exists, but I can’t see it
cPanel checks database names across all accounts on a shared server — not just yours. If a name conflicts with another account’s database (even though you can’t see it), cPanel will reject it. Simply choose a more unique name. Adding a short project abbreviation usually resolves it: mysite_proj2_db instead of mysite_db.
App says it can’t connect, but credentials are correct
Check whether your app is trying to connect via localhost or 127.0.0.1. In some environments — particularly with PHP and MySQL on Ubuntu — localhost resolves to a Unix socket while 127.0.0.1 uses TCP. If the MySQL user was created with @'localhost' and your app connects via 127.0.0.1, MySQL may treat it as a different host and deny access. Try switching the host value in your config and test again.
GRANT command denied to user ‘root’@’localhost’
This usually means you’re logged in as a MySQL user that doesn’t actually have the GRANT OPTION, or on some managed database setups (like certain VPS control panel stacks) the root user’s privileges have been scoped down. Log out and back in with the actual system root account via sudo mysql, not just mysql -u root -p.
mysql_secure_installation fails or loops asking for a password
As mentioned above, this is the socket auth issue. Run sudo mysql first, change root’s auth plugin with the ALTER USER command shown in Method 3 Step 1, then exit and retry mysql_secure_installation. This catches a lot of people on fresh Ubuntu/Debian VPS installs.
FAQ
Frequently Asked Questions
How many MySQL databases can I create on a cPanel hosting account?
It depends on your hosting plan. Shared hosting accounts typically have a limit set by the host — anywhere from 5 to unlimited databases depending on the package. You can check your current limit and usage under cPanel > MySQL Databases. On a VPS or dedicated server where you manage MySQL directly, there’s no enforced limit beyond disk space.
What's the difference between a MySQL user and a database?
A database is the container that holds your data — tables, rows, all of it. A user is a credential set (username + password) that’s allowed to connect to MySQL. Creating a database without a user, or a user without linking them to a database, means nothing can actually connect. You need both, and you need to explicitly grant the user access to the database.
Can I use the same MySQL user for multiple databases?
Yes. You can run GRANT ALL PRIVILEGES ON seconddb.* TO 'myappuser'@'localhost'; to give an existing user access to another database. Just make sure that’s intentional — giving one application user access to another app’s database is a security risk if that app is ever compromised.
How do I delete a MySQL database and user I no longer need?
In cPanel, go to MySQL Databases, scroll down to find the database, and click Delete. You’ll also see a separate section to remove users. Via CLI: DROP DATABASE myappdb; and DROP USER 'myappuser'@'localhost'; — run both from the MySQL prompt as root. Double-check you’re targeting the right database before running DROP; there’s no undo.
Do I need to restart MySQL after creating a new database or user?
No. Running FLUSH PRIVILEGES; is enough to apply permission changes immediately without a restart. MySQL restarts are only needed for config file changes (like edits to my.cnf). On a live server, restarting MySQL drops all active connections, so avoid it unless absolutely necessary.