在mysql中使用如下語句來備份某個表的數據:mysql
SELECT id,type,stock_name INTO OUTFILE "D:/temp/st_stock.txt" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM st_stock;
報錯:sql
ERROR 1045 (28000): Access denied for user 'stock'@'localhost' (using password:YES)
在網上搜索後發現是須要給用戶授予file權限:windows
grant file on *.* to stock@localhost;
再次執行如下語句:code
SELECT id,type,stock_name INTO OUTFILE "D:/temp/st_stock.txt" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM st_stock;
報錯:it
ERROR 1 (HY000): Can't create/write to file 'd:\temp\st_stock.txt' (Errcode: 13- Permission denied)
看這個異常的意思是沒有權限建立和寫文件,在網上搜索後發現mysql默認有一個tmpdir路徑,使用如下命令搜索該變量:io
show variables like '%tmpdir';
發現是在c盤windows下的某個比較複雜的目錄,找不到,因而手工修改my.ini文件來修改這個目錄:class
[mysqld] #該目錄須要預先建立 tmpdir=D:/soft/MySQL/MySQL Server 5.6/temp
重啓mysql後再執行如下語句,注意路徑已經改成tmpdir指向的路徑變量
SELECT id,type,stock_name INTO OUTFILE "D:/soft/MySQL/MySQL Server 5.6/temp/st_stock.txt" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM st_stock;
最後執行成功!file