Backing up and restoring MySQL databases are essential tasks in database administration. Regular backups ensure that data can be recovered in case of accidental deletion, hardware failure, or other issues.
MySQL supports different types of backups based on the method and data consistency. These include:
The mysqldump utility is a popular tool for creating logical backups by generating SQL statements that recreate the database, tables, and data.
mysqldump -u username -p database_name > backup_file.sql
mysqldump -u root -p employees > employees_backup.sql
mysqldump -u username -p --all-databases > all_databases_backup.sql
A physical backup involves copying MySQL's actual data files. This can be done while the server is running (hot backup) or when the server is stopped (cold backup).
Stop the MySQL server:
sudo systemctl stop mysql
Copy the data directory (usually located at /var/lib/mysql):
sudo systemctl start mysql
Restore a Single Database:
mysql -u username -p database_name < backup_file.sql
mysql -u root -p employees < employees_backup.sql
Restore All Databases:
mysql -u username -p < all_databases_backup.sql