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

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

Related

MySQLdump with WINDOWS10 : OS errno 13 - (Permission denied) when executing 'SELECT INTO OUTFILE'

I am unable to write tab separated text files into specified TSV/ folder using mysqldump on WINDOWS10 (MySQL80 service).
The Command prompt (or powershell) is in admin mode.
secure-file-priv ="" in my.ini MySQL config file.
MySQL was installed on my local computer and I use a localhost connection with the root username.
Command is:
mysqldump -T C:\Users\...\20200430_Dump\TSV\ --user root -p database_name
OUTPUT:
mysqldump: Got error: 1: Can't create/write to file 'C:\Users...\20200430_Dump\TSV\table_name.txt' (OS errno 13 - Permission denied) when executing 'SELECT INTO OUTFILE'.
They are many related post on LINUX but I am specifically looking for an solution for WINDOWS
My small hypothesis:
Some linux posts suggest that mysqld use a specific user-account (that seems to be mysql in linux but this is unfortunately not documented at all for any OS). So I imagine that when the server is executing INTO OUTFILE queries he needs to have permission for the TSV/ folder, but i do not know how to do give him this permission in WINDOWS.
best
Left click on your TSV/ folder > Properties > Security Tab > Edit > Add
You need to add NETWORK SERVICE in Group or User names with Full control privilege (probably just Write is sufficient).
The mysqldump now works (tested on windows10)
You need to create an empty SQL file and specify the path to it.

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

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

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.

mysqldump: Got error: 1: "Can't create/write to file '*.txt' (Errcode: 13 "Permission denied")" when executing 'SELECT INTO OUTFILE'

Linux. No SELinux (disabled).
I try to run this command as my linuxuser (member of group mysql):
mysqldump --user=root --tab "/some/folder" dbname
Getting this error:
mysqldump: Got error: 1: "Can't create/write to file 'sometable.txt' (Errcode: 13 "Permission denied")" when executing 'SELECT INTO OUTFILE'
If I look in the folder, there is a sometable.sql file, but no txt file.
As I understand, the sql file is created from the user's name who run the command (linuxuser in my case), but the txt file gets generated by mysql server (mysql user in this case). Note that mysql user is also a member of mysql group.
What could be the problem? Tried everything :)
If you trying to execute a mysqldump in a client diferent that the server
can be a problem
I tried with mysqldump Ver 10.14 Distrib 5.5.56-MariaDB, for Linux (x86_64), and works only with the /tmp/ dir.
Perhaps, help info say that will not work
-T, --tab=name Create tab-separated textfile for each table to given path. (Create .sql and .txt files.) NOTE: This only works if mysqldump is run on the same machine as the mysqld server.
The solution was to change "some/folder" to an external location. Looks like there were permission problems because originally the folder mentioned was under Perforce root and probably it had read-only permissions.
I then changed the new folder permissions to:
mylinuxuser:mysql
making sure that both the user with which the process was started, as well as mysql server have permissions to write there. This is because the sql file is written from user's name and txt file is written from mysql server's name.
I also added my user to the mysql group.
This was something I just found and wanted to pass along for the folks searching for the same answer.
This has been occurring on systems running systemd. In the /lib/systemd/system/mariadb.service file is the setting, 'PrivateTmp=true'. This is a security measure to prevent the data from being seen by folks who shouldn't be seeing it.
If you run:
SELECT * TO OUTFILE '/var/tmp/tablename.txt' FROM TABLENAME;
It will create the file but it creates it in a systemd protected directory.
/var/tmp/systemd-private-[hash]-mariadb-[hash]/tmp/tablename.txt
You can either save the data from here which is perfectly acceptable or stop mariadb, create the directory /etc/systemd/system/mariadb.service.d and in that directory create the file 'mariadb.conf' (as an example; any file should do) with the following:
[Service]
PrivateTmp=false
Run systemctl daemon-reload to reload the configuration and restart mariadb.
You should be able to create backups now.
What has worked for me (in Windows), trying to export a file using:
SELECT * INTO OUTFILE 'C:/documents/dumps' from file
MySQL Service properties --> Log on: take note account defined.
Directory C:\documents\dumps --> properties --> Security: account defined above needs read/write privileges

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"