MySqL loading local data is disable error , without using terminal commands - mysql

I got loading local data is disable error. I found many way that could solve by
mysql> SET GLOBAL local_infile=1;
mysql --local-infile=1 -u root -p1
But I am wondering whether I could not use terminal command to fix error.(Just type in MySql query)
I found some way to add into my.cnf
[mysql]
local-infile
[mysqld]
local-infile
But it still doesn't work.
This is my sample code that try to LOAD DATA to table, and get the error.
LOAD DATA LOCAL INFILE 'C:/Users/TEMP/Desktop/tempname.csv' INTO TABLE tempname
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n'
(Name, ID, Birthday)
What do I wrong in modifying my.cnf.
I need a way that I won't get the error by default.
I modified my.cnf like that, do I wrong?

You can add or change local_infile in the sections
[mysql]
local_infile=ON
and
[mysqld]
local_infile=ON
Buu you have to use ON not 1 and you need of course to restart the server

Related

Change default outfile location to avoid Error Code 1290 - my.ini not available

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";

Error Code: 2068 while loading csv file into MySql

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.

MySQL LOAD DATA LOCAL INFILE difference in Clients

I am trying to run this query
LOAD DATA LOCAL INFILE "C:/myfile.txt"
IGNORE INTO TABLE mydb.mytable
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
If I run it in MySQL Workbench (v8.0.16) I get an error
Error Code: 1148. The used command is not allowed with this MySQL version
However if I run it in SQLYog (Professional v12.09) it works fine.
I am running both bits of code using the same computer, with the same user, on the same database running on the same server.
That server has local_infile = 1
As far as I can tell the only difference in these queries is the SQL Client being used.
My assumption is that it is the connection string / settings that is different between the two as a default, however I can't find any documentation on this.
Why would this be and how can I fix it?
Thanks
In linux you have to start mysql client in command line as.
mysql -u root -ppassword --local-infile=1
you should also set local_infile = 1 in configuration file

Error 1148 (42000) using LOAD DATA LOCAL INFILE in MySQL

