How to solve MySql "Cannot execute this command" error? - mysql

When I try to run this command
select name, address, age into outfile 'user.csv' FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' from StudentTable;"
I got an error from MySql:
"ERROR 1290 (HY000): The MySQL server is running with the
--secure-file-priv option so it cannot execute this statement"

Use
SELECT ##secure_file_priv;
To see what Folder is secure.
If it returns f.e
C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\
Use
select name, address, age into outfile 'C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\user.csv'
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
from StudentTable;
the Backslashes must be doubled
To disable change in he my.ini file
[mysqld]
secure-file-priv = ""
But that is insecure, because you can save anywhere you have write rights, bit alsoo there mus specify the folder where you want to write it for example
c:\\temp\\user.csv

Related

To export query results from MySQL to CSV using SQL in macOS 12.6

I am trying to export query results from MySQL to CSV using SQL by the following query.
SELECT *
FROM Book
INTO OUTFILE '/Users/<MY_NAME>/Desktop/folder1/folder2/book.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
After executing above SQL query I am getting a following error.
Error Code: 1290. The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
I tried by editing .my.cnf file to secure_file_priv='', But still I am getting same error.
I ran the SHOW VARIABLES LIKE 'secure_file_priv'; query. But, I am getting:
# Variable_name, Value
'secure_file_priv', 'NULL'

How to export mysql table from the terminal

I'm trying to bypass the -secure-file-priv option that MySQL has enabled to export a table into a .csv file.
I've been running SELECT * FROM final_table INTO OUTFILE 'test.csv' FIELDS ENCLOSED BY '"' TERMINATED BY ';' ESCAPED BY '"' LINES TERMINATED BY '\r\n'; into the terminal, but it has been returning The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
Previously, I bypassed this error when importing files by running LOAD DATA LOCAL INFILE instead of LOAD DATA INFILE which had the same error and was wondering if there was a way to do it with exporting as well.
Thanks!
Answering my own question since I found a solution that might help out other people.
mysql -u root -p [database] -e '[SELECT * FROM TABLE_NAME]' > data_export.csv
got me exactly what I needed without disabling secure-file-priv!

Exporting sql data from mysql server into csv file in local mac computer Help,

when I attempt to take the data I have from my mysql server and generate a csv file with this command:
select * from table into outfile '/Users/username/Desktop/testfile.csv' fields terminated by ',' enclosed by '"' lines terminated by '/n';
I end up with the error:
ERROR 1 (HY000): Can't create/write to file '/Users/username/Desktop/testfile.csv' (Errcode: 13 - Permission denied)
Another weird problem is I can't seem to stop the server from running through the preference pane, maybe they are related. Anyways, thanks for the help!
Try this:
SELECT * FROM table INTO OUTFILE '/tmp/myfile.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"';
Then in you terminal, cd to the tmp directory, do ls to see if the file is there and when you are in that tmp directory, just do open myfile.csv

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 ',';

MySQL Loading Data Error

LOAD DATA
LOCAL INFILE "file.txt"
REPLACE INTO TABLE file
FIELDS TERMINATED BY '|'
(attribute1, attribute2)
LOAD DATA
LOCAL INFILE "file2.txt"
REPLACE INTO TABLE file2
FIELDS TERMINATED BY '|'
(attribute3, attribute4)
I have a single text file composed of several of these "LOAD DATA" commands. I receive an error message saying line 6, or the start of the 2nd command, is not proper syntax. And if I try to introduce a "lines terminated by '\n'" code, it says it is not allowed with my mysql version.
You should add a ';' at the end of each load statement.
LOAD DATA
LOCAL INFILE "file.txt"
REPLACE INTO TABLE file
FIELDS TERMINATED BY '|'
(attribute1, attribute2);
LOAD DATA
LOCAL INFILE "file2.txt"
REPLACE INTO TABLE file2
FIELDS TERMINATED BY '|'
(attribute3, attribute4);
See also ERROR 1148: The used command is not allowed with this MySQL version
You can specify that as an additional option when setting up your client connection:
mysql -u myuser -p --local-infile somedatabase