注意:mysqldump命令首先需要查出备份的表的结构,再在文本文件中生成一个create语句,
然后将表中的所有记录,转换成一条insert语句。这些create语句和insert语句都是还原时使用的。
还原数据库时,就是先用create语句来建表,insert语句来还原数据。
========================================================================================
一、mysqldump命令备份某个数据库里的某些表;
语法格式:mysqldump -u username -ppassword dbname table1 table2…… > backupname.sql
dbname:表示数据库名称;
table1 table2……:表示表的名称
backupname.sql:表示路径和备份的文件名称;
通常备份为.sql文件,但是并非一定是.sql文件,.sql文件给人的感觉是数据库文件。也可以是.txt文件等。
=========================================================
备份前数据准备:
cmd进入DOS环境,输入命令,备份 cr01 和 employee 这两个表:
将see数据库里面的cr01和employee表进行备份, 备份到桌面:C:\Users\del\Desktop\, 备份文件名为see.sql
mysqldump -u root -p123 see cr01 employee > C:\Users\del\Desktop\see.sql
桌面多了一个see.sql文件,文件内容如下:
-- MySQL dump 10.13 Distrib 5.5.15, for Win32 (x86) -- -- Host: localhost Database: see -- ------------------------------------------------------ -- Server version 5.5.15 /*!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 `cr01` -- DROP TABLE IF EXISTS `cr01`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `cr01` ( `sx` int(50) DEFAULT NULL, `mz` varchar(50) DEFAULT NULL, `bz` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `cr01` -- LOCK TABLES `cr01` WRITE; /*!40000 ALTER TABLE `cr01` DISABLE KEYS */; INSERT INTO `cr01` VALUES (1,'sww','sww01'),(2,'aww','aww02'),(3,'qww','qww03'),(4,'eww','eww04'),(5,'rww','rww05'),(6,'yww','yww06'),(7,'uww','uww07'); /*!40000 ALTER TABLE `cr01` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `employee` -- DROP TABLE IF EXISTS `employee`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `employee` ( `num` int(50) DEFAULT NULL, `d_id` int(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `age` int(50) DEFAULT NULL, `sex` varchar(50) DEFAULT NULL, `homeadd` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `employee` -- LOCK TABLES `employee` WRITE; /*!40000 ALTER TABLE `employee` DISABLE KEYS */; INSERT INTO `employee` VALUES (1,1001,'zhangsan',26,'nan','beijing'),(2,1001,'lisi',24,'nv','hunan'),(3,1002,'wangwu',25,'nan','jiangsu'),(4,1004,'aric',15,'nan','yingguo'); /*!40000 ALTER TABLE `employee` 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 2019-12-26 21:37:01
上面这个文件里面没有创建数据库的语句。因为是还原到一个已有的数据库里。
=========================================================================================
二、备份某个数据库里面的所有表:
mysqldump -u root -p123 see > C:\Users\del\Desktop\s.sql
-- MySQL dump 10.13 Distrib 5.5.15, for Win32 (x86) -- -- Host: localhost Database: see -- ------------------------------------------------------ -- Server version 5.5.15 /*!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 `cr01` -- DROP TABLE IF EXISTS `cr01`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `cr01` ( `sx` int(50) DEFAULT NULL, `mz` varchar(50) DEFAULT NULL, `bz` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `cr01` -- LOCK TABLES `cr01` WRITE; /*!40000 ALTER TABLE `cr01` DISABLE KEYS */; INSERT INTO `cr01` VALUES (1,'sww','sww01'),(2,'aww','aww02'),(3,'qww','qww03'),(4,'eww','eww04'),(5,'rww','rww05'),(6,'yww','yww06'),(7,'uww','uww07'); /*!40000 ALTER TABLE `cr01` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `department` -- DROP TABLE IF EXISTS `department`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `department` ( `d_id` int(50) DEFAULT NULL, `d_name` varchar(50) DEFAULT NULL, `functione` varchar(50) DEFAULT NULL, `address` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `department` -- LOCK TABLES `department` WRITE; /*!40000 ALTER TABLE `department` DISABLE KEYS */; INSERT INTO `department` VALUES (1001,'keyanbu','yanfachanpin','3lou5hao'),(1002,'shengchanbu','shengchanchanp','5louyiceng'),(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating'); /*!40000 ALTER TABLE `department` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `employee` -- DROP TABLE IF EXISTS `employee`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `employee` ( `num` int(50) DEFAULT NULL, `d_id` int(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `age` int(50) DEFAULT NULL, `sex` varchar(50) DEFAULT NULL, `homeadd` varchar(50) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `employee` -- LOCK TABLES `employee` WRITE; /*!40000 ALTER TABLE `employee` DISABLE KEYS */; INSERT INTO `employee` VALUES (1,1001,'zhangsan',26,'nan','beijing'),(2,1001,'lisi',24,'nv','hunan'),(3,1002,'wangwu',25,'nan','jiangsu'),(4,1004,'aric',15,'nan','yingguo'); /*!40000 ALTER TABLE `employee` 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 2019-12-26 21:59:15