I'm trying to load data into a MySQL table using LOAD DATA LOCAL INFILE using:
LOAD DATA LOCAL INFILE 'f.csv' INTO TABLE tableName
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\\r\\n'
IGNORE 1 LINES
(C1, C2, C3, C4)
SET cmj = REPLACE(#cmj,',','.');
OS : Ubuntu 14.04
MySQL : 5.7
In file my.cnf I have this config:
[mysqld]
local-infile=1
[mysql]
local-infile=1
[client]
loose-local-infile=1
local-infile=1
I'm getting the following error:
Message: SQLSTATE[42000]: Syntax error or access violation: 1148 The
used command is not allowed with this MySQL version, query was: LOAD
DATA LOCAL INFILE 'f.csv' INTO TABLE tableName FIELDS TERMINATED BY
';' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (C1, C2, C3, C4)
I think the problem is related to new line in MySQL
Thanks for your help
EDIT
In my file.csv I have two lines (header + values). If I update my request "IGNORE 1 LINES" to "IGNORE 0 LINES" the first line (header) is inserted.
All lines are considered as one line!!!!!
use --local-infile while connecting database.
mysql -hhostname -uusername -p --local-infile test -A
Hope this helps.
According to the MySQL reference manual v5.7, the error you received can result from LOCAL file capability being disabled either on the server or client side.
Running mysql with the --local-infile option will enable LOCAL capability on the client side, but this won't help if it's disabled on the server, where it's controlled by the system variable local_infile. The easiest way to fix this is to reset the system variable during runtime with the SET command:
SET GLOBAL local_infile = 1;
Note: In later versions of MySQL you can make the variable change persist over server restarts with the command
SET PERSIST local_infile = 1;

Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

I encountered such a problem: Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
when I tried to execute MySQL statement (Windows):
SELECT *
FROM xxxx
WHERE XXX
INTO OUTFILE 'report.csv'
FIELDS TERMINATED BY '#'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
When I execute it without:
INTO OUTFILE 'report.csv'
FIELDS TERMINATED BY '#'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
Then it works. Also, the same statement with INTO OUTFILE xxx actually worked before I reinstalled the MySQL server.
Anybody has ideas how to deal with this error?
A quick answer, that doesn't require you to edit any configuration files (and works on other operating systems as well as Windows), is to just find the directory that you are allowed to save to using:
mysql> SHOW VARIABLES LIKE "secure_file_priv";
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.06 sec)
And then make sure you use that directory in your SELECT statement's INTO OUTFILE clause:
SELECT *
FROM xxxx
WHERE XXX
INTO OUTFILE '/var/lib/mysql-files/report.csv'
FIELDS TERMINATED BY '#'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
Original answer
I've had the same problem since upgrading from MySQL 5.6.25 to 5.6.26.
In my case (on Windows), looking at the MySQL56 Windows service shows me that the options/settings file that is being used when the service starts is C:\ProgramData\MySQL\MySQL Server 5.6\my.ini
On linux the two most common locations are /etc/my.cnf or /etc/mysql/my.cnf.
Opening this file I can see that the secure-file-priv option has been added under the [mysqld] group in this new version of MySQL Server with a default value:
secure-file-priv="C:/ProgramData/MySQL/MySQL Server 5.6/Uploads"
You could comment this (if you're in a non-production environment), or experiment with changing the setting (recently I had to set secure-file-priv = "" in order to disable the default). Don't forget to restart the service after making changes.
Alternatively, you could try saving your output into the permitted folder (the location may vary depending on your installation):
SELECT *
FROM xxxx
WHERE XXX
INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 5.6/Uploads/report.csv'
FIELDS TERMINATED BY '#'
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
It's more common to have comma seperate values using FIELDS TERMINATED BY ','. See below for an example (also showing a Linux path):
SELECT *
FROM table
INTO OUTFILE '/var/lib/mysql-files/report.csv'
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
ESCAPED BY ''
LINES TERMINATED BY '\n';
If you changed my.ini and restarted mysql and you still get this error please check your file path and replace "\" to "/".
I solved my proplem after replacing.
I had to set
C:\ProgramData\MySQL\MySQL Server 8.0/my.ini secure-file-priv=""
When I commented line with secure-file-priv, secure-file-priv was null and I couldn't download data.
Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
TL;DR
Specify the secure-file-priv folder with double back slash
and also explicitly specify the output file with double back slashes
and the OPs original issue is solved.
The detail
The longer answer is as follows -
On Windows 11 64-bit operating system, x64-based processor, build 22621.963, Version 22H2 with MySQL installed using the web installer .MSI file as a developer configuration.
SELECT VERSION(); gives '8.0.31'
Help > About Workbench gives Version 8.0.31 build 2235049 CE 64 bits
The defaults file on Windows 11 is called "my.ini" and is in C:\ProgramData\MySQL\MySQL Server 8.0
C:\ProgramData is a hidden folder.
The default specification in my.ini is secure-file-priv="C:/ProgramData/MySQL/MySQL Server 8.0/Uploads"
execute SHOW VARIABLES LIKE "secure_file_priv";
copy and paste the row gives 'secure_file_priv', 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\'
which renders as 'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\' in Workbench
Note the backslash, forward slash and double back slash syntax of these folder specifications
execute the OP's code
SELECT * FROM shippers INTO OUTFILE 'report.csv'
gives error 1290
execute the OP's code
SELECT * FROM shippers INTO OUTFILE 'report.csv'
FIELDS TERMINATED BY '#' ENCLOSED BY '"' LINES TERMINATED BY '\n';
gives error 1290
next change my.ini to secure-file-priv="" and restart MySQL using Open Services > Stop Service MySQL80 > then Start Service MySQL80 then close and reopen Workbench.
secure-file-priv is now switched off
SHOW VARIABLES LIKE "secure_file_priv";
Variable_name, Value
'secure_file_priv', ''
execute the OP's code
SELECT * FROM shippers INTO OUTFILE 'report.csv'
The file report.csv is generated in C:\ProgramData\MySQL\MySQL Server 8.0\Data\northwind
northwind is the database name
The output file is tab separated, Excel does not recognise this as a .CSV file and renders it as a single column
"1 Speedy Express (503) 555-9831"
"2 United Package (503) 555-3199"
"3 Federal Shipping (503) 555-9931"
"1 Speedy Express (503) 555-9831"
"2 United Package (503) 555-3199"
"3 Federal Shipping (503) 555-9931"
execute the OP's code
SELECT * FROM shippers INTO OUTFILE 'report.csv'
FIELDS TERMINATED BY '#' ENCLOSED BY '"' LINES TERMINATED BY '\n';
file report.csv is generated in C:\ProgramData\MySQL\MySQL Server 8.0\Data\northwind
The output file is as specified in the SQL code with text fields enclosed by quotes and separated by # -
1#"Speedy Express"#"(503) 555-9831"
2#"United Package"#"(503) 555-3199"
3#"Federal Shipping"#"(503) 555-9931"
1#"Speedy Express"#"(503) 555-9831"
2#"United Package"#"(503) 555-3199"
3#"Federal Shipping"#"(503) 555-9931"
Next set secure_file_priv="C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\" in my.ini and restart MySQL and Workbench
execute the OP's code
SELECT * FROM shippers INTO OUTFILE 'report.csv'
Error 1290 because "C:\ProgramData\MySQL\MySQL Server 8.0\Data\northwind" is not the secure file output folder.
execute the following code
SELECT * FROM shippers INTO OUTFILE "C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\report.csv"
file report.csv is generated in "C:\ProgramData\MySQL\MySQL Server 8.0\Uploads"
The code above exports data without the heading columns which is weird.
Here's how to do it. You have to merge the two files later though using text a editor.
SELECT column_name FROM information_schema.columns WHERE table_schema = 'my_app_db' AND table_name = 'customers' INTO OUTFILE 'C:/ProgramData/MySQL/MySQL Server 5.6/Uploads/customers_heading_cols.csv' FIELDS TERMINATED BY '' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY ',';