2013-08-05 71 views
4

我試圖加載一個目錄的XML文件到數據庫表作爲斑點。每行將包含一個相應的blob文件。當通過python腳本將文件加載到表中時,值被插入爲空值。在mysql命令行中運行等效命令時也是如此。MySQL的LOAD_FILE()加載空值

有一點我能夠在將某些文件夾權限更改爲mysql後插入值,但由於需要腳本權限,我不得不修改目錄/ var/lib/mysql/foo的所有權,因此值將被重新插入爲null有效地打破了我寫的腳本。我不記得必要的目錄權限更改。

以下是話題的討論:

http://bugs.mysql.com/bug.php?id=38403

隨着

MySQL LOAD_FILE returning NULL

回答

5

確保:

  • 有父目錄
  • 文件權限必須被授予explicily執行權限。 (。上GRANT FILE對用戶@本地)
  • 你已經刷新特權
  • 您已註銷,並重新登錄權限的

例如在父目錄:

 

mysql> \! ls -ld `dirname /home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg` 
drwxrwxr--. 2 jlam jlam 4096 May 12 14:22 /home/jlam/code/projectName/doc/filesForTesting/images 

mysql> select hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/image 

Test01.jpg')); 
+-------------------------------------------------------------------------------------------------------------+ 
| hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg')) | 
+-------------------------------------------------------------------------------------------------------------+ 
| NULL                          | 
+-------------------------------------------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 



mysql> \! chmod o+x /home/jlam/code/projectName/doc/filesForTesting/images 
mysql> \! ls -ld `dirname /home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg` 
drwxrwxr-x. 2 jlam jlam 4096 May 12 14:22 /home/jlam/code/projectName/doc/filesForTesting/images 
mysql> select hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg')); 
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
| hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg'))                                 
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
| FFD8FFE1001845786966000049492A00080000000000000000000000FFEC00114475636B7900010004000000500000FFE10407687474703A2F2F6E732E61646F62652E636F6D2F7861702F312E302F003C3F787061636B657420626567696E3D22EFBBBF222069643D2257354D304D7043656869487 
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

用戶權限示例:

 

16:38:09 (getImages) ~/code/projectName/doc/filesForTesting/images$ mysql -u eventCal -p eventCal 
Enter password: 

mysql> show grants; 
+-----------------------------------------------------------------------------------------------------------------+ 
| Grants for [email protected]                     | 
+-----------------------------------------------------------------------------------------------------------------+ 
| GRANT USAGE ON *.* TO 'eventCal'@'localhost' IDENTIFIED BY PASSWORD '*xxxx' | 
| GRANT ALL PRIVILEGES ON `tmp`.* TO 'eventCal'@'localhost'              | 
| GRANT ALL PRIVILEGES ON `eventCalTesting`.* TO 'eventCal'@'localhost'           | 
| GRANT ALL PRIVILEGES ON `eventCal`.* TO 'eventCal'@'localhost'             | 
| GRANT ALL PRIVILEGES ON `eventCal_categoryMigration`.* TO 'eventCal'@'localhost'        | 
+-----------------------------------------------------------------------------------------------------------------+ 
5 rows in set (0.00 sec) 

mysql> select hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg')); 
+-------------------------------------------------------------------------------------------------------------+ 
| hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg')) | 
+-------------------------------------------------------------------------------------------------------------+ 
| NULL                          | 
+-------------------------------------------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

在其他根會話:

 

mysql> grant file ON *.* to [email protected]; 
Query OK, 0 rows affected (0.00 sec) 

mysql> flush privileges; 
Query OK, 0 rows affected (0.00 sec) 

回到用戶會話,我仍然無法加載文件

 

mysql> select hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg')); 
+-------------------------------------------------------------------------------------------------------------+ 
| hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg')) | 
+-------------------------------------------------------------------------------------------------------------+ 
| NULL                          | 
+-------------------------------------------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

.....但是,如果我註銷並重新登錄在:

 

mysql> exit 
Bye 

16:40:14 (getImages) ~/code/projectName/doc/filesForTesting/images$ mysql -u eventCal -p eventCal 
Enter password: 

mysql> select hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg')); 
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
| hex(LOAD_FILE('/home/jlam/code/projectName/doc/filesForTesting/images/imageTest01.jpg'))                                 
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
| FFD8FFE1001845786966000049492A00080000000000000000000000FFEC00114475636B7900010004000000500000FFE10407687474703A2F2F6E732E61646F62652E636F6D2F7861702F312E302F003C3F787061636B657420626567696E3D22EFBBBF222069643D2257354D304D7043656869487 
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

+0

很好的解釋,但沒有解決 –

+0

@Acubo的答案下面提供了一個額外的線索。我的回答是在2014年寫回來的,可能平臺的默認配置已經改變。 –

2

爲了加載文件的工作確保所有的權限被授予爲MySQL主人和組。

chown mysql:mysql /var/lib/mysql/foo/* 
chmod go+rw /var/lib/mysql/foo/* 
+0

這是最後的關鍵步驟,它最終允許我在數據庫中加載所需的文件。 'show VARIABLES'表明secure_file_priv變量設置正確,但我仍然無法加載文件。 – user208145

0

我複製了/var/lib/mysql/images(創建圖像文件夾後)的圖像,它的工作!

1

我已經失去了一些時間與這個問題。在我的情況下,問題必須與「my.ini」中定義的變量secure_file_priv有關。

從MySQL文檔:

如果secure_file_priv系統變量設置爲一個非空 目錄名,要加載的文件必須位於該 目錄

因此,有兩種方式:

1)刪除此選項 2)使用上傳文件的默認文件夾(「C:\ ProgramData \ MySQL \ MySQL Server 5.7 \ Uploads」) 。我也不得不在Windows中使用雙斜槓。