wp db check
Overview
Run a diagnostic check on all WordPress database tables to detect corruption, errors, and inconsistencies — without modifying any data. The first command to run when a database problem is suspected.
What It Does
wp db check executes mysqlcheck --check against the WordPress database. It reads credentials from wp-config.php and reports any tables with issues. No data is changed — this is a read-only diagnostic.
Syntax
wp db check [--dbuser=<value>] [--dbpass=<value>] [--defaults-file=<value>]
Options & Flags
| Flag | Description |
|---|---|
--dbuser=VALUE | Override the DB user from wp-config.php |
--dbpass=VALUE | Override the DB password |
--defaults-file=PATH | Use a custom MySQL defaults file |
Basic Usage
Check all tables
wp db check
Output when healthy:
Success: Database checked.
Output when issues found:
wordpress.wp_options
note : Table does not support optimize, doing recreate + analyze instead
status : OK
wordpress.wp_postmeta
error : Table './wordpress/wp_postmeta' is marked as crashed and should be repaired
Error: Database check failed.
Real-World Scenarios
Scenario 1: Pre-update health check
# Always check before major updates
wp db check
if [ $? -eq 0 ]; then
echo "Database healthy — proceeding with update"
wp core update
wp core update-db
else
echo "Database issues detected — run wp db repair first"
fi
Scenario 2: Post-migration verification
# After importing a database from another server
wp db import dump.sql
wp db check && echo "Import verified ✅" || echo "Import has issues ❌"
Scenario 3: Scheduled nightly health monitoring
#!/bin/bash
# /etc/cron.daily/wp-db-check
if ! wp --path=/var/www/html db check --quiet; then
echo "DB check failed on $(hostname)" | mail -s "WP DB Alert" admin@example.com
fi
Scenario 4: Multi-site audit
SITES=("/var/www/site1" "/var/www/site2" "/var/www/site3")
for site in "${SITES[@]}"; do
echo -n "Checking ${site}: "
wp --path="$site" db check --quiet && echo "✅ OK" || echo "❌ FAILED"
done
Best Practices
- Run before every DB backup to ensure the backup is of a healthy database.
- Run after every import to verify the dump was applied cleanly.
- Automate daily checks via cron on production sites.
- Follow with
wp db repairwhen issues are found — never ignore errors.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
Table is marked as crashed | Unexpected shutdown/corruption | Run wp db repair |
Access denied error | Wrong DB credentials | Check wp-config.php |
| Command hangs | Very large tables | Be patient; or run mysqlcheck directly with --timeout |
Quick Reference
wp db check # Check all tables
wp db check && echo "OK" # Silent check with conditional
wp db repair # Fix issues found by check
Next Steps
wp db repair— fix errors found by check.wp db optimize— optimize tables after repair.wp db export— export a verified clean database.