Tutorials

How to Create MySQL Database and User with Full Privileges in cPanel

How to Create MySQL Database and User with Full Privileges cPanel - CpanelFree Guide
Written by Blog

Quick Answer: To create a MySQL database and user in cPanel: 1) Navigate to MySQL Databases (or MySQL Database Wizard), 2) Enter your database name and click Create Database, 3) Create a new MySQL user with a strong password, and 4) Under Add User to Database, select your user and database, check ALL PRIVILEGES, and click Make Changes.

Understanding the 3 Core Components of cPanel Databases

When hosting dynamic web applications (WordPress, Laravel, Node.js, Python), cPanel requires 3 distinct elements to establish secure database communication:

  • 1. Database: The storage container housing your tables, columns, and data rows.
  • 2. Database User: A secure account with authentication credentials.
  • 3. User Privileges (GRANT): The explicit permissions determining whether the user can SELECT, INSERT, UPDATE, DELETE, or DROP tables.

Step-by-Step Tutorial: Creating Database and User in cPanel

Step 1: Create a New Database

  1. Log in to your CpanelFree dashboard.
  2. Under the Databases category, click MySQL Databases.
  3. Under Create New Database, enter your desired database name (e.g. appdb).
  4. Click Create Database. Note the full database name including your account prefix (e.g. username_appdb).

Step 2: Create a MySQL Database User

  1. Scroll down to the MySQL Users – Add New User section.
  2. Enter a username (e.g. dbadmin).
  3. Use the Password Generator to generate a strong 18-character password. Copy and save it securely.
  4. Click Create User.

Step 3: Assign User to Database with Full Privileges

  1. Scroll down to Add User To Database.
  2. Select your newly created user from the User dropdown and your database from the Database dropdown.
  3. Click Add.
  4. On the Manage User Privileges screen, check the ALL PRIVILEGES box.
  5. Click Make Changes.

Connecting Your PHP or WordPress Application

Use your newly created credentials to configure your application connection string:

// PHP PDO Database Connection Example
$host = 'localhost';
$db   = 'username_appdb';
$user = 'username_dbadmin';
$pass = 'YourSecurePassword123!';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$pdo = new PDO($dsn, $user, $pass, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);

Managing Granular Database Privileges and Security Restrictions

While ALL PRIVILEGES is required for WordPress to execute core updates, plugin installations, and schema migrations, custom microservices and read-only reporting dashboards should be restricted to minimal privilege sets:

# Grant Read-Only Analytical Access
GRANT SELECT ON username_appdb.* TO 'username_readonly'@'localhost';

# Grant Standard CRUD Privileges (No Schema Alteration)
GRANT SELECT, INSERT, UPDATE, DELETE ON username_appdb.* TO 'username_appuser'@'localhost';
FLUSH PRIVILEGES;

Enabling Remote Database Access in cPanel (Remote MySQL)

If you run desktop database management tools (like DBeaver, TablePlus, or Navicat) or host a frontend application on an external server, authorize external IP addresses in cPanel under Remote MySQL by adding your public IP address (or % for wildcard access, paired with strong SSL encryption).

Best Practices for Database Security in Production Environments

  • 🔒 Never Use Root for Applications: Always create dedicated, isolated database users for each application rather than reusing the root account.
  • 🔒 Enforce Strong Alphanumeric Passwords: Generate random 24-character passwords combining upper/lowercase, numbers, and symbols.
  • 🔒 Restrict Host Binding: Ensure users are bound strictly to localhost (or specific trusted IP subnets) rather than the open wildcard (%).
  • 🔒 Disable Remote MySQL if Unused: Keep external port 3306 closed in your server firewall unless remote access is strictly required.

Automated Database Provisioning via cPanel UAPI Command Line

Sysadmins and developers can automate database and user creation in cPanel without clicking through the GUI using cPanel UAPI:

# Create database via cPanel CLI
uapi Mysql create_database name=username_customdb

# Create database user
uapi Mysql create_user name=username_customuser password='SecurePassword123!'

# Grant full privileges
uapi Mysql set_privileges_on_database user=username_customuser database=username_customdb privileges=ALL%20PRIVILEGES

Unlimited MySQL Databases on CpanelFree

Create unlimited MySQL databases, manage tables with phpMyAdmin, and enjoy NVMe SSD hosting at 100% zero cost on CpanelFree.

Create Free Account

Frequently Asked Questions

Why is my database username limited to 8 characters in cPanel?

Older MySQL specifications limited usernames to 16 characters total. Because cPanel prepends your account prefix (up to 8 characters), the customizable portion is restricted to 8 characters to ensure compatibility.

Managing Multiple WordPress Databases on a Single Hosting Account

In cPanel, you can create separate databases for staging subdomains, development branches, and production sites. Assigning unique database users with dedicated passwords to each site ensures that a compromised test environment cannot access production customer records.

Can a single database user access multiple databases?

Yes. In cPanel, you can assign the same user to multiple databases by repeating the Add User to Database step with full privileges for each database.

Pro Sysadmin Tip: Renaming cPanel Databases Safely

In modern cPanel versions, you can rename a database in 1-click under MySQL Databases > Modify Databases without exporting and re-importing SQL files. Remember to update DB_NAME in your application config immediately after renaming.

By properly isolating database users and enforcing strict privilege boundaries, you establish an enterprise-grade security standard across all your hosted web applications.

About the author

Blog

DevOps architect and Linux sysadmin specializing in server hardening, OpenLiteSpeed performance optimization, and free cloud hosting infrastructure.

Leave a Comment