Skip to main content

WordPress Database Setup

Before you install WordPress, you need a database and a dedicated database user. The safest pattern is: create the database, create a least-privilege user scoped to that database, then verify connectivity with the mysql CLI. This prevents common install-time failures and reduces security risk.

Quick Summary

Create wordpress database + wpuser@localhost, grant privileges on wordpress.*, then test with mysql -u wpuser -p -D wordpress -e "SELECT 1;".

warning

Do not reuse the DB root account for WordPress. Use a dedicated user.

Mental Model

Prerequisites

  • MySQL/MariaDB server installed and running.
  • Admin DB access (typically root) to create databases and users.

Examples (Commands + Expected Output)

Output varies

Most CREATE/GRANT statements produce no output on success.

Create the WordPress database

create-wordpress-database.sql
mysql -u root -p -e "CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

Expected output:

example-output-create-db.txt
(no output on success)

Use case: Prepare the schema container for WordPress tables.

Create the WordPress user

create-wordpress-user.sql
mysql -u root -p -e "CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'REPLACE_ME_WITH_A_STRONG_PASSWORD';"

Expected output:

example-output-create-user.txt
(no output on success)

Use case: Dedicated credentials for the application.

Grant privileges to the WordPress user

grant-wordpress-user.sql
mysql -u root -p -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, CREATE TEMPORARY TABLES, LOCK TABLES ON wordpress.* TO 'wpuser'@'localhost';"

Expected output:

example-output-grant.txt
(no output on success)

Use case: Allow normal WordPress install/update operations.

Verify the user can connect to the database

verify-wordpress-user-connection.sh
mysql -u wpuser -p -D wordpress -e "SELECT 1 AS ok;"

Expected output:

example-output-select-1.txt
ok
1

Use case: Confirm credentials before you run the WordPress installer.

Verify the database is empty (before install)

verify-empty-db-before-install.sh
mysql -u wpuser -p -D wordpress -e "SHOW TABLES;"

Expected output:

example-output-show-tables-empty.txt
Empty set

Use case: Ensure you're not accidentally targeting an existing production database.

Add credentials to wp-config.php

wp-config.php (example)
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'REPLACE_ME_WITH_A_STRONG_PASSWORD');
define('DB_HOST', 'localhost');

Expected output:

expected-result-wp-config.txt
WordPress installer can connect to the database.

Use case: Connect WordPress to the DB using the dedicated user.

WordPress VPS Use Cases

ScenarioWhat to doWhy
Fresh installCreate DB + user, then installPrevents install failures
MigrationCreate DB, restore dump, then update configClean separation from old DB
Incident responseRotate wpuser passwordLimits credential reuse risk

Troubleshooting

SymptomLikely causeFix
WordPress cannot connectWrong credentials or hostTest with mysql -u wpuser -p -D wordpress
"Access denied"Grants missing or wrong hostCheck SHOW GRANTS FOR 'wpuser'@'localhost'
Wrong DB targetedDB_NAME mismatchValidate DB_NAME and run SHOW TABLES;

Best Practices

  • Use utf8mb4 for WordPress (emoji-safe).
  • Use least privilege; do not use DB root.
  • Test connectivity with CLI before installing.
Cheat Sheet
wordpress-db-setup-cheat-sheet.sql
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'REPLACE_ME';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, CREATE TEMPORARY TABLES, LOCK TABLES
ON wordpress.* TO 'wpuser'@'localhost';
SHOW GRANTS FOR 'wpuser'@'localhost';

What's Next