0

How do I set a value for secure_file_priv?

I found this which tells what settings may be used https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_priv

The mysql server starts without any command line options. There is nothing to override its' .cnf file.

user@server:~$ ps aux | grep [m]ysql
mysql     4495  0.0  7.0 544368 144924 ?       Ssl  09:16   0:02 /usr/sbin/mysqld

Running :~$ mysqld --verbose --help tells me

Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

Only the 2nd file exists. It is the beginning of a symlink chain to /etc/mysql/my.cnf.migrated as follows...

user@server:~$ ls -l /etc/mysql/my.cnf
lrwxrwxrwx 1 root root 24 Aug 12 15:15 /etc/mysql/my.cnf -> /etc/alternatives/my.cnf
user@server:~$ ls -l /etc/alternatives/my.cnf
lrwxrwxrwx 1 root root 26 Aug 12 15:15 /etc/alternatives/my.cnf -> /etc/mysql/my.cnf.migrated
user@server:~$ ls -l /etc/mysql/my.cnf.migrated
-rw-r--r-- 1 root root 4455 Dec 13 03:18 /etc/mysql/my.cnf.migrated

I've tried setting values for secure_file_priv in that last file, restarting the mysql server and even rebooting the Ubuntu server. No matter the value that is set the command

mysql> SELECT @@GLOBAL.secure_file_priv;

always returns /var/lib/mysql-files/.

I've also searched for other .cnf files and tried setting the value for secure_file_priv in each of them

user@server:~$ find /etc -iname "m*.cn*" -type f
/etc/mysql/conf.d/mysql.cnf
/etc/mysql/my.cnf.migrated
/etc/mysql/my.cnf.fallback
/etc/mysql/mysql.conf.d/mysqld.cnf
/etc/mysql/mysql.cnf

No matter. After making a change, restarting the server, and checking the value with

mysql> SELECT @@GLOBAL.secure_file_priv;

the result /var/lib/mysql-files/ is always the same. It doesn't change.

What do I need to do to set a value for secure_file_priv?

Alan
  • 1
  • 1
  • 2

1 Answers1

0

Add the variable under the [mysqld] section of /etc/mysql/my.cnf:

secure_file_priv=/absolute/path

Then restart mysql service afterwards.

Check with SELECT @@secure_file_priv;

It worked for me:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql [localhost] {msandbox} ((none)) > select @@secure_file_priv;
+--------------------+
| @@secure_file_priv |
+--------------------+
| /tmp/              |
+--------------------+
1 row in set (0.00 sec)

To avoid getting ERROR 1290 eg:

 mysql [localhost] {msandbox} (test) > select * from t1 into outfile 'file.sql';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

Make sure you use absolute paths like this:

mysql [localhost] {msandbox} (test) > select * from t1 into outfile '/tmp/file.sql';
Query OK, 1 row affected (0.00 sec)

mysql [localhost] {msandbox} (test) > \! ls /tmp/file.sql
/tmp/file.sql

mysql [localhost] {msandbox} (test) > truncate t1;
Query OK, 0 rows affected (0.04 sec)

mysql [localhost] {msandbox} (test) > load data infile '/tmp/file.sql' into table t1;
Query OK, 1 row affected (0.00 sec)
Records: 1  Deleted: 0  Skipped: 0  Warnings: 0
jerichorivera
  • 489
  • 1
  • 4
  • 12
  • Tried that. Didn't work. Had tried secure_file_priv at bottom of ..../my.cnf Based on your suggestion put it in [mysqld] section of my.cnf (it hadn't been). Run select @@secure_file_priv; shows value set as I'd like. Still get error ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement Now think user rights issue? Looking into mysql vs system user rights. If that's wrong direction pls stop me. p.s. Sorry no follow up before now. Was waiting for email notice and never saw one. Really appreciate the help. – Alan Dec 31 '16 at 14:39
  • @Alan, please see edited comment – jerichorivera Jan 01 '17 at 12:06
  • Thought I had done absolute paths. Must not have. Now getting different error `"Can't create/write to file ... (Errorcode: 13 - Permission denied)"` . Target directory has `drwxrwxrwx` permissions. All directories above, two of them, have `drwxr-xr-x`. syslog shows `apparmor="DENIED"`. So now I need to work that out. – Alan Jan 02 '17 at 12:05
  • That seems to be an apparmor issue now. I bet if you configure apparmor or test by disabling it (not recommended) then it will work. – jerichorivera Jan 02 '17 at 12:23