How to Import and Export WordPress Databases Using the Command Line

Importing and exporting MySQL databases using the command line is extremely easy. It is often more efficient to use the command line instead of a tool such as phpMyAdmin. This is especially true when managing a large number of websites. An existing database and database user is required before importing a database using the examples below. Instructions for creating a database can be found in my post about creating a database using the command line.

Below are examples for importing or exporting a database using traditional MySQL commands as well as magic WP-CLI commands.

Import or Export a WordPress Database Using MySQL Commands

Requirements

  • Access to your server via SSH
  • An existing database (to export) or a database dump file to import

In the examples below, replace USER with your database username and DATABASE with your database name.

Export

mysqldump -u USER -p DATABASE > database.sql

Import

mysql -u USER -p DATABASE < database.sql

Import or Export a WordPress Database Using WP-CLI

Importing and exporting a database can be even easier using WP-CLI. WP-CLI is the command-line interface for WordPress. These commands are extremely efficient as they utilize the database host, name, user, and password credentials set in wp-config.php.

Requirements

  • Access to your server via SSH
  • WP-CLI installed on your server
  • An existing database (to export) or a database dump file to import

Import With WP-CLI

wp db import database.sql

WP-CLI db import documentation

Export With WP-CLI

wp db export --add-drop-table

Note: I usually like any export I create to include statements to drop the table if it exists. This makes importing databases easier. The –add-drop-table in the above adds this option to the export file. Read more in the WP-CLI db export documentation.

Next Steps

The following related articles may also be helpful.