1、使用select ...into outfile ... 命令来导出数据
mysql> select * from tablename into outfile 'target_file' [option];
其中 option 参数可以是以下选项:
fields terminated by 'string' // 字段分隔符,默认为制表符'\t' fields [optionally] enclosed by 'char' // 字段引用符,如果加 optionally 选项则只用在 char、varchar 和 text 等字符型字段上,默认不使用引用符 fields escaped by ‘char’ // 转移字符、默认为 '\' lines starting by 'string' // 每行前都加此字符串,默认'' lines terminated by 'string' // 行结束符,默认为'\n' # char 表示此符号只能是单个字符,string表示可以是字符串。
2、使用mysqldump导出数据为文本。mysqldump实际调用的就是后者提供的接口,并在其上面添加了一些新的功能而已。
mysqldump -u username -T target_dir dbname tablename [option]
root@bogon:/usr/local/mysql/bin# ./mysqldump -uroot -p -T /data/mysql/dump t2 test --fields-terminated-by ',' --fields-optionally-enclosed-by '"' **************** test.txt ********************** zj@bogon:/data/mysql/dump$ more test.txt 1,"a","\\\"##!aa" 2,"b","helloworld" 3,"c","helloworld" 4,"d","helloworld" ***************** test.sql ********************* zj@bogon:/data/mysql/dump$ more test.sql -- MySQL dump 10.13 Distrib 5.7.18, for Linux (x86_64) -- -- Host: localhost Database: t2 -- ------------------------------------------------------ -- Server version 5.7.18-log /*!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 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 `test` -- DROP TABLE IF EXISTS `test`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `test` ( `id` int(11) DEFAULT NULL, `name` varchar(10) DEFAULT NULL, `content` varchar(100) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; /*!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 2017-09-25 11:14:06
以上就是mysql表导出的两种方法,希望对大家有所帮助。更多mysql学习指路:MySQL
推荐操作系统:windows7系统、mysql5.8、DELL G3电脑