该语句的基本语法如下:
SELECT column1, column2, ... INTO OUTFILE 'file_path' FIELDS TERMINATED BY 'field_separator' ENCLOSED BY 'field_encloser' LINES TERMINATED BY 'line_separator' FROM table_name;
以下是一个示例:
SELECT id, name, email INTO OUTFILE '/tmp/users.txt' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM users;
从名为 users 的表中选择 id、name 和 email 列,并将结果保存到 /tmp/users.txt 文件中。文件中的每一行都包含一个记录,记录中的字段以逗号分隔,并用双引号包围。行之间用换行符分隔。
注意:
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
原因:
show global variables like '%secure_file_priv%';
+------------------+-----------------------------------------+ | Variable_name | Value | +------------------+-----------------------------------------+ | secure_file_priv | D:\work\mysql\MySQL-Server-8.0\Uploads\ | +------------------+-----------------------------------------+
解决:
演示:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | product | | sys | | xj | +--------------------+ 6 rows in set (0.00 sec) mysql> use product; Database changed mysql> show tables; +-------------------+ | Tables_in_product | +-------------------+ | emp | | emp_log | | goods | | order_for_goods | | person_table | | sequence | | test_transaction | | text_alter | | text_copy | | text_null | | text_table | | text_table2 | | users | +-------------------+ 13 rows in set (0.00 sec) mysql> SELECT * -> INTO OUTFILE 'D:/work/mysql/MySQL-Server-8.0/Uploads/emp.sql' -> FIELDS TERMINATED BY ',' -> ENCLOSED BY '"' -> LINES TERMINATED BY '\n' -> FROM emp; Query OK, 15 rows affected (0.00 sec)
mysqldump -uxiaojian -proot product > D:\work\mysql\MySQL-Server-8.0\Uploads\product.sql
-- MySQL dump 10.13 Distrib 5.7.28, for Win64 (x86_64) -- -- Host: localhost Database: product -- ------------------------------------------------------ -- Server version 8.0.33 /*!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 `emp` -- DROP TABLE IF EXISTS `emp`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `emp` ( `id` int NOT NULL AUTO_INCREMENT, `sex` int DEFAULT NULL, `user_name` varchar(15) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL, `salary` decimal(10,2) DEFAULT NULL, `join_time` datetime DEFAULT NULL, `department` int DEFAULT NULL, `address` varchar(255) DEFAULT NULL, `text` char(255) DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, KEY `idx_name_salary` (`user_name`,`salary`) ) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `emp` -- LOCK TABLES `emp` WRITE; /*!40000 ALTER TABLE `emp` DISABLE KEYS */; INSERT INTO `emp` VALUES (1,1,'郭成龙',1800.90,'2023-06-07 00:00:00',1,NULL,NULL),(2,1,'小王',3600.89,'2023-05-02 00:00:00',2,NULL,NULL),(3,0,'马成虫',1000.10,'2023-01-08 00:00:00',2,NULL,NULL),(4,1,'老狗',1100.90,'2023-04-07 00:00:00',3,NULL,NULL),(5,0,'臭弟弟',4500.99,'2023-03-14 00:00:00',1,NULL,NULL),(6,1,'小李',2500.10,'2023-03-14 00:00:00',1,NULL,NULL),(7,0,'陈二狗',2500.90,'2023-03-14 00:00:00',3,NULL,NULL),(15,1,'郭成龙',1800.90,'2023-06-07 00:00:00',1,NULL,NULL),(16,1,'小王',3600.89,'2023-05-02 00:00:00',2,NULL,NULL),(17,0,'马成虫',1000.10,'2023-01-08 00:00:00',2,NULL,NULL),(18,1,'老狗',1100.90,'2023-04-07 00:00:00',3,NULL,NULL),(19,0,'臭弟弟',4500.99,'2023-03-14 00:00:00',1,NULL,NULL),(20,1,'小李',2500.10,'2023-03-14 00:00:00',1,NULL,NULL),(21,0,'陈二狗',2500.90,'2023-03-14 00:00:00',3,NULL,NULL),(22,0,'二狗',NULL,'2023-06-09 11:18:55',NULL,NULL,' CSDN'); /*!40000 ALTER TABLE `emp` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `emp_log` -- DROP TABLE IF EXISTS `emp_log`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `emp_log` ( `id` int NOT NULL, `name` char(10) NOT NULL DEFAULT '', `date` datetime NOT NULL, `signin` tinyint NOT NULL DEFAULT '0' COMMENT '登录次数', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `emp_log` -- LOCK TABLES `emp_log` WRITE; /*!40000 ALTER TABLE `emp_log` DISABLE KEYS */; INSERT INTO `emp_log` VALUES (1,'张三','2023-06-22 15:25:33',1),(2,'张三','2023-06-20 15:25:47',3),(3,'王五','2023-06-19 15:26:02',2),(4,'弟弟','2023-06-07 15:26:14',4),(5,'二狗','2023-06-11 15:26:40',4),(6,'二狗','2023-06-04 15:26:54',2); /*!40000 ALTER TABLE `emp_log` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `goods` -- DROP TABLE IF EXISTS `goods`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `goods` ( `id` int NOT NULL AUTO_INCREMENT, `category_id` int DEFAULT NULL, `category` varchar(15) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL, `name` varchar(50) CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci DEFAULT NULL, `price` decimal(10,2) DEFAULT NULL, `stock` int DEFAULT NULL, `upper_time` datetime DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `goods` -- LOCK TABLES `goods` WRITE; /*!40000 ALTER TABLE `goods` DISABLE KEYS */; INSERT INTO `goods` VALUES (1,1,'女装/女士精品','T恤',39.90,1000,'2020-11-10 00:00:00'),(2,1,'女装/女士精品','连衣裙',79.90,2500,'2020-11-10 00:00:00'),(3,1,'女装/女士精品','卫衣',89.90,1500,'2020-11-10 00:00:00'),(4,1,'女装/女士精品','牛仔裤',89.90,3500,'2020-11-10 00:00:00'),(5,1,'女装/女士精品','百褶裙',29.90,500,'2020-11-10 00:00:00'),(6,1,'女装/女士精品','呢绒外套',399.90,1200,'2020-11-10 00:00:00'),(7,2,'户外运动','自行车',399.90,1000,'2020-11-10 00:00:00'),(8,2,'户外运动','山地自行车',1399.90,2500,'2020-11-10 00:00:00'),(9,2,'户外运动','登山杖',59.90,1500,'2020-11-10 00:00:00'),(10,2,'户外运动','骑行装备',399.90,3500,'2020-11-10 00:00:00'),(11,2,'户外运动','运动外套',799.90,500,'2020-11-10 00:00:00'),(12,2,'户外运动','滑板',499.90,1200,'2020-11-10 00:00:00'),(15,1,'女装/女士精品1','连衣裙1',40.90,2500,'2020-11-10 00:00:00'); /*!40000 ALTER TABLE `goods` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `order_for_goods` -- DROP TABLE IF EXISTS `order_for_goods`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `order_for_goods` ( `order_id` int NOT NULL AUTO_INCREMENT, `user_id` int DEFAULT NULL, `money` decimal(10,2) DEFAULT NULL, `quantity` int DEFAULT NULL, `join_time` datetime DEFAULT NULL, PRIMARY KEY (`order_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=75 DEFAULT CHARSET=utf8mb3 ROW_FORMAT=DYNAMIC; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `order_for_goods` -- LOCK TABLES `order_for_goods` WRITE; /*!40000 ALTER TABLE `order_for_goods` DISABLE KEYS */; INSERT INTO `order_for_goods` VALUES (65,1001,1800.90,1,'2023-06-07 00:00:00'),(66,1001,3600.89,5,'2023-05-02 00:00:00'),(67,1001,1000.10,6,'2023-01-08 00:00:00'),(68,1002,1100.90,9,'2023-04-07 00:00:00'),(69,1002,4500.99,1,'2023-03-14 00:00:00'),(70,1003,2500.10,3,'2023-02-14 00:00:00'),(71,1002,2500.90,1,'2023-03-14 00:00:00'),(72,1003,2500.90,1,'2022-12-12 00:00:00'),(73,1003,2500.90,2,'2022-09-08 00:00:00'),(74,1003,6000.90,8,'2023-01-10 00:00:00'); /*!40000 ALTER TABLE `order_for_goods` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `person_table` -- DROP TABLE IF EXISTS `person_table`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `person_table` ( `first_name` char(20) COLLATE utf8mb4_unicode_ci NOT NULL, `last_name` char(20) COLLATE utf8mb4_unicode_ci NOT NULL, `sex` char(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`last_name`,`first_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `person_table` -- LOCK TABLES `person_table` WRITE; /*!40000 ALTER TABLE `person_table` DISABLE KEYS */; INSERT INTO `person_table` VALUES ('Thomas','Jay',NULL),('csdn','mr.xiao',NULL); /*!40000 ALTER TABLE `person_table` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `sequence` -- DROP TABLE IF EXISTS `sequence`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `sequence` ( `name` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, `current_value` int NOT NULL, `increment` int NOT NULL DEFAULT '1', PRIMARY KEY (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `sequence` -- LOCK TABLES `sequence` WRITE; /*!40000 ALTER TABLE `sequence` DISABLE KEYS */; INSERT INTO `sequence` VALUES ('seq_buz_engineering_task',1,1),('seq_buz_tender_info',2,1),('TestSeq',0,1); /*!40000 ALTER TABLE `sequence` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `test_transaction` -- DROP TABLE IF EXISTS `test_transaction`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `test_transaction` ( `id` int DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `test_transaction` -- LOCK TABLES `test_transaction` WRITE; /*!40000 ALTER TABLE `test_transaction` DISABLE KEYS */; INSERT INTO `test_transaction` VALUES (5),(6); /*!40000 ALTER TABLE `test_transaction` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `text_alter` -- DROP TABLE IF EXISTS `text_alter`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `text_alter` ( `ids` int DEFAULT NULL, `prices` int NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb3; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `text_alter` -- LOCK TABLES `text_alter` WRITE; /*!40000 ALTER TABLE `text_alter` DISABLE KEYS */; /*!40000 ALTER TABLE `text_alter` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `text_copy` -- DROP TABLE IF EXISTS `text_copy`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `text_copy` ( `text_id` int unsigned NOT NULL AUTO_INCREMENT, `text_title` varchar(100) NOT NULL, `text_author` varchar(40) NOT NULL, `submission_date` date DEFAULT NULL, PRIMARY KEY (`text_id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb3; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `text_copy` -- LOCK TABLES `text_copy` WRITE; /*!40000 ALTER TABLE `text_copy` DISABLE KEYS */; INSERT INTO `text_copy` VALUES (1,'安徒生童话','臭弟弟','2023-06-16'),(2,'小天鹅与丑小鸭','你才是臭弟弟','2023-06-16'),(3,'西游记','我不是臭弟弟','2023-06-16'),(5,'csdn','作者','2023-06-19'),(6,'测试','text','2023-06-19'); /*!40000 ALTER TABLE `text_copy` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `text_null` -- DROP TABLE IF EXISTS `text_null`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `text_null` ( `id` int NOT NULL, `text_title` char(10) DEFAULT NULL COMMENT '书名', `text_author` char(10) DEFAULT NULL COMMENT '作者', `price` double DEFAULT NULL COMMENT '价格', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `text_null` -- LOCK TABLES `text_null` WRITE; /*!40000 ALTER TABLE `text_null` DISABLE KEYS */; INSERT INTO `text_null` VALUES (1,'百科全书解决空值','火爆的后槽牙儿',6.66),(2,'这是一本书','小王',NULL),(3,'西游记','作者',NULL); /*!40000 ALTER TABLE `text_null` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `text_table` -- DROP TABLE IF EXISTS `text_table`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `text_table` ( `text_id` int unsigned NOT NULL AUTO_INCREMENT, `text_title` varchar(100) NOT NULL, `text_author` varchar(40) NOT NULL, `submission_date` date DEFAULT NULL, PRIMARY KEY (`text_id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb3; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `text_table` -- LOCK TABLES `text_table` WRITE; /*!40000 ALTER TABLE `text_table` DISABLE KEYS */; INSERT INTO `text_table` VALUES (1,'安徒生童话','臭弟弟','2023-06-16'),(2,'小天鹅与丑小鸭','你才是臭弟弟','2023-06-16'),(3,'西游记','我不是臭弟弟','2023-06-16'),(5,'csdn','作者','2023-06-19'),(6,'测试','text','2023-06-19'); /*!40000 ALTER TABLE `text_table` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `text_table2` -- DROP TABLE IF EXISTS `text_table2`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `text_table2` ( `text_id` int unsigned NOT NULL AUTO_INCREMENT, `text_title` varchar(100) NOT NULL, `text_author` varchar(40) NOT NULL, `submission_date` date DEFAULT NULL, PRIMARY KEY (`text_id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb3; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `text_table2` -- LOCK TABLES `text_table2` WRITE; /*!40000 ALTER TABLE `text_table2` DISABLE KEYS */; INSERT INTO `text_table2` VALUES (1,'安徒生童话','臭弟弟','2023-06-19'),(2,'小天鹅与丑小鸭','你才是臭弟弟','2023-06-19'),(3,'HPH调用','你才是臭弟弟','2023-06-19'),(4,'JAVA入门','我不是臭弟弟','2023-06-19'),(5,'水浒传','你才是臭弟弟','2023-06-19'),(6,'红楼梦','我不是臭弟弟','2023-06-19'),(7,'事实上d','d','2023-06-20'),(8,'鸟','a','2023-06-20'); /*!40000 ALTER TABLE `text_table2` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `users` -- DROP TABLE IF EXISTS `users`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `users` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `users` -- LOCK TABLES `users` WRITE; /*!40000 ALTER TABLE `users` DISABLE KEYS */; INSERT INTO `users` VALUES (1,'1',NULL),(2,'2',NULL); /*!40000 ALTER TABLE `users` 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 2023-06-23 10:25:27
总结:
如果你需要导出某个数据库的某张表的数据,可以使用以下命令:
mysqldump -uroot -proot 数据库名 表名 > D:\work\mysql\MySQL-Server-8.0\Uploads\product.sql
如果你需要导出整个数据库的数据,可以使用以下命令:
mysqldump -uroot -proot product > D:\work\mysql\MySQL-Server-8.0\Uploads\product.sql
如果需要备份所有数据库,可以使用以下命令:
mysqldump -u root -p --all-databases > D:\work\mysql\MySQL-Server-8.0\Uploads\database_dump.sql
mysqldump是MySQL提供的一个命令行工具,用于备份数据库和表。它有许多参数可以用来定制备份的各个方面。
以下是mysqldump的全部参数:
注意:这些参数可以在命令行中使用,也可以在配置文件中进行配置,以满足不同的备份需求
说明:
要将MySQL数据表和数据库复制到另一个主机上,可以遵循以下步骤
1、在源主机上备份数据:使用以下命令备份整个数据库或特定的数据表:
mysqldump -u username -p dbname > backupfile.sql
其中,username是数据库用户名,dbname是要备份的数据库名称,backupfile.sql是备份文件的名称。
也可以使用--all-databases选项备份所有数据库:
mysqldump -u username -p --all-databases > backupfile.sql
2、将备份文件传输到目标主机:也可以使用任何文件传输工具,将备份文件传输到目标主机上。
3、在目标主机上恢复数据:使用以下命令将备份文件恢复到目标主机上:
mysql -u username -p dbname < backupfile.sql
其中,username是数据库用户名,dbname是要恢复数据的数据库名称,backupfile.sql是备份文件的名称。如果备份文件中包含多个数据库,您需要逐个恢复它们。
也可以使用以下命令将导出的数据直接导入到远程的服务器上,但请确保两台服务器是相通的,是可以相互访问的:
mysqldump -u root -p database_name \ | mysql -h other-host.com database_name
mysqldump是一个用于备份MySQL数据库的工具,而mysql是用于恢复或导入数据到MySQL数据库的工具。在这个命令中,我们将使用mysqldump备份数据库,然后将备份文件通过管道符号|发送给mysql命令,以便在另一个主机上恢复数据库。
具体来命令的参数解释如下:
综上所述,这个命令将在源主机上备份名为database_name的数据库,然后将备份文件通过管道发送给目标主机上的mysql命令,以便在该主机上恢复相同的数据库。
mysql -u用户名 -p密码 数据库名称 < 要导入的数据库数据(product.sql)
mysql -u xiaojian -p xj < D:/work/mysql/MySQL-Server-8.0/Uploads/product.sql
以上命令会将备份的整个数据库 product.sql 导入。
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | product | | sys | | xj | +--------------------+ 6 rows in set (0.00 sec) mysql> use xj; Database changed mysql> show tables; Empty set (0.00 sec)
source D:/work/mysql/MySQL-Server-8.0/Uploads/product.sql
LOAD DATA LOCAL INFILE 'D:/work/mysql/MySQL-Server-8.0/Uploads/goods.txt' INTO TABLE goods FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
"1","1","女装/女士精品","T恤","39.90","1000","2020-11-10 00:00:00" "3","1","女装/女士精品","卫衣","89.90","1500","2020-11-10 00:00:00" "5","1","女装/女士精品","百褶裙","29.90","500","2020-11-10 00:00:00" "7","2","户外运动","自行车","399.90","1000","2020-11-10 00:00:00" "9","2","户外运动","登山杖","59.90","1500","2020-11-10 00:00:00" "11","2","户外运动","运动外套","799.90","500","2020-11-10 00:00:00" "15","1","女装/女士精品1","连衣裙1","40.90","2500","0000-00-00 00:00:00"
错误:
ERROR 3948 (42000): Loading local data is disabled; this must be enabled on both the client and server sides
ERROR 3948(42000):加载本地数据已被禁用;必须在客户端和服务器端同时启用此功能。
解决:
SET GLOBAL local_infile = ON;
mysqlimport [options] db_name file_name
db_name是要导入数据的数据库名称,file_name是包含数据的文件名。options是可选的参数,用于指定一些导入选项。
mysqlimport -u xiaojian -p --local --fields-terminated-by="," --lines-terminated-by="\n" xj D:/work/mysql/MySQL-Server-8.0/Uploads/goods.txt
1,1,女装/女士精品,T恤,39.90,1000,2020-11-10 00:00:00 3,1,女装/女士精品,卫衣,89.90,1500,2020-11-10 00:00:00 5,1,女装/女士精品,百褶裙,29.90,500,2020-11-10 00:00:00 7,2,户外运动,自行车,399.90,1000,2020-11-10 00:00:00 9,2,户外运动,登山杖,59.90,1500,2020-11-10 00:00:00 11,2,户外运动,运动外套,799.90,500,2020-11-10 00:00:00 15,1,女装/女士精品1,连衣裙1,40.90,2500,0000-00-00 00:00:00