TechBuzzPro

Export Import All MySQL Databases: Full Guide

· 5 min read · Apps & Browsers

In this article
  1. Why Export All Databases Instead of One?
  2. How to Export All MySQL Databases
  3. How to Import All Databases from a Dump
  4. Using phpMyAdmin for Export/Import
  5. Common Problems and How to Avoid Them
  6. Comparing mysqldump with Other Tools
  7. Safety and Best Practices
  8. Frequently Asked Questions

Yes, you can export and import all MySQL databases at once using the mysqldump command with the --all-databases flag, or by selecting multiple databases in tools like phpMyAdmin or MySQL Workbench. This article explains the exact commands, common pitfalls, and best practices for moving every database on a server.

Why Export All Databases Instead of One?

When you migrate a server, create a full backup, or clone a development environment, you usually need every database, not just one. Exporting them individually is time-consuming and error-prone, especially when you have dozens or hundreds of schemas. A full export also preserves:

  • User accounts and privileges (with the --all-databases option in mysqldump)
  • Stored procedures, functions, triggers, and events
  • Table structures, indexes, and data
  • System tables like mysql database (if you choose to include them)

It’s the standard approach for a complete server snapshot.

How to Export All MySQL Databases

The most common method is using mysqldump from the command line. Here’s the basic command:

mysqldump -u root -p --all-databases > all_databases.sql

This creates a single SQL file containing all databases. To include user accounts and grants, add --all-databases along with --flush-privileges:

mysqldump -u root -p --all-databases --flush-privileges > all_databases.sql

For a more consistent backup, use --single-transaction (for InnoDB tables) to avoid locking tables during the dump:

mysqldump -u root -p --all-databases --single-transaction > all_databases.sql

If you want to exclude certain databases (like the mysql system schema), you can use --ignore-table or --databases with a list. However, for a full migration, including the system tables is often necessary to preserve user accounts.

How to Import All Databases from a Dump

To restore the dump file, use the MySQL client:

mysql -u root -p < all_databases.sql

This will execute all SQL statements in the file, recreating databases, tables, and data. If the dump includes user accounts, they will be restored as well. If you’re importing into a fresh server, you may need to create the root user or use an account with sufficient privileges. If the file is large, you can use pv to monitor progress:

pv all_databases.sql | mysql -u root -p

Using phpMyAdmin for Export/Import

For those who prefer a graphical interface, phpMyAdmin offers a straightforward way:

  1. Log in to phpMyAdmin.
  2. Click the “Export” tab at the top.
  3. Select “Custom” or “Quick” – for all databases, choose “Custom” and then select “Select All” under the database list.
  4. Choose the format (usually SQL) and click “Go” to download the file.
  5. To import, go to the “Import” tab, choose the file, and click “Go”.

Note that phpMyAdmin may have file size limits (often 2MB by default) – you can change upload_max_filesize and post_max_size in PHP settings if needed.

Common Problems and How to Avoid Them

  • Permission errors: Exporting all databases requires the RELOAD and PROCESS privileges. If you get “Access denied”, use a user with full privileges, typically root.
  • Incomplete dumps due to large data: Use --max_allowed_packet to increase the packet size, e.g., --max_allowed_packet=512M.
  • Import timeouts: On shared hosting, PHP scripts may time out. Use command-line import instead or increase max_execution_time.
  • Character set issues: Add --default-character-set=utf8mb4 to both export and import to preserve Unicode data.
  • Foreign key constraints: If you import only some tables, you may get errors. Use --disable-foreign-key-checks in the dump or before import.

Comparing mysqldump with Other Tools

While mysqldump is the classic tool, other options exist:

  • MySQL Workbench: Provides a visual “Data Export” and “Data Import” wizard. It can export all schemas at once and offers scheduling for backups.
  • Percona XtraBackup: For large databases, this tool performs physical backups (copying data files) which are faster and less disruptive. It can back up all databases, but restoring requires a bit more care.
  • mysqlpump: A newer utility that supports parallel dumping, making it faster on multi-core systems. It uses a similar syntax: mysqlpump --all-databases > dump.sql.

Choose mysqldump for simplicity and compatibility, especially for smaller to medium databases. For huge databases, consider physical backups or mysqlpump.

Safety and Best Practices

Before exporting, ensure you have enough disk space for the dump file. Test the import on a staging server if possible. Always keep a backup of the dump file in a secure location, ideally off-site. When importing, be aware that it will overwrite existing databases with the same names. To avoid data loss, you can import into a temporary server first. Also, consider using --routines and --triggers flags (they are included by default with --all-databases in recent MySQL versions) to ensure all objects are captured.

Frequently Asked Questions

Does mysqldump --all-databases include user accounts?

Yes, in MySQL 5.6 and later, mysqldump --all-databases includes the mysql system database, which stores user accounts and privileges, but only if you use the --flush-privileges option to ensure they are applied on import. Without it, users may not be created properly.

Can I export all databases except a few?

Yes, you can use --databases with a list of databases, or use --ignore-table to exclude specific tables. For example: mysqldump -u root -p --databases db1 db2 --ignore-table=db1.logs > dump.sql. Alternatively, you can pipe the dump through grep to filter, but that’s risky.

Why does my import fail with “Unknown database” error?

This usually happens when the dump file does not contain CREATE DATABASE statements. If you used --databases without the --add-drop-database option, the database is not created. Ensure you use --all-databases or --databases (which automatically includes CREATE DATABASE). Alternatively, create the database manually before importing.

How long does it take to export all databases?

It depends on the total size of data, server hardware, and load. A small server with a few GB can take seconds to minutes; a large server with hundreds of GB can take hours. Using --single-transaction helps avoid locking, but the process is I/O intensive. You can estimate by checking the size of the data directory.

Can I export all databases without stopping the server?

Yes, if you use InnoDB tables, --single-transaction gives a consistent snapshot without locking. For MyISAM tables, you may need to lock tables or use --lock-tables, which can block writes. For a truly non-disruptive backup, consider Percona XtraBackup.