WP-CLI Database Functions Cheat Sheet

A Software › WordPress cheat sheet in English by wirehead

Database functions for the Wordpress command line interface

7,082 views 1,284 downloads 21 Dec 2021 History

Inform­ational

wp db check
check the current status of the database
wp db size
display the database name and size
wp db prefix
display the database table prefix
wp db tables
lists the database tables
wp db columns
display inform­ation about a given table

Manipu­lation

wp db import
imports a database
wp db export
exports the database
wp db search
finds a string in the database
wp db query
executes a SQL query
# Execute a query stored in a file
$ wp db query < debug.sql

# Execute a query from prompt
$ wp db query '­<qu­ery­>'

DB Functions

 
This sheet is a quick reference of the commands available to operate with and manipulate within a Wordpress database using wp-cli.

EXAMPLES

 
# Create a new database.
$ wp db create
Success: Database created.

# Drop an existing database.
$ wp db drop --yes
Success: Database dropped.

# Reset the current database.
$ wp db reset --yes
Success: Database reset.

# Execute a SQL query stored in a file.
$ wp db query < debug.sql

Operations

wp db cli
open a MySQL console
wp db create
create a new database
wp db drop
drop an existing database
wp db reset
reset the current database
OPTIONS
(create, drop, reset)
[--dbu­ser­=<v­alu­e>]
pass username to mysql
Defaults to DB_USER
[--dbp­ass­=<v­alu­e>]
pass password to mysql
Defaults to DB_PAS­SWORD
[--yes]
avoid confir­mation message
wp db optimize
optimizes the database
wp db repair
repairs the database

Quick Help

wp db <su­bco­mma­nd> help
lists all options
This will provide further options and parameters available for the command and subcommand specified. The technique is applicable throughout the wp-cli.

Comments (1)

tripdog 21:44 29 Mar 2022

Very useful!

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.