MySQL エラー「ERROR 1290 (HY000): The MySQL server is running with the –secure-file-priv option so it cannot execute this statement」が発生した場合の対処法

  • 作成日 2021.12.16
  • 更新日 2022.10.20
  • mysql
MySQL エラー「ERROR 1290 (HY000): The MySQL server is running with the –secure-file-priv option so it cannot execute this statement」が発生した場合の対処法

MySQLで、テーブルデータからファイルを作成時に、エラー「ERROR 1290 (HY000): The MySQL server is running with the –secure-file-priv option so it cannot execute this statement」が発生した場合の対処法を記述してます。

環境

  • OS ubuntu21.10
  • MySQL Ver 8.0.27-0ubuntu0.21.10.1 for Linux on x86_64 ((Ubuntu))

エラー全文

以下のクエリで、csvファイルを作成時に発生。

select * from tbl1 into outfile "/tmp/tbl1.csv" fields terminated by ',';

エラー全文

ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

原因

「–secure-file-priv」が設定されていると、そのディレクトリにのみにファイル出力が可能となるため。

mysql> SELECT @@global.secure_file_priv;

+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
| /var/lib/mysql-files/     |
+---------------------------+
1 row in set (0.00 sec)

ここでは「/var/lib/mysql-files/」配下のみに許可されています。

対処法

「/var/lib/mysql-files/」にファイルを出力する

select * from tbl1 into outfile "/var/lib/mysql-files/tbl1.csv" fields terminated by ',';

もしくは「–secure-file-priv」を空にします。

設定ファイルを変更するので、バックアップを取ります。

sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf{,.`date +%y%m%d%H%M%S`}

編集します。「secure-file-priv = “”」を追加します。

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

<追加>
secure-file-priv = ""

画像

再起動します。

sudo service mysql restart

「@@global.secure_file_priv」を確認すると空になっていることが確認できるので、これで、どこでもファイルが作成できます。

mysql> SELECT @@global.secure_file_priv;

+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
|                           |
+---------------------------+
1 row in set (0.00 sec)