How to Create Logical Backups and Restore a MySQL Database?

06-02-2024 02:51:07

Creating a logical backup of a MySQL database involves using the mysqldump command, while restoration is performed using the mysql command. Below are detailed instructions on how to back up and restore. If you are using the Windows operating system, it's necessary to navigate to the bin directory under the mysql directory to execute these commands.

To back up a specific database:

mysqldump -u[username] -p[password] [database name]>xxx.sql
mysqldump -uroot -p123456 test>test.sql (example)
mysqldump -uroot -p123456 --default-character-set=utf8 test>test.sql (setting default encoding)

To back up a specific table:

mysqldump -uroot -p[password] [database name] [table name]>xxx.sql
mysqldump -uroot -p123456 test table>table.sql (example)

To back up the structure of a database (without data):

mysqldump -uroot -p123456 -d --add-drop-table test>test.sql

To restore a database:

mysql -u[username] -p[password] [database name]<xxx.sql
mysql -uroot -p123456 wzx<wzx.sql (example)