我要评分
获取效率
正确性
完整性
易理解

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.
    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.

    Before restoring data to the database db1, you need to create the database db1.

    1
    mysql -h 192.168.202.102 -P 3306 -uroot -p123456 db1 < db1.sql