Advertisements

How to Backup MySQL Database Using mysqldump

  • Post last modified:October 21, 2023
  • Post category:Web
  • Post comments:0 Comments
  • Reading time:5 mins read

If you have a MySQL database that you want to backup, We can use mysqldump command. In this post, I will show you how to use the mysqldump to create a backup of your MySQL database.

Table of Contents:

Backup a Single MySQL Database

Advertisements

To backup a single MySQL database:

mysqldump -u [username] -p [database_name] > [backup_file].sql

For example, if your username is wordpressuser, and you want to back up a database named wordpress, and save the backup file as wpdbbackup.sql,:

mysqldump -u wordpressuser -p wordpress > wpdbbackup.sql

After running this command, you will be prompted to enter your MySQL password. Once you enter your password, the backup process will start.

creating mysqldump backup

Backup All MySQL Databases

To backup all MySQL databases:

mysqldump -u [username] -p --all-databases > [backup_file].sql

For example, if your username is root, and you want to back up all databases on your MySQL server and save the backup file as all_databases_backup.sql, you can use the following command:

mysqldump -u root -p --all-databases > all_databases_backup.sql
backup all mysql databases using mysqldump

Restore MySQL Databases

Note: Navigate to the directory where the backup file is located using the cd command.

To restore a single MySQL database from the backup file, use the following command:

mysql -u [username] -p [database_name] < [backup_file].sql

example: mysql -u wordpressuser -p wordpress < wpdbbackup.sql

To restore all MySQL databases:

mysql -u [username] -p < [backup_file].sql

Advertisements

example: mysql -u root -p < all_databases_backup.sql

Conclusion

In this post, I have shown you how to use the mysqldump command to create a backup of your MySQL database. Make sure to back up your databases regularly… if you have any questions about this post please leave a comment.

Leave a Reply