Hi Dude, this article is simple method how to backup and restore database mysql from command line cli. To backup and restore a MySQL database from the command line, we can use native command sql mysqldump
and mysql
commands, respectively. Lets do it.
Backup Mysql One Database from cli
Backup One Database
- Open a terminal and navigate to the directory where you want to save the backup file.
- Run the following command to create a backup of the database:
mysqldump -u username -p database_name > backup_file.sql
- Replace
username
with the username you use to connect to the MySQL server anddatabase_name
with the name of the database you want to back up. The>
operator redirects the output of themysqldump
command to a file namedbackup_file.sq
l. - Enter the password for the MySQL user when prompted.
- The backup file will be created in the current directory.
Restore One Database
Run the following command to restore the database.
mysql -u username -p database_name < backup_file.sql
Backup All Database
Execute this command create a backup of all databases:
mysqldump -u username -p --all-databases > backup_file.sql
Restore Mysql All Database
If we want to restore all database, please run the following command to restore.
mysql -u username -p < backup_file.sql
Backup Restore Mysql Include Store Procedure
This is additional, if we want to backup and restore mysql with include store procedure SPR.
mysqldump -u username -p --routines database_name > backup_file.sql
And to restore, run this command below.
mysql -u username -p --routines database_name < backup_file.sql
That is simply article about how to backup and restore mysql from command line cli. Maybe usefull.