WINDOWS上的MySQL LOAD DATA LOCAL INFILE无效

时间:2018-06-05 11:53:22

标签: mysql file-io load local

我正在使用安装了MySQL 8.0的Windows。

我已经检查过已在stackoverflow上发布的解决方案,但这些解决方案没有立竿见影的结果。

我已使用SET GLOBAL local_infile = 1;设置全局变量 这个选项现在似乎已启用,但MySQL不断抛出以下错误:

错误代码:

  
      
  1. 此MySQL版本不允许使用的命令
  2.   

有人可以帮我解决这个问题吗?

5 个答案:

答案 0 :(得分:2)

尝试:

档案:Z:\Path\To\MySQL\Files\my_file.csv

1,"a string"
2,"a string containing a , comma"
3,"a string containing a \" quote"
4,"a string containing a \", quote and comma"

MySQL命令行:

Z:\>mysql
Enter password: **************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> DROP TABLE IF EXISTS `_`.`my_table`;
Query OK, 0 rows affected, 1 warning (0.03 sec)

mysql> CREATE TABLE IF NOT EXISTS `_`.`my_table` (
    ->   `col0` INT NOT NULL PRIMARY KEY,
    ->   `col1` VARCHAR(50) NOT NULL
    -> );
Query OK, 0 rows affected (0.45 sec)

mysql> SHOW VARIABLES WHERE `variable_name` = 'secure_file_priv';
+------------------+-------------------------+
| Variable_name    | Value                   |
+------------------+-------------------------+
| secure_file_priv | Z:\Path\To\MySQL\Files\ |
+------------------+-------------------------+
1 row in set (0.00 sec)

mysql> LOAD DATA LOCAL INFILE 'Z:\\Path\\To\\MySQL\\Files\\my_file.csv'
    ->   INTO TABLE `_`.`my_table`
    ->   FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    ->   LINES TERMINATED BY '\r\n';
ERROR 1148 (42000): The used command is not allowed with this MySQL version

mysql> SELECT @@GLOBAL.`local_infile`;
+-------------------------+
| @@GLOBAL.`local_infile` |
+-------------------------+
|                       0 |
+-------------------------+
1 row in set (0.00 sec)

mysql> SET @@GLOBAL.`local_infile` := 1;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @@GLOBAL.`local_infile`;
+-------------------------+
| @@GLOBAL.`local_infile` |
+-------------------------+
|                       1 |
+-------------------------+
1 row in set (0.00 sec)

mysql> LOAD DATA LOCAL INFILE 'Z:\\Path\\To\\MySQL\\Files\\my_file.csv'
    ->   INTO TABLE `_`.`my_table`
    ->   FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    ->   LINES TERMINATED BY '\r\n';
ERROR 1148 (42000): The used command is not allowed with this MySQL version

mysql> SELECT `col0`, `col1`
    -> FROM `_`.`my_table`;
Empty set (0.00 sec)

mysql> exit
Bye

Z:\>mysql --local-infile=1
Enter password: **************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT @@GLOBAL.`local_infile`;
+-------------------------+
| @@GLOBAL.`local_infile` |
+-------------------------+
|                       1 |
+-------------------------+
1 row in set (0.00 sec)

mysql> LOAD DATA LOCAL INFILE 'Z:\\Path\\To\\MySQL\\Files\\my_file.csv'
    ->   INTO TABLE `_`.`my_table`
    ->   FIELDS TERMINATED BY ',' ENCLOSED BY '"'
    ->   LINES TERMINATED BY '\r\n';
Query OK, 4 rows affected (0.19 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 0

mysql> SELECT `col0`, `col1`
    -> FROM `_`.`my_table`;
+------+------------------------------------------+
| col0 | col1                                     |
+------+------------------------------------------+
|    1 | a string                                 |
|    2 | a string containing a , comma            |
|    3 | a string containing a " quote            |
|    4 | a string containing a ", quote and comma |
+------+------------------------------------------+
4 rows in set (0.00 sec)

答案 1 :(得分:0)

截至2019年1月,这是MySQL Workbench 8.0.12的一个未解决的错误。请参阅:https://bugs.mysql.com/bug.php?id=91891
通过CLI执行相同的命令应该可以,但是,前提是“ local_infile”全局变量已设置为1:

  

SET @@ GLOBAL.local_infile = 1;

答案 2 :(得分:0)

搜索后我遇到了同样的问题,我通过从查询中删除本地关键字来解决了这个问题。

SET GLOBAL local_infile = 1;
SHOW VARIABLES LIKE 'local_infile';

查询应返回:ON

查询应如下所示:

load data infile 'C:\\CA_DRU_proj_2010-2060.csv'
into table pop_proj
fields terminated by ','
enclosed by '"'
lines terminated by '\n'
ignore 1 lines;

您应该注意到,我从此处删除了本地关键字,现在可以正常使用了。

答案 3 :(得分:0)

我一直在处理类似的问题,在Windows上使用MySQL 8.0.19。 我通过不使用LOCAL关键字AND来解决 使用Windows服务管理器(在资源管理器任务栏上,键入服务),右键单击MySQL80->选择“属性”,然后在“登录”选项卡上,将“登录身份:”值更改为“本地系统帐户”。

答案 4 :(得分:0)

Windows 10用户:值得重复一遍,确认Windows使用哪个帐户运行MySQL服务有多么重要。

此问题在Windows 10的MySQL 8.0.21中仍然存在。使用LOAD DATA INFILE(并且不是 LOAD DATA INFILE LOCAL),即使您正在运行客户端,也需要进行其他文件传输和服务器在同一桌面上),我开始收到此错误:

not found (OS errno 13 - Permission denied)

由于某种原因,MySQL决定选择“网络服务”,可能是因为我将安装从默认的C:驱动器移到了D:上更快的物理驱动器上。

否:
No. Wrong. Gives  not found (OS errno 13 - Permission denied error

原始安装已设置为Local(本地),我选择了“ Local System Account(本地系统帐户)”单选按钮,然后为我解决了该问题。

是:
Original and correct configuration

mySQL documentation鼓励在Windows上将MySQL作为Windows服务运行,但没有建议将哪个帐户指定为该服务的登录。

和其他许多人一样,我经历了所有选项(local_infile等),但是直到看到the answer from CesarinG才开始检查服务登录,我现在欠他们啤酒或咖啡。