Tutorials

|

15

minutes read

How to Backup and Restore MySQL Database

Tom

September 19, 2022

feature image

A better way to debug your PHP code

A better way to debug your PHP code

Khoa Pham

November 16, 2022

Discover iOS Dark Mode Programming

Discover iOS Dark Mode Programming

thomas.le

November 1, 2022

A quick tutorial on how to backup and recover MySQL Database using Mysqldump/MySQL command and workbench tool.

1. Backup with mysqldump:

Mysqldump is a utility used for logical backup. It backups all SQL create, insert statements for restoring objects and data. Mysqldump can also generate CSV files, delimited texts, and XML files for importing data to any database management tool such as SQL Server, Oracle, PostgreSQL, etc.

You can use the backup of all physical data functions so that files in the original format can be restored more quickly.

Backup single database:

– Show all databases on your server:

a screen when you enter code to show all database on your server

– Use this command to backup single database to dump file (.sql) with mysqldump

 backup single database to dump file (.sql) with mysqldump

– If you do not want  to type in your password when running mysqldump command (when running crontab job for schedule backup), create configure file include username/password in <user home_folder>/.my.cnf (hidden file) and grant permission as 600 as below:

 create configure file include username/password in <user home_folder>/.my.cnf (hidden file) and grant permission as 600

– Then run mysqldump without using a password for the backup

 run mysqldump without using a password

Backup multiple database with mysqldump:

– This can backup multiple databases with just one single mysqldump command. You can run it by using the –databases option to specify which database you want to backup into single dump files.

backup multiple databases with just one single mysqldump command

Backup all databases to separate dump files with timestamp:

If you want to backup all databases into separate dump files, each with a specific filename, you can write a simple script as below to use for crontab.

– Include username and password in file <home folder>/.my.cnf as below for run this script

run the script above to backup all database
a screenshot of what happens when the code is running perfectly

Backup all database into single dump file use mysqldump:

Backup all database into single dump file use mysqldump

Backup single table in a database:

Backup single table in a database

Backup all Procedure, Triggers and Events in database:

Backup all Procedure, Triggers and Events in database:

2. Restore with MySQL:

– Always test your backup by restoring it to the development/test environment.

– Backup with scripts using mysqldump command and restore with scripts using MySQL command.

Restore single database with MySQL command:

– Backup full single database first

Backup full single database first before restoring with mysql

– Testing restore function by dropping database beforehand

testing restore function by dropping database beforehand

– Create database again (can use different name for restoring) and then restore:

Create database again (can use different name for restoring)
finally restoring the database

Restore multiple database from single dump file:

– First, backup multiple databases with option –databases:

backup multiple databases with option –databases with mysql

– Drop 2 databases create again and restore

Drop 2 databases create again and restore
restore database in mysql

3. Data Export/Import Use MySQL Workbench tool:

– Use MySQL Workbench connect to MySQL Server and choose menu Data Export

Use MySQL Workbench connect to MySQL Server and choose menu Data Export
a MySQL Workbench screenshot when exporting data

Select which database name you want to backup and location and filename be exported

Select which database name you want to backup and location and filename be exported
export complete in MySQL Workbench

– You can backup individual tables in one database by selecting table name after selecting database name in menu Data Export

backup individual tables in one database in MySQL Workbench