WP-CLI Database Commands
Last Updated -
WP-CLI has some powerful tools built in for database management, here are some of the most used/convenient features. (If you haven't done so please view Using WP-CLI Command Line Tools first)
Note: Please use caution when performing commands on the database and always make sure to take a backup first.
Backing Up Your Database
Exports a .sql dump into your site directory.
For more information on creating database backups with WP-CLI, take a look at our tutorial on exporting databases with WP-CLI.
wp db export backup_name.sql
Importing a New .sql Dump
Imports the database .sql file that you indicate.
For more information, take a look at our tutorial on importing databases with WP-CLI.
wp db import new_database.sql
Truncating Your Database Tables and Dropping Tables Without the wp_ Prefix
This will reset the database to a fresh wp_ instance.
wp db reset
Opening a MySQL Client to Perform MySQL Queries
Connects to the database and opens the mysql command line client
wp db query
Exporting a .xml File
Exports the database in .xml/wxr format.
Note: Please see the documentation for handling authors from the wp-cli.org site
Importing a .xml File
Imports the .xml file you indicate.
wp import filename.xml
Search and Replace
Performs a search and replace function on the database that also handles serialized arrays.
The --all-tables flag will update tables that don't have the same table prefix that is used by WordPress.
You can also use --precise which uses PHP and is a bit more thorough but it takes longer so it should be used after the first process did not correct everything. Please consider cache on the server and in your local browser before using that though.
Note: Multi-site should use this article
wp search-replace 'http://old-domain.com' 'http://new-domain.com' --all-tables