mysql可使用 into outfile 參數把表中資料匯出到csv,例如可用以下命令把user表的資料匯出到user.csv
select * from user into outfile '/tmp/user.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';
執行後,user表的資料會匯出到/tmp/user.csv。
參數說明:
into outfile ‘匯出的目錄和檔案名稱’
指定匯出的目錄和檔案名稱
fields terminated by ‘欄位間分隔字元’
定義欄位間的分隔字元
optionally enclosed by ‘欄位包圍符’
定義包圍欄位的字元(數值型欄位無效)
lines terminated by ‘行間分隔字元’
定義每行的分隔字元
問題分析
以上命令在mysql5.6下運行沒有問題,但在mysql5.7下運行則出現了以下錯誤。
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
查看官方文檔,secure_file_priv參數用於限制LOAD DATA, SELECT …OUTFILE, LOAD_FILE()傳到哪個指定目錄。
secure_file_priv 為 NULL 時,表示限制mysqld不允許匯入或匯出。
secure_file_priv 為 /tmp 時,表示限制mysqld只能在/tmp目錄中執行匯入匯出,其他目錄不能執行。
secure_file_priv 沒有值時,表示不限制mysqld在任意目錄的匯入匯出。
查看 secure_file_priv 的值,預設為NULL,表示限制不能匯入匯出。
mysql> show global variables like '%secure_file_priv%';+------------------+-------+| Variable_name | Value |+------------------+-------+| secure_file_priv | NULL |+------------------+-------+1 row in set (0.00 sec)
因為 secure_file_priv 參數是唯讀參數,不能使用set global命令修改。
mysql> set global secure_file_priv='';ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable
解決方案
開啟my.cnf 或 my.ini,加入以下語句後重啟mysql。
查看secure_file_priv修改後的值
mysql> show global variables like '%secure_file_priv%';+------------------+-------+| Variable_name | Value |+------------------+-------+| secure_file_priv | |+------------------+-------+1 row in set (0.00 sec)
修改後再次執行,成功匯出。
mysql> select * from user into outfile '/tmp/user.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';Query OK, 15 rows affected (0.00 sec)
mysql可使用 into outfile 參數把表中資料匯出到csv,例如可用以下命令把user表的資料匯出到user.csv
select * from user into outfile '/tmp/user.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';
執行後,user表的資料會匯出到/tmp/user.csv。
本文講解了mysql5.7匯出資料提示--secure-file-priv選項問題的解決方案,更多相關內容請關注php中文網。
相關推薦:
php 利用debug_backtrace方法跟蹤代碼調用
mysql在終端執行sql並把結果寫入檔案的方法
php 基於redis使用令牌桶演算法實現流量控制