Backing Up the Database
To prevent data loss, you are advised to back up the database data before uninstalling the database. Before backing up the database, exit the MySQL environment. Perform backups as required.
Basic Commands
- Back up a database.
1mysqldump -h <Host> -P <Port NO.> -u <User name> -p<Password> --database <Database> > /Path/File.sql
- Restore a database.
1mysql -h <Host> -P <Port NO.> -u <User name> -p<Password> --<Database> < /opt/mytest_bak.sql
Example
In the following commands, the IP address following -h indicates the IP address of the host. If the database backup path is not specified, the backup file will be generated in the current directory.
- Back up all databases.
1mysqldump -h 192.168.202.102 -P 3306 -uroot -p123456 --all-databases > alldb.sql
- Back up the database db1 to db1.sql.
1mysqldump -h 192.168.202.102 -P 3306 -uroot -p123456 --databases db1 > db1.sql
- Back up the table tb1 in the database db1 to db1tb1.sql.
1mysqldump -h 192.168.202.102 -P 3306 -uroot -p123456 db1 tb1 > db1tb1.sql
- Restore the database db1.
Before restoring data to the database db1, you need to create the database db1.
1mysql -h 192.168.202.102 -P 3306 -uroot -p123456 db1 < db1.sql
Parent topic: (Optional) Backing Up the Database