Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL LOAD DATA LOCAL INFILE on WINDOWS not WORKING

I am using a windows with MySQL 8.0 installed.

I have checked the solutions already posted on stackoverflow but these had no immediate results.

I have set the global variable with SET GLOBAL local_infile = 1; This options seems to be enable now but MySQL keeps throwing the following error:

Error Code:

  1. The used command is not allowed with this MySQL version

Can anyone help me out to remedy this behavior ?

like image 732
veadeveloper Avatar asked Jun 05 '18 11:06

veadeveloper


People also ask

How do I enable local data Infile in MySQL?

For the mysql client, local data loading capability is determined by the default compiled into the MySQL client library. To disable or enable it explicitly, use the --local-infile=0 or --local-infile[=1] option. For the mysqlimport client, local data loading is not used by default.

How do I insert selected columns from a CSV file to a MySQL database using load data infile?

The code is like this: LOAD DATA INFILE '/path/filename. csv' INTO TABLE table_name FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (column_name3, column_name5); Here you go with adding data to only two columns(you can choose them with the name of the column) to the table.

What is load data infile?

The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed. If the LOCAL keyword is specified, the file is read from the client host. If LOCAL is not specified, the file must be located on the server.

How to enable load data local file in MySQL?

Enabling LOAD DATA LOCAL INFILE in mysql. LOAD DATA LOCAL INFILE is not enabled by default. Normally, it should be enabled by placing local-infile=1 in my.cnf. But it does not work for all installations.

Why is local infile disabled by default in MySQL?

LOAD DATA LOCAL INFILE is disabled by default because it poses a security risk. A malicious server or proxy could send a fake “local infile request” packet to the client and read any file that the client has permission to open. For more information, see the MySQL documentation.

How do I load a local file in SQL Server?

To allow LOAD DATA LOCAL INFILE to succeed, you must set AllowLoadLocalInfile=true in the client’s connection string. If you use MySqlBulkLoader and set Local=true, then everything should work by default. If you are manually creating a LOAD DATA LOCAL INFILE statement, you must be connected to a trusted server.

How to use load data local infile?

To use LOAD DATA LOCAL INFILE, set AllowLoadLocalInfile=true in the connection string. Use SourceStream or SslMode >= VerifyCA for LOAD DATA LOCAL INFILE.


2 Answers

Try:

File: 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 Command-Line:

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)
like image 149
wchiquito Avatar answered Oct 15 '22 21:10

wchiquito


I have the same problem after a search, I solve the problem by removing the local keyword from the query.

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

The query should return: ON

The query should look like this:

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;

You should be noticed that I removed the local keyword from here and now it works fine.

like image 29
Abdullah Avatar answered Oct 15 '22 21:10

Abdullah