在mysql中使用以下语句来备份某个表的数据:
SELECT id,type,stock_name INTO OUTFILE "D:/temp/st_stock.txt" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM st_stock;
报错:
ERROR 1045 (28000): Access denied for user 'stock'@'localhost' (using password:YES)
在网上搜索后发现是需要给用户授予file权限:
grant file on *.* to stock@localhost;
再次执行以下语句:
SELECT id,type,stock_name INTO OUTFILE "D:/temp/st_stock.txt" FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM st_stock;
报错:
ERROR 1 (HY000): Can't create/write to file 'd:\temp\st_stock.txt' (Errcode: 13- Permission denied)
看这个异常的意思是没有权限创建和写文件,在网上搜索后发现mysql默认有一个tmpdir路径,使用以下命令搜索该变量:
show variables like '%tmpdir';
发现是在c盘windows下的某个比较复杂的目录,找不到,于是手工修改my.ini文件来修改这个目录:
[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;
最后执行成功!