mysql secure-file-priv選項問題的解決方案

來源:互聯網
上載者:User
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=''

查看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使用令牌桶演算法實現流量控制

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.