Rate This Document
Findability
Accuracy
Completeness
Readability

Backing Up the Database

To prevent data loss, you are advised to back up the database data before uninstalling the database. Perform backups as required.

Basic Commands

  • Back up a database.
    1
    mysqldump -h <Host> -P <Port NO.> -u <User name> -p<Password> --database <Database> > /Path/File.sql
    
  • Restore a database.
    1
    mysql -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.
    1
    mysqldump -h 192.168.202.102 -P 3306 -uroot -p123456 --all-databases > alldb.sql
    
  • Back up the database db1 to db1.sql.
    1
    mysqldump -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.
    1
    mysqldump -h 192.168.202.102 -P 3306 -uroot -p123456 db1 tb1 > db1tb1.sql
    
  • Restore the database db1.
    1
    mysql -h 192.168.202.102 -P 3306 -uroot -p123456 db1 < db1.sql