2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MySQLからcsvを出力する

Last updated at Posted at 2020-03-29

環境

mysql : Ver 8.0.19 for Linux on x86_64 (MySQL Community Server - GPL)

SELECT文でcsvファイルを出力する

SELECT * FROM hoge INTO OUTFILE '/output/hoge.csv'
 FIELDS TERMINATED BY ','
 OPTIONALLY ENCLOSED BY '"'

dockerでMYSQLコンテナ立ち上げている場合、OUTFILEをマウント位置にすると良い

csvファイルを出力する際にエラーを吐いた場合

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

データの入出力エラー

MySQLの設定を確認

mysql> SELECT @@global.secure_file_priv;

+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
| NULL                      |
+---------------------------+

このNULLがダメらしい

設定を変更して解決する

DockerでMySQLコンテナを立ち上げて.sqlファイルからDBを作る
立ち上げているコンテナは上の通りなので、my.cnfに以下を追記

[mysqld]
secure-file-priv = ""

コンテナ再起動で設定を反映させてると設定欄のNULLが消える

mysql> SELECT @@global.secure_file_priv;

+---------------------------+
| @@global.secure_file_priv |
+---------------------------+
|                           |
+---------------------------+

これでSELECT文でcsvファイルが吐き出せるようになっているはず

参考資料

MySQLのバージョン確認方法
MySQLのSELECT文でcsvを出力する
MySQLでCSV出力しようとしたら --secure-file-priv option のエラーになった
MySQLでCSVファイルを使って結果出力やデータ入力を行う方法

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?