数据库的转储和恢复

  1. 使用 mysqldump 转储数据库

    • 转储数据库的格式:
      mysqldump --set-gtid-purged=OFF -u 用户名 -p密码 数据库名> 目标文件的路径及文件名
      把数据库 com 的信息转储到 db.txt 文件中:
      
          mysqldump --set-gtid-purged=OFF -u root -phappy com> d:/doc/it/db.txt
                                                     
      
      D:\j\msql\bin>mysqldump --set-gtid-purged=OFF -u root -phappy com> d:/doc/it/db.txt
      mysqldump: [Warning] Using a password on the command line interface can be insecure.
      D:\j\msql\bin>
                                                     
      打开文件 db.txt 可以看到:
      -- MySQL dump 10.13  Distrib 9.6.0, for Win64 (x86_64)
      --
      -- Host: localhost    Database: com
      -- ------------------------------------------------------
      -- Server version	9.6.0
      
                         中间内容省略
      
      -- Dump completed on 2026-05-30 21:24:06
                                      
  2. 恢复转储文件

    • 恢复转储文件的格式:
      mysql -u 用户名 -p密码 新数据库名 < 被恢复转储文件的路径及文件名
      新建数据库 db:
      create database db;
      恢复转储文件 db.txt 到新数据库 db 中:
      
          mysql -u root -phappy db < d:/doc/it/db.txt
                                                     
      D:\j\msql\bin>mysql -u root -phappy db < d:/doc/it/db.txt
      mysql: [Warning] Using a password on the command line interface can be insecure.
      
      D:\j\msql\bin>