FILE權限指的是對服務器主機上文件的訪問,數據庫用戶擁有FILE權限才可以執行select into outfile,load data infile操作。
參考文章:http://blog.itpub.net/27126919/viewspace-2125131/
測試select into outfile操作
root用戶上執行
mysql> CREATE USER 'filetest'@'localhost' IDENTIFIED BY ‘123456'
FILE權限是對所有數據庫文件而言的,因此數據庫只能寫成*.*,而不是某一個數據庫 mysql> grant file on test3.* to 'filetest'@'localhost'; ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES //賦予FILE權限 mysql> grant file on *.* to 'filetest'@'localhost'; Query OK, 0 rows affected (0.00 sec) //賦予select權限 grant select on test3.* to 'filetest'@'localhost'; //刷新信息 flush privileges;
filetest用戶上執行
use test3; mysql> select * from test2 into outfile 'd:/test.txt' -> ; ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv opti on so it cannot execute this statement //即使有了file權限,輸出目錄路徑應該secure_file_priv一致 mysql> show variables like '%sec%' -> ; +--------------------------+----------+ | Variable_name | Value | +--------------------------+----------+ | require_secure_transport | OFF | | secure_auth | ON | | secure_file_priv | e:\test\ | +--------------------------+----------+ mysql> select * from test2 into outfile 'e:/test/test.txt'; Query OK, 1 row affected (0.01 sec)
測試load data infile操作:
mysql> load data infile 'e:/test/test.txt' into table test2; ERROR 1142 (42000): INSERT command denied to user 'filetest'@'localhost' for table 'test2' //需要給filetest賦予insert權限 root用戶上執行: mysql> grant insert on test3.* to 'filetest'@'localhost'; flush privileges; filetest用戶: //infile的目錄也要與secure_file_priv一致 mysql> load data infile 'e:/test/test.txt' into table test2; Query OK, 1 row affected (0.07 sec) Records: 1 Deleted: 0 Skipped: 0 Warnings: 0