2. 使用mysqldump命令导出文本文件
**举例1:**使用mysqldump命令将将atguigu数据库中account表中的记录导出到文本文件:
mysqldump -uroot -p -T "/var/lib/mysql-files/" atguigu account
mysqldump命令执行完毕后,在指定的目录/var/lib/mysql-files/下生成了account.sql和account.txt文件。
打开account.sql文件,其内容包含创建account表的CREATE语句。
[root@node1 mysql-files]# cat account.sql -- MySQL dump 10.13 Distrib 8.0.26, for Linux (x86_64) -- -- Host: localhost Database: atguigu -- ------------------------------------------------------ -- Server version 8.0.26 /*!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 */; /*!50503 SET NAMES utf8mb4 */; /*!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 `account` -- DROP TABLE IF EXISTS `account`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `account` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `balance` int NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3; /*!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 2022-01-07 23:19:27
演示
导出
[root@centos7-mysql-1 mysql-files]# mysqldump -uroot -p -T "/var/lib/mysql-files/" atguigudb3 account Enter password: [root@centos7-mysql-1 mysql-files]# ll 总用量 12 -rw-r-----. 1 mysql mysql 50 8月 14 12:43 account1.txt -rw-r--r--. 1 root root 1460 8月 14 12:45 account.sql -rw-r-----. 1 mysql mysql 50 8月 14 12:45 account.txt [root@centos7-mysql-1 mysql-files]#
查看
[root@centos7-mysql-1 mysql-files]# vim account.sql -- MySQL dump 10.13 Distrib 8.0.25, for Linux (x86_64) -- -- Host: localhost Database: atguigudb3 -- ------------------------------------------------------ -- Server version 8.0.25 /*!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 */; /*!50503 SET NAMES utf8mb4 */; /*!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 `account` -- DROP TABLE IF EXISTS `account`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `account` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(15) DEFAULT NULL, `balance` decimal(15,0) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
[root@centos7-mysql-1 mysql-files]# vim account.txt 1 张三 30 2 李四 0 3 王五 110 4 马六 1000 ~
**举例2:**使用mysqldump将atguigu数据库中的account表导出到文本文件,使用FIELDS选项,要求字段之间使用逗号“,”间隔,所有字符类型字段值用双引号括起来:
mysqldump -uroot -p -T "/var/lib/mysql-files/" atguigu account --fields-terminated-by=',' --fields-optionally-enclosed-by='\"'
语句mysqldump语句执行成功之后,指定目录下会出现两个文件account.sql和account.txt。
打开account.sql文件,其内容包含创建account表的CREATE语句。
[root@node1 mysql-files]# cat account.sql -- MySQL dump 10.13 Distrib 8.0.26, for Linux (x86_64) -- -- Host: localhost Database: atguigu -- ------------------------------------------------------ -- Server version 8.0.26 /*!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 */; /*!50503 SET NAMES utf8mb4 */; /*!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 `account` -- DROP TABLE IF EXISTS `account`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `account` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `balance` int NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3; /*!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 2022-01-07 23:36:39
打开account.txt文件,其内容包含创建account表的数据。从文件中可以看出,字段之间用逗号隔开,字符类型的值被双引号括起来。
[root@node1 mysql-files]# cat account.txt 1,"张三",90 2,"李四",100 3,"王五",0
演示
导出
[root@centos7-mysql-1 mysql-files]# mysqldump -uroot -p -T "/var/lib/mysql-files/" atguigudb33 student --fields-terminated-by=',' --fields-optionally-enclosed-by='\"' Enter password: [root@centos7-mysql-1 mysql-files]# ll 总用量 20 -rw-r-----. 1 mysql mysql 50 8月 14 12:43 account1.txt -rw-r--r--. 1 root root 1460 8月 14 12:45 account.sql -rw-r-----. 1 mysql mysql 50 8月 14 12:45 account.txt -rw-r--r--. 1 root root 1398 8月 14 12:48 student.sql -rw-r-----. 1 mysql mysql 212 8月 14 12:48 student.txt [root@centos7-mysql-1 mysql-files]#
查看
[root@centos7-mysql-1 mysql-files]# vim student.sql -- MySQL dump 10.13 Distrib 8.0.25, for Linux (x86_64) -- -- Host: localhost Database: atguigudb33 -- ------------------------------------------------------ -- Server version 8.0.25 /*!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 */; /*!50503 SET NAMES utf8mb4 */; /*!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 */; /*!50503 SET character_set_client = utf8mb4 */; CREATE TABLE `student` ( `id` int NOT NULL, `name` varchar(20) DEFAULT NULL, `class` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; /*!40101 SET character_set_client = @saved_cs_client */; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
[root@centos7-mysql-1 mysql-files]# vim student.txt 1,"张三3","一班" 3,"李四1","一班" 6,"jerry","一班" 8,"王五","二班" 11,"Tim","一班" 15,"赵六","二班" 17,"Tom1","三班" 18,"Jerry","四班" 20,"钱七","三班" 22,"bbb","No.1" 23,"aaa","No.1"
3. 使用mysql命令导出文本文件
**举例1:**使用mysql语句导出atguigu数据中account表中的记录到文本文件:
mysql -uroot -p --execute="SELECT * FROM account;" atguigu> "/var/lib/mysql-files/account.txt"
打开account.txt文件,其内容包含创建account表的数据。
[root@node1 mysql-files]# cat account.txt id name balance 1 张三 90 2 李四 100 3 王五 0
演示
导出
[root@centos7-mysql-1 mysql-files]# mysql -uroot -p --execute="SELECT * FROM account;" atguigudb3> "/var/lib/mysql-files/account2.txt" Enter password: [root@centos7-mysql-1 mysql-files]# ll 总用量 24 -rw-r-----. 1 mysql mysql 50 8月 14 12:43 account1.txt -rw-r--r--. 1 root root 66 8月 14 12:52 account2.txt -rw-r--r--. 1 root root 1460 8月 14 12:45 account.sql -rw-r-----. 1 mysql mysql 50 8月 14 12:45 account.txt -rw-r--r--. 1 root root 1398 8月 14 12:48 student.sql -rw-r-----. 1 mysql mysql 212 8月 14 12:48 student.txt [root@centos7-mysql-1 mysql-files]#
查看
[root@centos7-mysql-1 mysql-files]# cat account2.txt id name balance 1 张三 30 2 李四 0 3 王五 110 4 马六 1000
**举例2:**将atguigu数据库account表中的记录导出到文本文件,使用–veritcal参数将该条件记录分为多行显示:
mysql -uroot -p --vertical --execute="SELECT * FROM account;" atguigu > "/var/lib/mysql-files/account_1.txt"
打开account_1.txt文件,其内容包含创建account表的数据。
[root@node1 mysql-files]# cat account_1.txt *************************** 1. row *************************** id: 1 name: 张三 balance: 90 *************************** 2. row *************************** id: 2 name: 李四 balance: 100 *************************** 3. row *************************** id: 3 name: 王五 balance: 0
演示
导出
[root@centos7-mysql-1 mysql-files]# mysql -uroot -p --vertical --execute="SELECT * FROM account;" atguigudb3 > "/var/lib/mysql-files/account3.txt" Enter password: [root@centos7-mysql-1 mysql-files]# ll 总用量 28 -rw-r-----. 1 mysql mysql 50 8月 14 12:43 account1.txt -rw-r--r--. 1 root root 66 8月 14 12:52 account2.txt -rw-r--r--. 1 root root 410 8月 14 12:54 account3.txt -rw-r--r--. 1 root root 1460 8月 14 12:45 account.sql -rw-r-----. 1 mysql mysql 50 8月 14 12:45 account.txt -rw-r--r--. 1 root root 1398 8月 14 12:48 student.sql -rw-r-----. 1 mysql mysql 212 8月 14 12:48 student.txt [root@centos7-mysql-1 mysql-files]#
查看
[root@centos7-mysql-1 mysql-files]# cat account3.txt *************************** 1. row *************************** id: 1 name: 张三 balance: 30 *************************** 2. row *************************** id: 2 name: 李四 balance: 0 *************************** 3. row *************************** id: 3 name: 王五 balance: 110 *************************** 4. row *************************** id: 4 name: 马六 balance: 1000 [root@centos7-mysql-1 mysql-files]#
**举例3:**将atguigu数据库account表中的记录导出到xml文件,使用–xml参数,具体语句如下。
mysql -uroot -p --xml --execute="SELECT * FROM account;" atguigu>"/var/lib/mysql-files/account_3.xml"
[root@node1 mysql-files]# cat account_3.xml <?xml version="1.0"?> <resultset statement="SELECT * FROM account" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <row> <field name="id">1</field> <field name="name">张三</field> <field name="balance">90</field> </row> <row> <field name="id">2</field> <field name="name">李四</field> <field name="balance">100</field> </row> <row> <field name="id">3</field> <field name="name">王五</field> <field name="balance">0</field> </row> </resultset>
演示
[root@centos7-mysql-1 mysql-files]# mysql -uroot -p --xml --execute="SELECT * FROM account;" atguigudb3>"/var/lib/mysql-files/account4.xml" Enter password: [root@centos7-mysql-1 mysql-files]# cat account4.xml <?xml version="1.0"?> <resultset statement="SELECT * FROM account" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <row> <field name="id">1</field> <field name="name">张三</field> <field name="balance">30</field> </row> <row> <field name="id">2</field> <field name="name">李四</field> <field name="balance">0</field> </row> <row> <field name="id">3</field> <field name="name">王五</field> <field name="balance">110</field> </row> <row> <field name="id">4</field> <field name="name">马六</field> <field name="balance">1000</field> </row> </resultset> [root@centos7-mysql-1 mysql-files]#
说明:如果要将表数据导出到html文件中,可以使用 --html 选项。然后可以使用浏览器打开。
6.2 表的导入
1. 使用LOAD DATA INFILE方式导入文本文件
举例1:
使用SELECT…INTO OUTFILE将atguigu数据库中account表的记录导出到文本文件
SELECT * FROM atguigu.account INTO OUTFILE '/var/lib/mysql-files/account_0.txt';
删除account表中的数据:
DELETE FROM atguigu.account;
从文本文件account.txt中恢复数据:
LOAD DATA INFILE '/var/lib/mysql-files/account_0.txt' INTO TABLE atguigu.account;
查询account表中的数据:
select * from account; /* + ---- +-------- +--------- + | id | name | balance | + ---- +-------- +--------- + | 1 | 张三 | 90 | | 2 | 李四 | 100 | | 3 | 王五 | 0 | + ---- +-------- +--------- + 3 rows in set (0.00 sec) */
演示
mysql> SELECT * FROM atguigudb3.account INTO OUTFILE '/var/lib/mysql-files/account_0.txt'; Query OK, 4 rows affected (0.01 sec) #ll mysql> select * from account; +----+--------+---------+ | id | name | balance | +----+--------+---------+ | 1 | 张三 | 30 | | 2 | 李四 | 0 | | 3 | 王五 | 110 | | 4 | 马六 | 1000 | +----+--------+---------+ 4 rows in set (0.00 sec) mysql> delete from account; Query OK, 4 rows affected (0.00 sec) mysql> LOAD DATA INFILE '/var/lib/mysql-files/account_0.txt' INTO TABLE atguigudb3.account; Query OK, 4 rows affected (0.01 sec) Records: 4 Deleted: 0 Skipped: 0 Warnings: 0 mysql> select * from account; +----+--------+---------+ | id | name | balance | +----+--------+---------+ | 1 | 张三 | 30 | | 2 | 李四 | 0 | | 3 | 王五 | 110 | | 4 | 马六 | 1000 | +----+--------+---------+ 4 rows in set (0.00 sec) mysql>
举例2:
选择数据库atguigu,使用SELECT…INTO OUTFILE将atguigu数据库account表中的记录导出到文本文件,使用FIELDS选项和LINES选项,要求字段之间使用逗号","间隔,所有字段值用双引号括起来:
SELECT * FROM atguigu.account INTO OUTFILE '/var/lib/mysql-files/account_1.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '\"';
删除account表中的数据:
DELETE FROM atguigu.account;
从/var/lib/mysql-files/account.txt中导入数据到account表中:
LOAD DATA INFILE '/var/lib/mysql-files/account_1.txt' INTO TABLE atguigu.account FIELDS TERMINATED BY ',' ENCLOSED BY '\"';
查询account表中的数据,具体SQL如下:
select * from account; select * from account; /* + ---- +-------- +--------- + | id | name | balance | + ---- +-------- +--------- + | 1 | 张三 | 90 | | 2 | 李四 | 100 | | 3 | 王五 | 0 | + ---- +-------- +--------- + 3 rows in set (0.00 sec) */
2. 使用mysqlimport方式导入文本文件
举例:
导出文件account.txt,字段之间使用逗号","间隔,字段值用双引号括起来:
SELECT * FROM atguigu.account INTO OUTFILE '/var/lib/mysql-files/account.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '\"';
删除account表中的数据:
DELETE FROM atguigu.account;
使用mysqlimport命令将account.txt文件内容导入到数据库atguigu的account表中:
mysqlimport -uroot -p atguigu '/var/lib/mysql-files/account.txt' --fields-terminated-by=',' --fields-optionally-enclosed-by='\"'
查询account表中的数据:
select * from account; select * from account; /* + ---- +-------- +--------- + | id | name | balance | + ---- +-------- +--------- + | 1 | 张三 | 90 | | 2 | 李四 | 100 | | 3 | 王五 | 0 | + ---- +-------- +--------- + 3 rows in set (0.00 sec) */
演示
备份
mysql> SELECT * FROM atguigudb3.teacher INTO OUTFILE '/var/lib/mysql-files/teacher.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '\"'; Query OK, 6 rows affected (0.01 sec) mysql> select * from teacher; +----+--------------+ | id | name | +----+--------------+ | 1 | zhangsan | | 2 | lisi | | 3 | wangwu | | 4 | zhaoliu | | 5 | songhongkang | | 6 | leifengyang | +----+--------------+ 6 rows in set (0.00 sec) mysql> delete from teacher; Query OK, 6 rows affected (0.00 sec)
恢复
[root@centos7-mysql-1 mysql-files]# ll [root@centos7-mysql-1 mysql-files]# mysqlimport -uroot -p atguigudb3 '/var/lib/mysql-files/teacher.txt' --fields-terminated-by=',' --fields-optionally-enclosed-by='\"' Enter password: atguigudb3.teacher: Records: 6 Deleted: 0 Skipped: 0 Warnings: 0
查看
mysql> select * from teacher; +----+--------------+ | id | name | +----+--------------+ | 1 | zhangsan | | 2 | lisi | | 3 | wangwu | | 4 | zhaoliu | | 5 | songhongkang | | 6 | leifengyang | +----+--------------+ 6 rows in set (0.00 sec)
除了前面介绍的几个选项之外,mysqlimport支持需要选项,常见的选项有:
- –columns=column_list,-c column_list:该选项采用逗号分隔的列名作为其值。列名的顺序只是如何匹配数据文件列和表列。
- –compress,-C:压缩在客户端和服务器之间发送的所有信息(如果二者均支持压缩)。. -d, --delete:导入文本文件前请空表。
- –force,-f:忽视错误。例如,如果某个文本文件的表不存在,就继续处理其他文件。不使用–force,若表不存在,则mysqlimport退出。
- –host=host_name,-h host host_name:将数据导入给定主机上的MySQL服务器,默认主机是localhost。
- –ignore,-i:参见–replace选项的描述。
- –ignore-lines=n:忽视数据文件的前n行。
- –local,-L:从本地客户端读入输入文件。
- –lock-tables,-l:处理文本文件前锁定所有表,以便写入。这样可以确保所有表在服务器上保持同步。
- –password[=password], -p[password]:当连接服务器时使用的密码。如果使用短选项形式(-p),选项和密码之间不能有空格。如果在命令行中–password或-p选项后面没有密码值,就提示输入一个密码。
- –port=port_num,-P port_num:用户连接的TCP/IP端口号。
- –protocol={TCP|SOCKET|PIPE|MEMORY}:使用的连接协议。
- –replace,-r --replace和–ignore选项控制复制唯一键值已有记录的输入记录的处理。如果指定–replace,新行替换有相同唯一键值的已有行;如果指定–ignore,复制已有唯一键值的输入行被跳过;如果不指定这两个选项,当发现一个复制键值时会出现一个错误,并且忽视文本文件的剩余部分。
- –silent, -s:沉默模式。只有出现错误时才输出信息。
- –user=username,-u user_name:当连接服务器时MySQL使用的用户名。
- –verbose,-v:冗长模式。打印出程序操作的详细信息。
- –version,-V:显示版本信息并退出。