MySQL Backup and Restore

Backing up and restoring your MySQL database is essential for data protection and recovery. MySQL provides tools like mysqldump to help with this process.

Backing Up a Database

You can use the mysqldump command to create a backup of your database.

Example: Using mysqldump

mysqldump -u username -p database_name > backup.sql

Code Explanation: This command backs up the specified database to a file named backup.sql. You will be prompted to enter your password.

Restoring a Database

To restore a database from a backup, use the mysql command.

Example: Restoring from a Backup

mysql -u username -p database_name < backup.sql

Code Explanation: This command restores the database from the backup.sql file. You will be prompted to enter your password.

Best Practices

  • Schedule regular backups to ensure data protection.
  • Store backups in a secure and redundant location.

Key Takeaways

  • Back up your databases regularly to prevent data loss.
  • Use mysqldump and mysql commands for easy backup and restoration.