文 | Promise Sun
一、Bug問題
系統(tǒng)環(huán)境:win11
運行環(huán)境:MySQL 8.0.28
圖形工具:Navicat Premium 15
Bug描述: 在備份數據庫時,運行SQL語句時報錯:
eg:SQL語句:
select * from mysql_test.customers into outfile 'C:/BACKUP/backupfile.txt'
fields terminated by ',' optionally enclosed by '''' lines terminated by '?';
報錯信息:

二、Bug分析
1.首先,翻譯信息
報錯信息:
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
翻譯:MySQL服務器運行在secure-file-priv選項時,無法執(zhí)行此語句。
2.然后,分析報錯信息
1)mysql文件的導入和導出路徑有默認的設置,secure-file-priv出現問題,說明SQL語句中傳入的文件路徑與默認的路徑沖突,才會報錯。
secure-file-priv的值有3種情況:
secure_file_prive=null —— 默認值為null,限制mysql,禁止導入導出文件
secure_file_prive=“” —— 表示不限制路徑,允許mysql 導入導出文件
secure_file_prive=“D:/path” —— 指定路徑,表示只允許在該路徑目錄下導入導出文件(這里的路徑值可以自己設置)
2)查看secure-file-priv當前的值
可以使用下面兩種命令行方式查看secure-file-priv當前的值(任選其一即可)
第一種查看方式:
show variables like '%secure%';
顯示結果

第二種查看方式:
show variables like "secure_file_priv";
顯示結果

3)從上面顯示結果中,可以明確系統(tǒng)默認禁止mysql導入導出文件。重新設置一下secure-file-priv的值即可。
三、解決方案
1) 首先,找到你的mysql安裝路徑下的my.ini文件
注:若安裝路徑下沒有找到此文件,可以在此安裝路徑下自己新建一個my.ini文件。
下圖是本人的mysql安裝路徑,僅供參考

2) 打開my.ini文件,在文件末尾設置secure-file-priv的值。
secure-file-priv的值可以指定路徑設置,也可以不限制路徑。區(qū)別是指定路徑相對安全,但在mysql導入導出文件時則必須在指定路徑下才可以,否則依然會報錯。
注:本人直接設置不限制路徑,你可以自行決定是否設置指定路徑
secure_file_prive=“”

3) 然后,my.ini文件 點擊另存為窗口,編碼選擇ANSI,保存。**

4) 驗證secure-file-priv當前的值是否設置成功。
注:成功設置后,就可以使用SQL語句了。
命令行查看:
show variables like "secure_file_priv";
驗證結果:

版權聲明:本文為博主原創(chuàng)文章,轉載請點贊此文并注明出處,謝謝!