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.
Create wordpress database + wpuser@localhost, grant privileges on wordpress.*, then test with mysql -u wpuser -p -D wordpress -e "SELECT 1;".
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)
Most CREATE/GRANT statements produce no output on success.
Create the WordPress database
mysql -u root -p -e "CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
Expected output:
(no output on success)
Use case: Prepare the schema container for WordPress tables.
Create the WordPress user
mysql -u root -p -e "CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'REPLACE_ME_WITH_A_STRONG_PASSWORD';"
Expected output:
(no output on success)
Use case: Dedicated credentials for the application.
Grant privileges to the WordPress user
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:
(no output on success)
Use case: Allow normal WordPress install/update operations.
Verify the user can connect to the database
mysql -u wpuser -p -D wordpress -e "SELECT 1 AS ok;"
Expected output:
ok
1
Use case: Confirm credentials before you run the WordPress installer.
Verify the database is empty (before install)
mysql -u wpuser -p -D wordpress -e "SHOW TABLES;"
Expected output:
Empty set
Use case: Ensure you're not accidentally targeting an existing production database.
Add credentials to wp-config.php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'REPLACE_ME_WITH_A_STRONG_PASSWORD');
define('DB_HOST', 'localhost');
Expected output:
WordPress installer can connect to the database.
Use case: Connect WordPress to the DB using the dedicated user.
WordPress VPS Use Cases
| Scenario | What to do | Why |
|---|---|---|
| Fresh install | Create DB + user, then install | Prevents install failures |
| Migration | Create DB, restore dump, then update config | Clean separation from old DB |
| Incident response | Rotate wpuser password | Limits credential reuse risk |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| WordPress cannot connect | Wrong credentials or host | Test with mysql -u wpuser -p -D wordpress |
| "Access denied" | Grants missing or wrong host | Check SHOW GRANTS FOR 'wpuser'@'localhost' |
| Wrong DB targeted | DB_NAME mismatch | Validate DB_NAME and run SHOW TABLES; |
Best Practices
- Use
utf8mb4for WordPress (emoji-safe). - Use least privilege; do not use DB root.
- Test connectivity with CLI before installing.
Cheat Sheet
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';