Managing MySQL databases through the terminal is an efficient way to handle backups and data transfers. Below are simple commands to export (backup) and import (restore) databases.
1. Exporting a MySQL Database
Use the mysqldump command to create a backup file:
mysqldump -u [username] -p [database_name] > backup.sql
Examples:
-
- Export a single database:
mysqldump -u root -p mydatabase > mydatabase_backup.sql
-
- Export all databases:
mysqldump -u root -p --all-databases > full_backup.sql
-
- Compress the backup file:
mysqldump -u root -p mydatabase | gzip > backup.sql.gz
2. Importing a MySQL Database
Use the mysql command to restore data from a backup file:
mysql -u [username] -p [database_name] < backup.sql
Examples:
-
- Restore a database:
mysql -u root -p mydatabase < mydatabase_backup.sql
-
- Import a compressed backup:
gunzip < backup.sql.gz | mysql -u root -p mydatabase
Conclusion
Using these simple MySQL export and import commands, you can efficiently back up and restore databases. Keep these handy for database management! π