I have trouble exporting a mySQL query result to a file in a specific folder. Here is what I did:
SELECT N1_Name,V_Name,N2_Name INTO OUTFILE '/home/user/Documents/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\' LINES TERMINATED BY '\n'
FROM Result;
I always get "ERROR 1 (HY000): Can't create/write to file '/home/tle/Documents/PTQL_result.csv' (Errcode: 13)", which is permission denied error.
The query works if I use the default location or tmp.
I even chmod Documents to 777 but the mysql process still could not write to it.
Any ideas is greatly appreciated.
Thanks
The file will be create by the mysql user, so the mysql user needs write permission on the directory. It sounds like you've granted that permission.
The mysql user also needs execute permission on the higher-level directories, specifically this one:
/home/tle
I'm guessing the mysql user does not have execute permission on that directory. If you grant that permission it should work.
Related
When I try to export records using the below query, I am getting an error
SELECT ID, Name FROM Emp
INTO OUTFILE 'C:/temp/hash.csv'
FIELDS ENCLOSED BY '"'
TERMINATED BY ';'
ESCAPED BY '"'
LINES TERMINATED BY '\r\n';
Error:
SQL Error (1045): Access denied for user 'test'#'%' (using password: YES)
I am getting same error while running the below:
GRANT FILE ON *.* TO 'test';
SHOW VARIABLES LIKE "secure_file_priv";
secure_file_priv: /tmp/
I want to run a stored procedure to export the records through a .Net application. I am expecting the output file to be created on the windows computer. The MySQL instance is running on AWS.I think the "secure_file_priv" path is causing the issue. The same query is working in another MySQL instance that is running on windows server.
Thank you.
You need to out file the csv file to aws instance first, and then download it to your windows computer.
When I run the below query:
SELECT * FROM selecteddb.mytable
where time between "2018-08-06 00:00:00" and "2018-08-13 00:00:00"
INTO outfile "~/stats.csv"
FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n';
I get the below error:
Error Code: 1045. Access denied for user 'admin'#'%' (using password: YES)
But when I run just the query without outfile it runs just fine.
I'm using MySQL Workbench and Mac to run the query.
It looks like your admin user doesn't have FILE privilege. You can grant the permission and it will work.
GRANT FILE ON * . * TO 'admin'#'%';
The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the
selected rows to a file. The file is created on the server host, so
you must have the FILE privilege to use this syntax. file_name cannot
be an existing file, which among other things prevents files such as
/etc/passwd and database tables from being destroyed. The
character_set_filesystem system variable controls the interpretation
of the file name.
https://dev.mysql.com/doc/refman/8.0/en/select-into.html
=========================================================
Also note that mysql server needs to have write permission on the path you provide.
The FILE privilege also enables the user to create new files in any
directory where the MySQL server has write access. This includes the
server's data directory containing the files that implement the
privilege tables.
If you execute your query as:
SELECT * FROM selecteddb.mytable ..... INTO outfile "~/stats.csv";
This will try to create the file in the home directory. If you omit the paths and leave only the file name, it will be created in the Mysql's data directory. (/var/lib/mysql in my Ubuntu machine).
So this should work without any problem.
SELECT * FROM selecteddb.mytable ..... INTO outfile "stats.csv";
Hope it helps!!
I switched from being a long term windows user to mac.
Now I face this problem trying to save a query to a csv file:
mysql> SELECT * FROM mytable
-> INTO OUTFILE '/Users/localuser/Documents/myfolder/test.csv'
-> FIELDS TERMINATED BY ','
-> ENCLOSED BY '"'
-> LINES TERMINATED BY '\n';
But i get:
ERROR 1 (HY000): Can't create/write to file
'/Users/localuser/Documents/myfolder/test.csv' (Errcode: 13 "Permission denied")
I think it is something about setting permission but i have not found a solution that works for my mac osx.
thank you in advance.
Please take into account that this ones works
MySQL writing on a text file
But what I am looking for is more the operation to set any directory so that *.csv files can be put there by the sql server.
I meet the same problem, and I find that it is relevant to file permission of Mac OS, not relevant to MySQL.
The solution is to make all the folders in the path have permission of 755 (read and execution) and to make the folder putting the csv file has the permission of 777 which can write the file.
So in your case, the folders in '/Users/localuser/Documents' should have 755 permission and the folder 'myfolder' should have 777 permission.
I am writing a very simple shell script to dump a table into CSV file. Here is part of it:
day=`/bin/date +'%Y-%m-%d'`
file="/tmp/table-$day.csv"
rm $file
query="SELECT * FROM table INTO OUTFILE '$file' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\\n'"
echo "$query" | mysql <connection_parameters>
I put in rm $file to make sure that the file does not exist prior to the query's execution.
However, when I execute the script, I get conflicting messages:
rm: cannot remove `/tmp/table-2013-02-08.csv': No such file or directory
ERROR 1086 (HY000) at line 1: File '/tmp/table-2013-02-08.csv' already exists
I cannot find the OUTFILE anywhere in the machine.
So what is wrong.. ?
Thank you.
I have found the answer.
OUTFILE creates the file on the MySQL server, rather than on my MySQL client's machine.
Check if you have this in /etc/passwd
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
change shell to /bin/bash instead
I'm trying to create a csv export of data from mysql using the following query:
SELECT * INTO OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM getfreepellets WHERE 1
And I get the following error:
#1045 - Access denied for user '[username]'#'localhost' (using password: YES)
(removed username but it's the right one)
How would I go about granting access to this user to create a file on the server?
Edit:
I changed the first line to be my exact home path, and received the same error.
You may want to GRANT your user the FILE privilege:
The FILE privilege gives you permission to read and write files on the server host using the LOAD DATA INFILE and SELECT ... INTO OUTFILE statements and the LOAD_FILE() function.
A user who has the FILE privilege can read any file on the server host that is either world-readable or readable by the MySQL server. (This implies the user can read any file in any database directory, because the server can access any of those files.) The FILE privilege also enables the user to create new files in any directory where the MySQL server has write access. As a security measure, the server will not overwrite existing files.
To grant the FILE privilege, log-in as root and execute:
GRANT FILE ON *.* TO 'your_user'#'localhost';