LoginSignup
74
45

More than 5 years have passed since last update.

[ MySQL ] データをCSVでエクスポートしようとして、secure-file-privに引っかかった件

Posted at

まぁ、お恥ずかしいお話ですが、また自分が困りそうなので残しておきます。

◆ 今日やること

Macでローカルに持っているMySQLサーバのデータをエクスポートしようかと。

◆ バージョン

mysql  Ver 14.14 Distrib 5.7.17, for osx10.11 (x86_64) using  EditLine wrapper

◆ データをエクスポートするまでの話

--secure-file-priv問題

今まで、インポート、エクスポートでそんなエラー出たことなかった・・・ということに遭遇。

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

mysql> SELECT 1 INTO OUTFILE '/home/mysql/test.txt';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

簡単に抜粋すると、LOAD DATA and SELECT ... INTO OUTFILE statements and the LOAD_FILE() などを使う、データのインポートとエクスポートの利用を制限します。

これらの操作はFILE権限を持つユーザーから許可されるものです。

secure_file_priv とは

空であれば、何も影響しませんが、セキュアな設定ではありません。
MySQL5.7.6前まではデフォルトで空でしたが、5.7.6以降は、プラットフォームに依存した値で、INSTALL_LAYOUT CMake optionの値によります。

今回は

LocalのMacなので、とりあえず空にすることにしました。

  • 現在の値
mysql> SELECT @@global.secure_file_priv;
+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
| NULL                      |
+---------------------------+
1 row in set (0.00 sec)
  • /etc/my.cnfを作り、空の設定に
sudo cp /usr/local/opt/mysql/support-files/my-default.cnf /etc/my.cnf
sudo vim /etc/my.cnf
[mysqld]
secure-file-priv = ""
mysql.server restart
  • 空になりました
mysql> SELECT @@global.secure_file_priv;
+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
|                           |
+---------------------------+
1 row in set (0.00 sec)

先頭カラムつきでエクスポート

エクスポートする時に、先頭にカラム名をつけたい場合ってあるんですよ。
その場合のやり方。もっとすんなりな方法はないのだろうか。

SELECT 'ColName1', 'ColName2', 'ColName3'
UNION ALL
SELECT ColName1, ColName2, ColName3
    FROM YourTable
    INTO OUTFILE '/path/outfile'

以下でエクスポート完了しました。

select 'id','name','kana','en_name','history'
union all
select od,name,kana,en_name,history
from composers into  outfile "/tmp/composers.csv" fields terminated by ',' optionally enclosed by '"';

ʅ( ´・◡・`)ʃ

74
45
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
74
45