How to backup and restore a MySQL database using mysqldump?
The mysqldump command in MySQL can be used to backup and restore databases. Here are the steps for backing up and restoring a database.
Backup database:
- Open the command prompt or terminal window.
- Enter the following command:
mysqldump -u username -p database_name > backup.sql
In this case, “username” is your MySQL username, “database_name” is the name of the database to be backed up, and “backup.sql” is the name of the backup file.
- backup file named backup.sql
Restore database.
- Open the command prompt or terminal window.
- Please enter the following command:
mysql -u username -p database_name < backup.sql
username is your MySQL username, database_name is the name of the database you want to restore, and backup.sql is the path to the backup file.
- After pressing the enter key to execute the command, the system will prompt for a password. Enter the password and wait for the recovery process to complete.
Before restoring the database, please ensure that the database has been created and is empty. Otherwise, there may be data loss or conflicts. If you want to restore the database to an existing one, please delete all data in that database first.