How to perform data backup and recovery in MySQL?
In MySQL, you can use the mysqldump tool to backup and restore data. Here are the steps for backing up and restoring data:
Data backup:
- Open the command line tool and sign in to the MySQL database.
- Backup the entire database using the following command:
mysqldump -u username -p database_name > backup.sql
Replace username with your database username and database_name with the name of the database you want to backup. You will be prompted to enter a password to complete the backup operation. If you only want to backup specific tables, you can use the following command:
mysqldump -u username -p database_name table_name > backup.sql
Replace table_name with the name of the table you want to back up.
Data recovery:
- Create a new database (if necessary):
mysql -u username -p -e "CREATE DATABASE new_database_name"
Replace username with your database username, and new_database_name with the name of the new database.
2. Use the following command to restore the entire database:
mysql -u username -p database_name < backup.sql
Replace username with your database username and database_name with the name of the database you want to restore. You will be prompted to enter a password to complete the restore operation. If you only want to restore specific tables, you can use the following command:
mysql -u username -p database_name < backup.sql
Replace “table_name” with the name of the table you want to restore.
Please note that backup and recovery operations may take some time depending on the size of the database and network speed. Make sure to backup the database before executing these operations to prevent any unexpected data loss.