Skip to main content

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

FlagDescription
--dbuser=VALUEOverride the DB user from wp-config.php
--dbpass=VALUEOverride the DB password
--defaults-file=PATHUse 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

  1. Run before every DB backup to ensure the backup is of a healthy database.
  2. Run after every import to verify the dump was applied cleanly.
  3. Automate daily checks via cron on production sites.
  4. Follow with wp db repair when issues are found — never ignore errors.

Troubleshooting

ProblemCauseFix
Table is marked as crashedUnexpected shutdown/corruptionRun wp db repair
Access denied errorWrong DB credentialsCheck wp-config.php
Command hangsVery large tablesBe 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