user1990170
user1990170

Reputation: 115

Errcode 13 , SELECT INTO OUTFILE issue

I'm trying to understand the reason why I keep experiencing problems while using INTO OUTFILE command.

I always get this erroro:

ERROR 1 (HY000): Can't create/write to file '/var/www/p1.txt' (Errcode: 13)


SELECT password FROM mysql.user WHERE user='root' INTO OUTFILE '/var/www/p1.txt';

Useful details:

I'm running the command as root. Also, I can freely create folders or files in /var/www/

Errcode 13 I know it means permission denied, but what should I do in order to fix the problem?

Any help will be highly appreciated.

Upvotes: 9

Views: 28292

Answers (7)

kklepper
kklepper

Reputation: 813

I was fighting with this enigmatic error for hours, too, tried everything I could find, to no avail, and finally remembered I already had this same problem years before without being able to find a solution back then no matter how long I searched for and tried all these smart counsels.

secure_file_priv was new to me but I didn't try this because I didn't want to rebuild my docker container just to make this work.

Solution

Looking at my docker-compose file I found the solution to this problem: I didn't have a mapping to the target directory, so for the mysql container this directory wasn't existent.

Workaround

Back then I developed a workaround for my cron jobs:

  1. first dump to tmp (to which my container has a mapping)
  2. mv to where it should be in the first place

Well, it works fine, so why bother.

Upvotes: 0

LF-DevJourney
LF-DevJourney

Reputation: 28529

Check the user of mysqld with,

ps -aef | grep mysql
mysql     9355  9102  0 Aug24 ?        21:53:25 /usr/libexec/mysqld 

Check wiich group mysql belong to with,

groups mysql
mysql : mysql www

Then write the file under path which belong to mysql or have write permission for group www and mysql. For example, test under has write permission to group www.

ll /data/
drwxrwxr-x 2 www    www 4096 Dec  9 19:31 test

Then execute mysql mysql -u root -p -e 'use sc_test; select file_path from sc_files INTO OUTFILE "/data/test/paths.txt";'

Upvotes: 0

Petrus
Petrus

Reputation: 31

Although this post is quite old, in 2018 this problem is still there. I spent a couple of hours banging my head in this maze.

Server version: 5.7.24 MySQL Community Server (GPL) running on Ubuntu 14.04

  1. To allow MySql to SELECT INTO OUTFILE requires to set MySQL's secure-file-priv option in your configuration. Append the following 2 lines to /etc/mysql/mysql.conf:

    [mysqld]
    # allow INTO OUTFILE file and LOAD DATA INFILE to this directory
    secure_file_priv=/usr/share/mysql-files                
    
  2. /usr/share/mysql-files is the directory where my files will be stored. I created it doing:

    sudo su
    cd /usr/share
    mkdir mysql-files
    chown mysql:mysql mysql-files
    chmod a+rw mysql-files
    

Change /usr/share/mysql-files for whatever you prefer, but avoid to use the /tmp directory!

Why? Because, at next time you'll be rebooting, the /tmp directory is happily erased including your precious mysql-files sub-directory. The mysql service then chokes and it won't start, leading to wierd errors with cryptics messages.

  1. restart mysql and check:

    sudo su
    service mysql restart
    mysql
    mysql> SHOW VARIABLES LIKE "%secure%";
    +--------------------------+-------------------------+
    | Variable_name            | Value                   |
    +--------------------------+-------------------------+
    | require_secure_transport | OFF                     |
    | secure_auth              | ON                      |
    | secure_file_priv         | /usr/share/mysql-files/ |
    +--------------------------+-------------------------+
    3 rows in set (0.07 sec)
    mysql> quit
    Bye
    
  2. You are not done, yet!

There is a troll by the name of apparmor who will ruines your project. Edit the file /etc/apparmor/local/usr/sbin/mysqld and append the following 2 lines -- don't forget the ending commas:

    /usr/share/mysql-files rw,
    /usr/share/mysql-files/** rw,

save it, and reparse:

sudo apparmor_parser -r /etc/apparmor.d/usr.sbin.mysqld

That should make it.

Upvotes: 3

Ellert van Koperen
Ellert van Koperen

Reputation: 583

On centos the selinux thing is playing not-nice.

$ getenforce
Enforcing

$ setenforce 0
Permissive

Now this is crude, but worked for me (until reboot, then it switches back on). If this temporary measure works, you need to google for how to configure selinux properly.

Upvotes: 0

tony gil
tony gil

Reputation: 9554

you must alter the permissions for user mysqld. start by running the following command sudo aa-status to check your user status and authorized directories. if you want to change permissions, edit /etc/apparmor.d/usr.sbin.mysqld and insert the directories you want.

you must then restart apparmor sudo /etc/init.d/apparmor restart

Upvotes: 7

Vorsprung
Vorsprung

Reputation: 34307

chown /var/www to the user trying to write the file, or chmod 777 /var/www

this is probably not a secure way of doing it, you might like to consider putting the file elsewhere

Upvotes: 3

Joachim Isaksson
Joachim Isaksson

Reputation: 180887

Even if you're logged in as root into MySQL, the file write will be performed as the user running the actual MySQL daemon.

In other words, you should check which user runs mysqld, and give write permission to the directory for that user.

Upvotes: 12

Related Questions