MySQL Data Migration: Using mysqldump to Avoid Import/Export Errors
When migrating databases using graphical tools like phpMyAdmin, especially between different servers or MySQL versions, you often encounter issues like incompatible export formats, import timeouts, or failures due to large file sizes. This article introduces a more reliable method: using the official MySQL command-line tool mysqldump for backup and the mysql command for restoration, effectively avoiding common errors.
1. Exporting a Database with mysqldump
mysqldump is MySQL's built-in logical backup tool that generates a text file containing SQL statements.
Basic Command Format:
mysqldump -u [username] -p [database_name] > [output_filename].sql
Example:
To export a database named abc to a file db_abc.sql in the current directory using the root user:
mysqldump -u root -p abc > db_abc.sql
After executing, you will be prompted for the user's password. Enter the correct password to start the export.
Additional Notes and Common Parameters:
- Add
--single-transaction: For InnoDB tables, this parameter ensures a consistent backup without locking tables, ideal for production.mysqldump -u root -p --single-transaction abc > db_abc.sql - Export All Databases: Use
--all-databases.mysqldump -u root -p --all-databases > all_backup.sql - Export Only Structure: Add
--no-data.mysqldump -u root -p --no-data abc > abc_schema.sql
2. Importing a Database with the mysql Command
The exported SQL file must be imported using the mysql command-line client.
Basic Command Format:
mysql -u [username] -p [database_name] < [sql_filename].sql
Example:
To import data from /root/backup/db_abc.sql into the abc database on the target server:
mysql -u root -p abc < /root/backup/db_abc.sql
You will again be prompted for the password.
Key Prerequisites:
- The target database must already exist. If not, create it first:
CREATE DATABASE abc CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; - Ensure the importing user has sufficient privileges (e.g., CREATE, INSERT, DROP) on the target database.
3. Complete Migration Workflow Example
To migrate data from Server A to Server B:
- Export data on Server A:
mysqldump -u root -p --single-transaction --routines --triggers abc > db_abc.sql - Transfer the backup file to Server B: Use tools like
scp,rsync, orwget. - Create an empty database on Server B (if it doesn't exist):
mysql -u root -p -e "CREATE DATABASE abc;" - Import data on Server B:
mysql -u root -p abc < db_abc.sql
4. Why Is This More Reliable Than phpMyAdmin?
- Avoids Timeouts: Command-line operations are not limited by PHP execution time or web server timeout settings, making them suitable for large databases.
- Avoids File Size Limits: Not restricted by phpMyAdmin or server PHP configuration parameters like
upload_max_filesize. - Better Format Compatibility: SQL generated by
mysqldumphas high standards compliance, increasing success rates across different MySQL versions. - Greater Flexibility: Parameters allow selective backup of data, structure, or exclusion of specific tables.
By mastering the mysqldump and mysql command-line tools, you can perform database backup, migration, and restoration tasks more confidently and stably, eliminating the unexpected errors common with graphical tools.