1.数据备份
1.1使用mysqldump命令备份
mysqldump备份的语法格式:
mysqldump -u -h -p password dbname[tbname,[tbname...]] >filename.sql
1.使用mysqldump备份单个数据中的所有表
例:使用mysqldump命令备份数据库中的所有表,执行过程如下:
mysql> CREATE DATABASE booksDB;
Query OK, 1 row affected (0.00 sec)
mysql> USE booksDB;
Database changed
mysql> CREATE TABLE books(
-> bk_id INT NOT NULL PRIMARY KEY,
-> bk_title VARCHAR(50) NOT NULL,
-> copyright YEAR NOT NULL
-> );
Query OK, 0 rows affected (0.47 sec)
mysql> INSERT INTO books
-> VALUES(11078,'Learning MySQL',2010),
-> (11033,'Study Html',2011),
-> (11035,'How to use php',2003),
-> (11072,'Teach yourself javascript',2005),
-> (11028,'Learning C++',2005),
-> (11069,'MySQL professional',2009),
-> (11026,'Guide to MySQL 5.5',2008),
-> (11041,'Inside C++',2011);
Query OK, 8 rows affected (0.42 sec)
Records: 8 Duplicates: 0 Warnings: 0
mysql> CREATE TABLE authors(
-> auth_id INT NOT NULL PRIMARY KEY,
-> auth_name VARCHAR(20),
-> auth_gender CHAR(1)
-> );
Query OK, 0 rows affected (0.44 sec)
mysql> INSERT INTO authors
-> VALUES(1001,'WriterX','f'),
-> (1002,'WriterA','f'),
-> (1003,'WriterB','m'),
-> (1004,'WriterC','F'),
-> (1011,'WriterD','f'),
-> (1012,'WriterE','m'),
-> (1013,'WriterF','m'),
-> (1014,'WriterG','f'),
-> (1015,'WriterH','f');
Query OK, 9 rows affected (0.14 sec)
Records: 9 Duplicates: 0 Warnings: 0
mysql> CREATE TABLE authorbook(
-> auth_id INT NOT NULL,
-> bk_id INT NOT NULL,
-> PRIMARY KEY(auth_id,bk_id),
-> FOREIGN KEY(auth_id)REFERENCES authors (auth_id),
-> FOREIGN KEY(bk_id)REFERENCES books (bk_id)
-> );
Query OK, 0 rows affected (0.66 sec)
mysql> INSERT INTO authorbook
-> VALUES(1001,11033),
-> (1002,11035),
-> (1003,11072),
-> (1004,11028),
-> (1011,11078),
-> (1012,11026),
-> (1012,11041),
-> (1014,11069);
Query OK, 8 rows affected (0.05 sec)
Records: 8 Duplicates: 0 Warnings: 0
C:\Users\lenovo>mysqldump -u root -p booksdb>E:\MYSQLtest\booksdb_20181114.sql
Enter password: ******
这里是在E:\MYSQLtest文件下进行操作的。
进行如上操作后,打开E:\MYSQLtest文件夹,可以看到大致如下备份信息:
-- MySQL dump 10.13 Distrib 5.7.14, for Win64 (x86_64)
--
-- Host: localhost Database: booksdb
-- ------------------------------------------------------
-- Server version 5.7.14
/*!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 `authorbook`
--
DROP TABLE IF EXISTS `authorbook`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `authorbook` (
`auth_id` int(11) NOT NULL,
`bk_id` int(11) NOT NULL,
PRIMARY KEY (`auth_id`,`bk_id`),
KEY `bk_id` (`bk_id`),
CONSTRAINT `authorbook_ibfk_1` FOREIGN KEY (`auth_id`) REFERENCES `authors` (`auth_id`),
CONSTRAINT `authorbook_ibfk_2` FOREIGN KEY (`bk_id`) REFERENCES `books` (`bk_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `authorbook`
--
LOCK TABLES `authorbook` WRITE;
/*!40000 ALTER TABLE `authorbook` DISABLE KEYS */;
INSERT INTO `authorbook` VALUES (1012,11026),(1004,11028),(1001,11033),(1002,11035),(1012,11041),(1014,11069),(1003,11072),(1011,11078);
/*!40000 ALTER TABLE `authorbook` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `authors`
--
DROP TABLE IF EXISTS `authors`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `authors` (
`auth_id` int(11) NOT NULL,
`auth_name` varchar(20) DEFAULT NULL,
`auth_gender` char(1) DEFAULT NULL,
PRIMARY KEY (`auth_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `authors`
--
...
...省略部分内容
...
/*!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 2018-11-14 8:16:44
可以看到备份文件包含了一些信息,文件开头表明了备份文件使用的是mysqldump工具的版本号;接着是备份账户和主机信息,以及备份的数据库名称,最后是mysql服务器版本号。
关于SET语句,SET语句将一些系统变量赋值给用户定义变量,以确保被恢复的数据库的系统变量和原来备份时的变量相同。如:
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
这里SET语句将当前系统变量CHARACTER_SET_CLIENT
的值赋给用户定义变量@OLD_CHARACTER_SET_CLIENT
。其他变量与此类似。
备份文件的最后几行,MySQL使用SET语句恢复服务器系统原来的值,例如:
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
此语句将用户定义的变量@OLD_CHARACTER_SET_CLIENT
中保存的值赋给实际的系统变量CHARACTER_SET_CLIENT
。
符号的含义:
- - | /*! … */ |
---|---|
行为注释语句 | 可执行的MySQL注释 |
(注:40101表明这些语句只有在MySQL版本号为4.01.01或者更高的条件下才可以被执行)
2.使用mysqldump备份数据库中的某个表
语法格式:
mysqldump -u user -h host -p dbname [tbname,[tbname...]] > filename.sql
多个表名之间用空格隔开。
备份表与备份数据库中所有表的语句中不同的地方在于,要在数据库名称dbname后指定要备份的表名称。
例:备份数据库booksDB中的books表,输入语句如下:
C:\Users\lenovo>mysqldump -u root -p booksDB books>E:\MYSQLtest\books_20181114.sql
Enter password: ******
books_20181114.sql文件中只含有books表的CREATE和INSERT语句。
3.使用mysqldump备份多个数据库
语句格式:
mysqldump -u user -h host -p --databases [dbname,[dbname...]]>filename.sql
例:使用mysqldump备份booksDB和test_db数据库,输入语句如下:
C:\Users\lenovo>mysqldump -u user -h host -p --databases booksDB test_db>E:\MYSQLtest\books_testDB_20181114.sql
Enter password: ******
另外,--all-databases
参数可备份系统中所有数据库。
例:
C:\Users\lenovo>mysqldump -u root -p --all-databases>E:\MYSQLtest\alldbinMySQL_20181114.sql
Enter password: ******
!!!提示:如果在服务器上进行备份,并且表均为MyISAM表,应考虑使用mysqlhotcopy,这样可以更快捷地恢复和备份。
1.2直接复制整个数据库目录
1.使用mysqlhotcopy工具快速备份
语法格式:
mysqlhotcopy db_name_1,...,db_name_n /path/to/new_directory
2.数据还原
2.1使用MySQL命令还原
语法:
mysql -u user -p [dbname]
例:使用mysql命令将E:\MYSQLtes\booksdb_20181114.sql文件的备份导入数据库中,输入语句如下:
mysql -u root -p booksDB
例:使用root用户登录到服务器,使用source导入本地的备份文件booksdb_20181114.sql,输入语句如下:
mysql> USE booksDB;
Database changed
mysql> SOURCE E:\MYSQLtest\booksdb_20181114.sql
提示:执行source命令前,必须使用use语句,否则,恢复过程中可能会出现“ERROR 1046(3D000): NO database selected”的错误。
14.2.2 直接复制到数据库目录
如果数据库通过复制数据库文件备份,可以直接复制备份文件到MySQL数据目录下实现(只对MyISAM引擎有效)。
14.2.3 mysqlhotcopy快速恢复
>chown -R/usr/backup/test usr/local/mysql/data
14.3 数据库迁移
例:将www.abc.com主机上的MySQL数据库全部迁移到www.bcd.com主机上。在www.abc.com主机上执行如下命令:
mysqldump -h www.bac.com -uroot -p password dbname|
mysql -h www.bcd.com -uroot -p password
14.4 表的导出和导入
SELECT coiumnlist FROM table WHERE condition INTO OUTFILE'filename'[OPTIONS]
--OPTIONS选项
FIELDS TERMINATED BY 'value'
FIELDS [OPTIONALLY] ENCLOSED BY 'value'
FIELDS ESCAPED BY 'value'
LINES STARTING BY 'value'
LINES TERMINATED BY 'value'
SELECT coiumnlist FROM table WHERE condition 是一个查询语句,查询结果返回满足指定条件的一条或多条记录;
INTO OUTFILE的作用是把前面SELECT语句查询出来的结果导出到名称为“filename”的外部文件中。(注意,filename不可是已经存在的文件)
[OPTIONS]为可选参数,它包括FIELDS和LINES语句,其可能取值如下:
例:将view_test数据库下的student表中的记录导出到文本文件,命令如下:
mysql> SELECT * FROM view_test.student INTO OUTFILE "E:\MYSQLtest\student0.txt";
Query OK, 3 rows affected (0.10 sec)
例:使用FIELDS 和LINES导出数据:
mysql> SELECT * FROM view_test.student INTO OUTFILE "E:\MYSQLtest\student1.txt"
-> FIELDS
-> TERMINATED BY ','
-> ENCLOSED BY '\"'
-> ESCAPED BY '\''
-> LINES
-> TERMINATED BY '\r\n';
Query OK, 3 rows affected (0.00 sec)
mysqldump -T path -u root -p dbname[tables] [OPTIONS]
--OPTIONS 选项
--fields-terminated-by=value
--fields-enclosed-by=value
--fields-optionally-enclosed-by=value
--fields-escaped-by=value
--lines-terminated-by=value
只有指定了-T参数才可以导出纯文本文件;
path表示导出数据的目录;
tables:指定要导出的表名称
例:使用mysql将view_test数据库中的student表导出到文本:
C:\Users\lenovo>mysqldump -T E:\ view_test student -u root -p
Enter password: ******
可以看到在E的目录下产生了student.sql和student.txt文件。
在student.sql文件中可以看到CREATE 语句:
-- MySQL dump 10.13 Distrib 5.7.14, for Win64 (x86_64)
--
-- Host: localhost Database: view_test
-- ------------------------------------------------------
-- Server version 5.7.14
/*!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' */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `student`
--
DROP TABLE IF EXISTS `student`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `student` (
`s_id` int(11) NOT NULL,
`name` varchar(15) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!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 2018-12-21 10:18:38
student.txt包含数据包中的数据,内容如下:
1 wanglin1
2 gaoli
3 zhanghai
例:使用mysqldump命令导出文本,同时使用FIELDS选项:
C:\Users\lenovo>mysqldump -T E:\MYSQLtest view_test student -u root -p --fields-terminated-by=, --fields-optionally-enclosed-by=\" --fields-escaped-by=? --lines-terminated-by=\r\n
Enter password: ******
1,"wanglin1"
2,"gaoli"
3,"zhanghai"
mysql -u root -p --execute=“SELECT语句” dbname=filename.txt
C:\Users\lenovo>mysql -u root -p --execute="SELECT * FROM student;" view_test>E:\student3.txt
Enter password: ******
使用–html选项:
C:\Users\lenovo>mysql -u root -p --html --execute="SELECT * FROM fruits;" index_test>E:\MYSQLtest\fruits5.html
Enter password: ******
C:\Users\lenovo>mysql -u root -p --xml --execute="SELECT * FROM student;" view_test>E:\MYSQLtest\student6.xml
Enter password: ******
LOAD DATA INFILE 'filename.txt' INTO TABLE tablename [OPTIONS] [IGNORE number LINES]
--OPTIONS 选项
FIELDS TERMINATED BY 'value'
FIELDS [OPTIONALLY] ENCLOSED BY 'value'
FIELDS ESCAPED BY 'value'
LINES STARTING BY 'value'
LIINES STARTING BY 'value'
LOAD DATA 语句中关键字INFILE后面的filename文件为导入数据来源;tablename为待导入数据表的名称。
mysql> use view_test;
Database changed
mysql> delete from student ;
Query OK, 0 rows affected (0.10 sec)
mysql> LOAD DATA INFILE 'E:\MYSQLtest\student0.txt' INTO TABLE view_test.student;
Query OK, 3 rows affected (0.19 sec)
Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
查看:
mysql> SELECT * FROM student;
+------+----------+
| s_id | name |
+------+----------+
| 1 | wanglin1 |
| 2 | gaoli |
| 3 | zhanghai |
+------+----------+
3 rows in set (0.00 sec)