INTO OUTFILE in SQL on MAC OSX - Errcode 13 "Permission denied" - mysql

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.

Related

MySQL LOAD DATA INFILE error 13 on a Pi

I am trying to use the LOAD DATA INFILE MySQL command on a Raspberry Pi running Raspbian. There are lot of similar questions on here but none seems to answer my problem exactly.
My code works fine on my Windows dev machine but on the Pi I get this error:
Can't get stat of '/var/www/transfer/categories.csv' (Errcode: 13)
Mysql statement is:
LOAD DATA INFILE '/var/www/transfer/categories.csv'
IGNORE INTO TABLE category
FIELDS TERMINATED BY ',' ENCLOSED BY '"';
The code is running in PHP and the database is MySQL.
The file and its '/transfer' folder have read permissions for World.
I have read a little about apparmor but can't see how to check or change how it is configured. There are 2 files in the /etc/apparmor.d folder. One is .usr.sbin.mysqld.swp but it doesn't seem to contain text and the other file refers to lightdm.
The database server and client is on the same server, so the LOCAL keyword doesn't apply.
My MySQL user has global privileges, so includes the FILE privilege.
I have checked the secure_file_priv setting and there is none.
I am sure this is some sort of permission or privilege issue, but I've run out of ideas. I want the file to live under the www folder because the system user has FTP rights to put it there. Ultimately I want to also create the file on the same machine but for now I'm happy to just read the file created under Windows.
$ errno 13
EACCES 13 Permission denied
Check your permissions; especially folder permissions. You can try su or sudo -u to the MySQL user and running ls -la /var/www/transfer/; if you don't see anything then you know the issue has to do with permissions of the folder and/or its contents.
If MySQL is running locally; to see which user: ps -elf | grep mysql
To switch to the MySQL user and test: sudo -u <mysql> bash

Using mysql command line to generate CSV, can't generate it in any other directory except /tmp

I am creating csv and mysql dumps via mysql command line.
For creating mysql file I can easily created the .sql dump in my required directory
mysqldump -u"root" -p"root" dns packet --where="server_id=1 > /var/www/mydatafile/SQLData.sql
that works all okay but in case of CSV, it only creates the files in TMP folder, it can't create files in any other location
mysql -u"root" -p"" dns -e "SELECT * INTO OUTFILE '/var/www/mydatafile/my_csv.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' FROM TABLENAME";
it says
ERROR 1 (HY000) at line 1: Can't create/write to file '/var/www/mydatafile/my_csv.csv' (Errcode: 13)
I have given permission to the www directory but still it gives the same error...May I know the reason behind not creating the CSV into anyother location while SQL can be generated easily..
your directory /var/www/mydatafile/ has to be writable by the mysql user (usually mysql). You can check which user in file my.cnf (in debian/ubuntu based, located in /etc/mysql/ ).
The first command works because you generate sql instruction to stdout and redirect the output to a file, so that use the current user environment.
The second command is internal to mysql, so correct permissions are required for the mysql user.
EDIT: you can alternatively use mysqldump to generate csv with a command like this:
mysqldump -u"root" -p"root" dns packet -p -t --fields-terminated-by=, --lines-terminated-by="\r\n"

Error when trying to export table to a csv file (errcode: 2)

I'm trying to export my table to a .csv file. The query (mysql) is:
SELECT *
FROM TEMP_FINAL
INTO OUTFILE '/home/test/TEMP_FINAL.csv'
FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n';
I get the following error:
"Can't create/write to file '/home/test/TEMP_FINAL.csv' (Errcode: 2)
The directory "test" has permission to write and I`m using Ubuntu 12.04.5 LTS.
What is wrong and how to fix it?
The mysql program does not have access to a user's directory. Change the outfile to somewhere like /tmp/ and move the file later.

Can't create/write to file 'xxxx.txt' (Errcode: 13 - Permission denied)

mysql> \T abc.txt
./mysql: Can't create/write to file 'abc.txt' (Errcode: 13 - Permission denied)
Error logging to file 'logPremiereQueries.txt'
I meet the same problem when I use Windows 8. I solve it by running cmd (MySQL 5.6 Command Line Client) as administrator. And then I could easily find the position of abc.txt. However, in Mac OS X, I do not know how to change the permission. And if log to file successfully, where to find this 'abc.txt'? Any ideas? Thank you very much!
Just leaving this here for the future...
If you are using Windows, open services. Right click on mysql service, select properties. In the properties window select Log On tab then Log on as: Local System account.
If you are ready, restart the service and you can select into outfile anywhere where you have permission to write.
If you don't provide a specific path for a file when using tee (\T) command mysql will try to create or append to the file in the current working directory (directory from which mysql has been started)
The error is self explanatory - you just don't have permissions to write a file in a current working directory.
If you don't know (which is strange) what is your current working directory you can execute system pwd command from mysql command prompt
mysql> \! pwd
Now to resolve the problem either:
change your working directory in terminal to the one that you have write permissions on (e.g. your home directory) and then start mysql client
$ cd ~
$ mysql -uusername -p dbname
in mysql prompt when using tee (\T) command specify a valid path to the directory that you have write permissions on e.g.
mysql> \T /Users/username/abc.txt
please to see -> https://dev.mysql.com/doc/refman/5.0/en/cannot-create.html
One fix is to start mysqld with the --tmpdir option or to add the option to the [mysqld] section of your option file. For example, to specify a directory of C:\temp
I have faced this problem and solved this bug.
SELECT * FROM mahitab INTO OUTFILE '/var/lib/mysql-files/mahitab.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINE;
ERROR 1 (HY000): Can't create/write to file '/var/lib/mysql-files/mahitab.csv' (Errcode: 13 - Permission denied)
We have to give the permission to
[root#localhost tmp]# chmod 1777 /var/lib/mysql-files/
I work with windows 10 and, I also faced this problem when I used 'TEE'/'tee'/'\T' command. I solved it using these steps.
First, we have to create a text file in a notepad and save it. Then copy the file path and use it with the tee command. Don't forget to convert the backslashes to forward slashes.
Ex: TEE C:/Users/User-PC/Desktop/sqlfile.txt

MySQL into outfiles

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.