I am trying to import csv file in sql, please find the below code:
LOAD DATA INFILE 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\Assignment\Auto.csv' INTO TABLE assignments.Auto
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
Its giving me the error:
Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement 0.016 sec
I then tried with keyword LOCAL(as mentioned in some of the solutions) and it gave me error:
Error Code: 1148. The used command is not allowed with this MySQL version 0.000 sec
As i found many solution to this, so i checked the value of this variable manually in session with command:
SHOW VARIABLES LIKE "secure_file_priv";
the output for this is :
secure_file_priv C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\
**This is the same location i am using in the load data path above.
I checked the C:\ProgramData\MySQL\MySQL Server 8.0\my.ini file and it has the below details:
Secure File Priv.
secure-file-priv="C:/ProgramData/MySQL/MySQL Server 8.0/Uploads"
Can someone please tell me what is the issue here and how can i resolve this.
SHOW VARIABLES LIKE "secure_file_priv";
This command will show the path, for example
C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/
Put your file in the same path in uploads
Also check the backslash in both the path. If it is different, change in your code.
I did the two steps and it got resolved in MySQL 8.
If you want to use LOAD DATA LOCAL INFILE … you need to set MySQL Server global variable local_infile to true.
SET GLOBAL local_infile = true;
You need also pass --local-infle option to mysql command-line client
mysql --local-infile -hlocalhost -uroot -p
About secure file priv error - does your Auto.csv file is on the same machine where MySQL Server is running?
Related
I am trying to export the result of a query to a .csv using:
SELECT 'BAT_POSITION_IN_SERIES', 'NUMBER_OF_PITCHES', 'PCT_BALLS', 'PCT_CALLED_STRIKE', 'STRIKEOUTS_PER_PITCH', 'WALKS_PER_PITCH', 'HITS_PER_PITCH', 'RUNS_PER_PITCH'
UNION
SELECT *
FROM per_pitch_summary
INTO OUTFILE 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\per_pitch_summary.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ','
ESCAPED BY '"'
LINES TERMINATED BY '\n'
;
The above executes, however I would like to change the destination folder. When replacing the file path with my desired destination, I receive the message: Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement.
Most of the troubleshooting solutions I have seen involve commenting out a line in the my.ini file, but I do not have this file in my version of MySQL. Instead, the Path to Executable is C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe.
Any suggestions would be appreciated!
I'm pretty sure you have a default config file somewhere. But you can specify your own config when launching sql. The following will load the default options but override the ones set in your config, this is the safer option.
mysql --defaults-extra-file=/home/my.cnf
Then in your my.cnf:
[client]
user="root"
password="mypassword"
host="localhost"
database="placeToStart"
[mysqld]
secure-file-priv = ""
By setting it to an empty path you'll disable the variable. To confirm this, look at the global variable value:
SHOW VARIABLES LIKE "secure_file_priv";
I am using Amazon RDS MySql for data storage. I am able to load data by specifying the column values, however, when I try to load the data from my local machine to MySql, it fails with
Error: 2068.
LOAD DATA LOCAL INFILE '/Users/priya/Documents/Atlas/data/The_Atlas_0820.csv' INTO TABLE schedule
FIELDS TERMINATED by ','
ENCLOSED by '"'
LINES TERMINATED by '\n' IGNORE 1 LINES (Carrier1, FlightNo1, DupCar1,DupCar2, DupCar3,
DupCar4,DupCar5, DupCar6,DupCar7, DupCar8,DepAirport, ArrAirport);
Error Code: 2068. LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.
I also checked SHOW GLOBAL VARIABLES LIKE 'local_infile'; and it is set to "ON".
If i use Terminal to open a connection with MySql and execute the command there, it works ok.
$ ./mysql -h <hostname> -P 3306 --local_infile=1 -u <username> -p
But how to make it work in MySql Workbench.
It seems that there is a bug in MySQL Workbench for LOAD DATA LOCAL INFILE, check if the workaround in this link works for you:
MySQL Workbench 8.0 restricts usage of LOAD DATA LOCAL INFILE
or there is another solution that seems to work here:
Workbench 8.0.12 no longer allows LOAD DATA LOCAL INFILE
Adding what worked for me here, I was able to find a working solution through the links #Zacca provided, but I had to dig through solutions that didn't work, and I thought I'd repost here in case those links die at some point.
First, if I ran SHOW GLOBAL VARIABLES LIKE 'local_infile'; I can see that INFILE is enabled. However, I was still getting "ERROR CODE 2068"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile | ON |
+---------------+-------+
I was able to resolve this by editing the connection string in MySQL workbench by navigating to Home, then:
Edit Connection > Advanced > Copy OPT_LOCAL_INFILE=1 into Others box on a new line
Then restart the connection. After that, LOAD LOCAL INFILE runs without error.
Also, as a side note, before I found this solution, I found I could run the LOAD LOCAL INFILE command from R without issue, which helped me isolate the client as the issue, as opposed to a security/permissions related problem.
The solution that worked for me is from the workarounds shared in the link:
Workbench 8.0.12 no longer allows LOAD DATA LOCAL INFILE as shared by #Zacca.
Steps for Mac:
Create a my.cnf file with the following statements at the path: /etc.
I created my.cnf file using root user.
[client]
port = 3306
[mysqld]
port = 3306
secure_file_priv=''
local-infile = 1
Set the my.cnf file as the default configuration file in MySql Workbench.
Click the Wrench icon next to Instance.
Under configuration file, enter the path to my.cnf file: /etc/my.cnf.
Restart the MySQL server workbench.
Try the following statements in MySQL Workbench:
SHOW VARIABLES LIKE "local_infile"; //Should be ON
SHOW VARIABLES LIKE "secure_file_priv"; //Should have no values (not NULL but blank)
Load the data.
LOAD DATA LOCAL INFILE '<path>/file.csv' INTO TABLE <tablename>
FIELDS TERMINATED by ','
ENCLOSED by '"'
LINES TERMINATED by '\n' IGNORE 1 LINES;
Using LOCAL keyword, loading is successful.
However, without LOCAL keyword, I get access error.
Error Code: 1045. Access denied for user 'admin'#'%' (using password: YES)
After beating my head around this for an hour, I switched back to MySQL Workbench 6.3.
Bliss.
I tried to load csv file into table using
load data infile 'C:\\a.csv' into table errorClick columns terminated by ',' lines terminated by ',\r\n' ignore 1 lines;
I am using sql version 8.0 It gave error:
Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement 0.000 sec
I ran this statement:
SHOW VARIABLES LIKE "secure_file_priv";
The result is
secure_file_priv C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\
Then i tried
Set secure_file_priv="";
It gave result
Error Code: 1238. Variable 'secure_file_priv' is a read only variable 0.047 sec
Then I tried to find my.ini in my windows 10 machine at location
C:\ProgramData\MySQL\MySQL Server 8.0\
I did not find any my.ini file; Please help me resolve this error;
you can only change it in my.inio file and then restart he server
You find my.ini in a hidden folder
C:\ProgramData\MySQL\MySQL Server 8.0
There you find under the section
[mysqld]
secure-file-priv = ""
After the edit, you need to restart the server, so that the changes will take effect.
I need to import csv file into mysql:
first I tried:
LOAD DATA LOCAL INFILE 'C:/Users/MJ/Desktop/nysgi1.csv' INTO TABLE gift FIELDS TERMINATED BY ',' (giftid,date,udate,vamount);
errors:
ERROR 1148 (42000): The used command is not allowed with this MySQL version
And I then followed some instruction to successfully set local_infile ON ,and then I restarted mysql by using service mysql restart.However it not works,still the same error.
Then I tried to use directly:
LOAD DATA INFILE 'C:/Users/MJ/Desktop/nysgi1.csv' INTO TABLE gift FIELDS TERMINATED BY ',' (giftid,date,udate,vamount);
errors:
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
So I use:
SELECT ##global.secure_file_priv;
and find out the directory is:
/var/lib/mysql-files/
My question is how to import the csv file to /var/lib/mysql-files/ using command line.
Thanks!
Seems you are having exactly the same problem i have,
please use scp command to place your csv file into linux directory.
I'm not quite sure a similar question to this was closed by I'm trying to execute the following MySQL program.
mysql -e "load data local infile \
'/tmp/ept_inventory_wasp_export_04292013.csv' into \
table wasp_ept_inv fields terminated by ',' \
lines terminated by '\n' ;"
at the bash command line and get this error
ERROR 1148 (42000) at line 1: The used command is not allowed with this MySQL version
How can I work around this problem?
I am actually running this command from a Python program, but pulled the command out to try fiddling with it at the bash command line.
I've seen how I can modify my.cnf (local-infile), but I do not want that global a change if I can avoid it.
Here's the MySQL version.
mysql Ver 14.14 Distrib 5.5.31, for debian-linux-gnu (i686) using readline 6.2
The workaround for this is to modify the command line mysql -e to pass in the --local-infile=1 argument like this:
mysql --local-infile=1 -u username -p `
Then run the LOAD DATA LOCAL INFILE command again.
As documented under Security Issues with LOAD DATA LOCAL:
To deal with these problems, we changed how LOAD DATA LOCAL is handled as of MySQL 3.23.49 and MySQL 4.0.2 (4.0.13 on Windows):
By default, all MySQL clients and libraries in binary distributions are compiled with the --enable-local-infile option, to be compatible with MySQL 3.23.48 and before.
If you build MySQL from source but do not invoke configure with the --enable-local-infile option, LOAD DATA LOCAL cannot be used by any client unless it is written explicitly to invoke mysql_options(... MYSQL_OPT_LOCAL_INFILE, 0). See Section 20.6.6.49, “mysql_options()”.
You can disable all LOAD DATA LOCAL statements from the server side by starting mysqld with the --local-infile=0 option.
For the mysql command-line client, enable LOAD DATA LOCAL by specifying the --local-infile[=1] option, or disable it with the --local-infile=0 option. For mysqlimport, local data file loading is off by default; enable it with the --local or -L option. In any case, successful use of a local load operation requires that the server permits it.
If you use LOAD DATA LOCAL in Perl scripts or other programs that read the [client] group from option files, you can add the local-infile=1 option to that group. However, to keep this from causing problems for programs that do not understand local-infile, specify it using the loose- prefix:
[client]
loose-local-infile=1
If LOAD DATA LOCAL is disabled, either in the server or the client, a client that attempts to issue such a statement receives the following error message:
ERROR 1148: The used command is not allowed with this MySQL version
local-infile needs to enabled on both the server and the client. You can accomplish this by adding local-infile = 1 to the appropriate section in each end's my.cnf (Unix) or my.ini (Windows) file. For example, on the client:
[client]
local-infile = 1
You can also enable this at runtime on the server by setting the system variable local_infile:
SET GLOBAL local_infile=1;
However, you still need to enable it on the client. You can do this at runtime by adding a command-line parameter to the mysql command:
mysql --local-infile=1 ...
If you're using Amazon Web Services RDS, you can configure the server setting by editing or creating a Parameter Group. Look for the local_infile parameter. You may need to restart your server after applying the changes.
My guess is that your MySQL server does not have LOAD DATA LOCAL enabled. See this section of MySQL documentation:
If LOAD DATA LOCAL is disabled, either in the server or the client, a client that attempts to issue such a statement receives the following error message:
ERROR 1148: The used command is not allowed with this MySQL version
Here is link to the page I got this from:
http://dev.mysql.com/doc/refman/5.5/en/load-data-local.html