文章教程

17.5实战演练——数据的备份与恢复

9/17/2020 9:40:31 PM 人评论 次浏览

17.5 实战演练——数据的备份与恢复

备份有助于保护数据库,通过备份可以完整保存MySQL中各个数据库的特定状态。通过还原,可以在系统出现故障导致数据丢失或者不合理操作对数据库造成灾难时,恢复数据库中的数据。作为MySQL的管理人员,应该定期备份所有活动的数据库。因此无论怎样强调数据库的备份工作都不过分。本章综合案例将向读者提供数据库备份与还原的方法与过程。

01 使用mysqldump命令将suppliers表备份到文件C:\bktestdir\suppliers_bk.sql。

首先创建系统目录,在系统C盘下面新建文件夹bktestdir,然后打开命令行窗口,输入语句如下:

  C:\ >mysqldump -u root -p test_db suppliers > C:\bktestdir\suppliers_bk.sql
  Enter password: **

语句执行完毕,打开目录C:\bktestdir,可以看到已经创建好的备份文件suppliers_bk.sql,内容如下:

  -- MySQL dump 10.13  Distrib 5.5.13, for Win32 (x86)
  --
  -- Host: localhost    Database: test_db
  -- ------------------------------------------------------
  -- Server version	5.5.13
  /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  /*!40101 SET NAMES utf8 */;
  /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
  /*!40103 SET TIME_ZONE='+00:00' */;
  /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_
  CHECKS=0 */;
  /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;
  /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  --
  -- Table structure for table 'suppliers'
  --
  DROP TABLE IF EXISTS 'suppliers';
  /*!40101 SET @saved_cs_client     = @@character_set_client */;
  /*!40101 SET character_set_client = utf8 */;
  CREATE TABLE 'suppliers' (
    's_id' int(11) NOT NULL AUTO_INCREMENT,
    's_name' char(50) NOT NULL,
    's_city' char(50) DEFAULT NULL,
    's_zip' char(10) DEFAULT NULL,
    's_call' char(50) NOT NULL,
    PRIMARY KEY ('s_id')
  ) ENGINE=InnoDB AUTO_INCREMENT=108 DEFAULT CHARSET=utf8;
  /*!40101 SET character_set_client = @saved_cs_client */;
  --
  -- Dumping data for table 'suppliers'
  --
  LOCK TABLES 'suppliers' WRITE;
  /*!40000 ALTER TABLE 'suppliers' DISABLE KEYS */;
  INSERT INTO 'suppliers' VALUES (101,'FastFruit Inc.','Tianjin','463400','48075'),(102,'LT
Supplies','Chongqin
  g','100023','44333'),(103,'ACME','Shanghai','100024','90046'),(104,'FNK
Inc.','Zhongshan','212021','11111'),(105,
  'Good Set','Taiyuang','230009','22222'),(106,'Just Eat Ours','Beijing','010','45678'),(107,'DK Inc.','Qingdao','23000
  9','33332');
  /*!40000 ALTER TABLE 'suppliers' ENABLE KEYS */;
  UNLOCK TABLES;
  /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
  /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
  -- Dump completed on 2011-08-20 15:07:44

02 使用mysql命令将备份文件suppliers_bk.sql中的数据还原suppliers表。

为了验证还原之后数据的正确性,删除suppliers表中的所有记录,登录MySQL,输入语句:

  mysql> USE test_db;
  Database changed
  mysql> DELETE FROM suppliers;
  Query OK, 7 rows affected (0.00 sec)

此时,suppliers表中不再有任何数据记录,在MySQL命令行输入还原语句如下:

  mysql> source C:/bktestdir/suppliers_bk.sql;

语句执行过程中会出现多行提示信息,执行成功之后使用SELECT语句查询suppliers表的内容如下:

mysql> SELECT * FROM suppliers;

+------ +----------------- +-------------- +--------- +--------+
s_id s_name s_city s_zip s_call
+------ +----------------- +-------------- +--------- +--------+
101 FastFruit Inc. Tianjin 463400 48075
102 LT Supplies Chongqing 100023 44333
103 ACME Shanghai 100024 90046
104 FNK Inc. Zhongshan 212021 11111
105 Good Set Taiyuang 230009 22222
106 Just Eat Ours Beijing 010 45678
107 DK Inc. Qingdao 230009 33332
+------ +----------------- +-------------- +--------- +--------+

7 rows in set (0.00 sec)

由查询结果可以看到,还原操作成功。

03 使用SELECT… INTO OUTFILE语句导出suppliers表中的记录,导出文件位于目录C:\bktestdir下,名称为suppliers_out.txt。

执行过程如下:

  mysql> SELECT * FROM test_db.suppliers INTO OUTFILE "C:/bktestdir/suppliers_out.txt"
      -> FIELDS
      -> TERMINATED BY ','
      -> ENCLOSED BY '\"'
      -> LINES
      -> STARTING BY '<'
      -> TERMINATED BY '>\r\n';
  Query OK, 7 rows affected (0.00 sec)

TERMINATED BY ','指定不同字段之间使用逗号分隔开;ENCLOSED BY '\"'指定字段值使用双引号包括;STARTING BY '< '指定每行记录以左箭头符号开始;TERMINATED BY ' >\r\n';指定每行记录以右箭头符号和回车换行符结束。语句执行完毕,打开目录C:\bktestdir,可以看到已经创建好的导出文件suppliers_out.txt,内容如下:

  <"101","FastFruit Inc.","Tianjin","463400","48075">
  <"102","LT Supplies","Chongqing","100023","44333">
  <"103","ACME","Shanghai","100024","90046">
  <"104","FNK Inc.","Zhongshan","212021","11111">
  <"105","Good Set","Taiyuang","230009","22222">
  <"106","Just Eat Ours","Beijing","010","45678">
  <"107","DK Inc.","Qingdao","230009","33332">

04 使用LOAD DATA INFILE语句导入suppliers_out.txt数据到suppliers表。

首先使用DELETE语句删除suppliers表中的所有记录,然后输入导入语句:

  mysql> LOAD DATA INFILE 'C:/bktestdir/suppliers_out.txt' INTO TABLE test_db.suppliers
      -> FIELDS
      -> TERMINATED BY ','
      -> ENCLOSED BY '\"'
      -> LINES
      -> STARTING BY '<'
      -> TERMINATED BY '>\r\n';
  Query OK, 7 rows affected (0.00 sec)
  Records: 7  Deleted: 0  Skipped: 0  Warnings: 0

语句执行之后,suppliers_out.txt文件中的数据将导入suppliers表中,由于导出TXT文件时指定了一些特殊字符,因此还原语句中也要指定这些字符,已确保还原后数据的完整性和正确性。

05 使用musqldump命令将suppliers表中的记录导出到文件C:\bktestdir\suppliers_html. html。

导出表数据到HTML文件,使用mysql命令时需要指定--html选项,在Windows命令行窗口输入导出语句如下:

  mysql -u root -p --html --execute="SELECT * FROM suppliers;" test_db > C:/bktestdir/suppliers_html.html

语句执行完毕,打开目录C:\bktestdir,可以看到已经创建好的导出文件suppliers_html.html,读者可以使用浏览器打开将该文件,在浏览器中显示格式和内容如图17-3所示。

image

图17-3 浏览器中显示导出文件的内容

教程类别