wp db
Performs basic database operations using credentials stored in wp-config.php.
Subcommands
| Command | Description |
|---|---|
wp db check | Checks the current status of the database. |
wp db clean | Removes all tables with $table_prefix from the database. |
wp db cli | Opens a MySQL console using credentials from wp-config.php. |
wp db columns | Displays information about a given table. |
wp db create | Creates a new database. |
wp db drop | Deletes the existing database. |
wp db export | Exports the database to a file or to STDOUT. |
wp db import | Imports a database from a file or from STDIN. |
wp db optimize | Optimizes the database. |
wp db prefix | Displays the database table prefix. |
wp db query | Executes a SQL query against the database. |
wp db repair | Repairs the database. |
wp db reset | Removes all tables from the database. |
wp db search | Finds a string in the database. |
wp db size | Displays the database name and size. |
wp db tables | Lists the database tables. |
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.sqlwp db check
Checks the current status of the database.
Runs mysqlcheck utility with --check using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Synopsis
wp db check [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--dbuser=<value>] | Username to pass to mysqlcheck. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysqlcheck. Defaults to DB_PASSWORD. |
[--<field>=<value>] | Extra arguments to pass to mysqlcheck. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db check
Success: Database checked.wp db clean
Removes all tables with $table_prefix from the database.
Runs DROP_TABLE for each table that has a $table_prefix as specified in wp-config.php.
Synopsis
wp db clean [--dbuser=<value>] [--dbpass=<value>] [--yes] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--dbuser=<value>] | Username to pass to mysql. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysql. Defaults to DB_PASSWORD. |
[--yes] | Answer yes to the confirmation message. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db clean --yes
Success: Tables dropped.wp db cli
Opens a MySQL console using credentials from wp-config.php.
Synopsis
wp db cli [--database=<database>] [--default-character-set=<character-set>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--database=<database>] | Use a specific database. Defaults to DB_NAME. |
[--default-character-set=<character-set>] | Use a specific character set. Defaults to DB_CHARSET when defined. |
[--dbuser=<value>] | Username to pass to mysql. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysql. Defaults to DB_PASSWORD. |
[--<field>=<value>] | Extra arguments to pass to mysql. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
# Open MySQL console
$ wp db cli
mysql>wp db columns
Displays information about a given table.
Synopsis
wp db columns <table> [--format]Parameters
| Parameter | Description |
|---|---|
<table> | Name of the database table. |
[--format] | Render output in a particular format. Default: table. Options: table, csv, json, yaml |
Examples
$ wp db columns wp_posts
+-----------------------+---------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+---------------------+------+-----+---------------------+----------------+
| ID | bigint(20) unsigned | NO | PRI | | auto_increment |
| post_author | bigint(20) unsigned | NO | MUL | 0 | |
| post_date | datetime | NO | | 0000-00-00 00:00:00 | |
| post_date_gmt | datetime | NO | | 0000-00-00 00:00:00 | |
| post_content | longtext | NO | | | |
| post_title | text | NO | | | |
...
+-----------------------+---------------------+------+-----+---------------------+----------------+wp db create
Creates a new database.
Runs CREATE_DATABASE SQL statement using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Synopsis
wp db create [--dbuser=<value>] [--dbpass=<value>] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--dbuser=<value>] | Username to pass to mysql. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysql. Defaults to DB_PASSWORD. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db create
Success: Database created.wp db drop
Deletes the existing database.
Runs DROP_DATABASE SQL statement using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Synopsis
wp db drop [--dbuser=<value>] [--dbpass=<value>] [--yes] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--dbuser=<value>] | Username to pass to mysql. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysql. Defaults to DB_PASSWORD. |
[--yes] | Answer yes to the confirmation message. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db drop --yes
Success: Database dropped.wp db export
Exports the database to a file or to STDOUT.
Runs mysqldump utility using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php. Accepts any valid mysqldump flags.
Synopsis
wp db export [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--tables=<tables>] [--exclude_tables=<tables>] [--include-tablespaces] [--porcelain] [--add-drop-table] [--defaults]Parameters
| Parameter | Description |
|---|---|
[<file>] | The name of the SQL file to export. If ‘-‘, then outputs to STDOUT. If omitted, it will be ‘{dbname}-{Y-m-d}-{random-hash}.sql’. |
[--dbuser=<value>] | Username to pass to mysqldump. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysqldump. Defaults to DB_PASSWORD. |
[--<field>=<value>] | Extra arguments to pass to mysqldump. |
[--tables=<tables>] | The comma separated list of specific tables to export. |
[--exclude_tables=<tables>] | The comma separated list of specific tables that should be skipped from exporting. |
[--include-tablespaces] | Skips adding the default –no-tablespaces option to mysqldump. |
[--porcelain] | Output filename for the exported database. |
[--add-drop-table] | Include a DROP TABLE IF EXISTS statement before each CREATE TABLE statement. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
# Export database with drop query included
$ wp db export --add-drop-table
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
# Export certain tables
$ wp db export --tables=wp_options,wp_users
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
# Export all tables matching a wildcard
$ wp db export --tables=$(wp db tables 'wp_user*' --format=csv)
Success: Exported to 'wordpress_dbase-db72bb5.sql'.
# Export database to STDOUT.
$ wp db export -
-- MySQL dump 10.13 Distrib 5.7.19, for osx10.12 (x86_64)
...wp db import
Imports a database from a file or from STDIN.
Runs SQL queries using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Synopsis
wp db import [<file>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--skip-optimization] [--defaults]Parameters
| Parameter | Description |
|---|---|
[<file>] | The name of the SQL file to import. If ‘-‘, then reads from STDIN. If omitted, it will look for ‘{dbname}.sql’. |
[--dbuser=<value>] | Username to pass to mysql. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysql. Defaults to DB_PASSWORD. |
[--<field>=<value>] | Extra arguments to pass to mysql. |
[--skip-optimization] | When using an SQL file, do not include speed optimization such as disabling auto-commit and key checks. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db import wordpress_dbase.sql
Success: Imported from 'wordpress_dbase.sql'.wp db optimize
Optimizes the database.
Runs mysqlcheck utility with --optimize=true using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Synopsis
wp db optimize [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--dbuser=<value>] | Username to pass to mysqlcheck. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysqlcheck. Defaults to DB_PASSWORD. |
[--<field>=<value>] | Extra arguments to pass to mysqlcheck. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db optimize
Success: Database optimized.wp db prefix
Displays the database table prefix.
Display the database table prefix, as defined by the database handler’s interpretation of the current site.
Synopsis
wp db prefixExamples
$ wp db prefix
wp_wp db query
Executes a SQL query against the database.
Executes an arbitrary SQL query using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Use the --skip-column-names MySQL argument to exclude the headers from a SELECT query. Pipe the output to remove the ASCII table entirely.
Synopsis
wp db query [<sql>] [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]Parameters
| Parameter | Description |
|---|---|
[<sql>] | A SQL query. If not passed, will try to read from STDIN. |
[--dbuser=<value>] | Username to pass to mysql. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysql. Defaults to DB_PASSWORD. |
[--<field>=<value>] | Extra arguments to pass to mysql. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
# Execute a query stored in a file
$ wp db query < debug.sql
# Query for a specific value in the database
$ wp db query 'SELECT option_value FROM wp_options WHERE option_name="home"' --skip-column-names
+---------------------+
| https://example.com |
+---------------------+
# Check all tables in the database
$ wp db query "CHECK TABLE $(wp db tables | paste -s -d, -);"Multisite Usage
Please note that the global --url parameter will have no effect on this command. In order to query for data in a site other than your primary site, you will need to manually modify the table names to use the prefix that includes the site’s ID.
# Get the home option for your second site
$ wp db query 'SELECT option_value FROM wp_2_options WHERE option_name="home"' --skip-column-nameswp db repair
Repairs the database.
Runs mysqlcheck utility with --repair=true using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Synopsis
wp db repair [--dbuser=<value>] [--dbpass=<value>] [--<field>=<value>] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--dbuser=<value>] | Username to pass to mysqlcheck. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysqlcheck. Defaults to DB_PASSWORD. |
[--<field>=<value>] | Extra arguments to pass to mysqlcheck. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db repair
Success: Database repaired.wp db reset
Removes all tables from the database.
Runs DROP_DATABASE and CREATE_DATABASE SQL statements using DB_HOST, DB_NAME, DB_USER and DB_PASSWORD database credentials specified in wp-config.php.
Synopsis
wp db reset [--dbuser=<value>] [--dbpass=<value>] [--yes] [--defaults]Parameters
| Parameter | Description |
|---|---|
[--dbuser=<value>] | Username to pass to mysql. Defaults to DB_USER. |
[--dbpass=<value>] | Password to pass to mysql. Defaults to DB_PASSWORD. |
[--yes] | Answer yes to the confirmation message. |
[--defaults] | Loads the environment’s MySQL option files. |
Examples
$ wp db reset --yes
Success: Database reset.wp db search
Finds a string in the database.
Searches through all of the text columns in a selection of database tables for a given string. Outputs colorized references to the string.
Defaults to searching through all tables registered to $wpdb. On multisite, this default is limited to the tables for the current site.
Synopsis
wp db search <search> [<tables>...] [--network] [--all-tables-with-prefix] [--all-tables] [--before_context=<num>] [--after_context=<num>] [--regex] [--regex-flags=<regex-flags>] [--regex-delimiter=<regex-delimiter>] [--table_column_once] [--one_line] [--matches_only] [--stats] [--table_column_color=<color_code>] [--id_color=<color_code>] [--match_color=<color_code>] [--fields=<fields>] [--format=<format>]Parameters
| Parameter | Description |
|---|---|
<search> | String to search for. The search is case-insensitive by default. |
[<tables>...] | One or more tables to search through for the string. |
[--network] | Search through all the tables registered to $wpdb in a multisite install. |
[--all-tables-with-prefix] | Search through all tables that match the registered table prefix. |
[--all-tables] | Search through ALL tables in the database, regardless of the prefix. |
[--before_context=<num>] | Number of characters to display before the match. Default: 40 |
[--after_context=<num>] | Number of characters to display after the match. Default: 40 |
[--regex] | Runs the search as a regular expression (without delimiters). |
[--regex-flags=<regex-flags>] | Pass PCRE modifiers to the regex search. |
[--stats] | Output stats on the number of matches found, time taken, tables/columns/rows searched. |
[--format=<format>] | Render output in a particular format. Options: table, csv, json, yaml, ids, count |
Examples
# Search through the database for the 'wordpress-develop' string
$ wp db search wordpress-develop
wp_options:option_value
1:http://wordpress-develop.dev
...
# Search using regular expressions with stats
$ wp db search 'https?://' --regex --stats
wp_comments:comment_author_url
1:https://wordpress.org/
...
Success: Found 99146 matches in 10.752s (10.559s searching). Searched 12 tables, 53 columns, 1358907 rows.
# Search for a string and print the result as a table
$ wp db search https://localhost:8889 --format=table --fields=table,column,match
+------------+--------------+-----------------------------+
| table | column | match |
+------------+--------------+-----------------------------+
| wp_options | option_value | https://localhost:8889 |
| wp_options | option_value | https://localhost:8889 |
| wp_posts | guid | https://localhost:8889/?p=1 |
| wp_users | user_url | https://localhost:8889 |
+------------+--------------+-----------------------------+wp db size
Displays the database name and size.
Synopsis
wp db size [--size_format=<format>] [--tables] [--human-readable] [--format=<format>] [--scope=<scope>] [--network] [--decimals=<decimals>] [--all-tables-with-prefix] [--all-tables]Parameters
| Parameter | Description |
|---|---|
[--size_format=<format>] | Display the database size in a specific format. Default: b. Options: b (bytes), kb, mb, gb, tb |
[--tables] | Show information for each table. |
[--human-readable] | Display the database size in a human readable format. |
[--format=<format>] | Render output in a particular format. Default: table. Options: table, csv, json, yaml |
[--scope=<scope>] | Filter tables by scope. Options: all, global, blog, old |
[--network] | Show the database sizes for all network sites. |
[--decimals=<decimals>] | Number of decimals to display. Default: 0 |
[--all-tables-with-prefix] | Show all tables that match the prefix, even if not registered. |
[--all-tables] | Show ALL tables in the database. |
Examples
$ wp db size
+------------------+------+
| Name | Size |
+------------------+------+
| wordpress_dbase | 128M |
+------------------+------+
$ wp db size --tables
+-----------------------+-------+
| Name | Size |
+-----------------------+-------+
| wp_comments | 64KB |
| wp_options | 128KB |
| wp_posts | 1MB |
...
+-----------------------+-------+wp db tables
Lists the database tables.
Synopsis
wp db tables [<table>...] [--scope=<scope>] [--network] [--all-tables-with-prefix] [--all-tables] [--format=<format>]Parameters
| Parameter | Description |
|---|---|
[<table>...] | List tables based on wildcard search pattern or specific table name. |
[--scope=<scope>] | Can be all, global, blog, or old tables. Default: all |
[--network] | List all network tables based on multisite setup. |
[--all-tables-with-prefix] | List all tables that match the table prefix even if not registered on $wpdb. |
[--all-tables] | List all tables in the database, regardless of the prefix. |
[--format=<format>] | Render output in a particular format. Default: list. Options: list, csv |
Examples
# List tables for a single site, delimited by a space.
$ wp db tables --format=csv
wp_users,wp_usermeta,wp_posts,wp_comments,wp_links,wp_options,wp_postmeta,wp_terms,wp_term_taxonomy,wp_term_relationships,wp_termmeta,wp_commentmeta
# List all tables with wildcard
$ wp db tables 'wp_user*'
wp_users
wp_usermeta