一、問題描述
數據文件 a.txt 導入mysql表中。
1、第一個問題
# mysqlimport -uabc -p'123' -h db1 -P 3306 DB a.txt ERROR 1045 (28000): Access denied for user 'root'@'db1' (using password: YES), when using table:
在排除密碼和格式填寫錯誤之後,查看mysql.user表 ,確認是否有File權限。
確認之後確實沒有File權限。更新mysql.user表的File權限爲'Y',刷新權限。
> flush privileges;
2、第二個問題
重新執行導入命令
# mysqlimport -uabc -p'123' -h db1 -P 3306 DB a.txt mysqlimport: Error: 1290, The MySQL server is running with the --secure-file-priv option so it cannot execute this statement, when using table:
--使用load data infile 也報相同錯誤。
查看--secure-file-priv參數
> show global variables like 'secure_file_priv'; +------------------+-----------+ | Variable_name | Value | +------------------+-----------+ | secure_file_priv | /dev/null | +------------------+-----------+
此參數不是動態參數。需要添加配置文件,重啓服務纔可以生效。
1)不限制導入導出
# cat /etc/my.cnf [mysqld] secure_file_priv
2)限制在特定目錄下
# cat /etc/my.cnf [mysqld] secure_file_priv = /tmp
--只允許/tmp目錄下的數據文件可以導入,其他目錄下的文件沒有權限導入。
因爲我們使用的是騰訊雲的mysql數據庫,這個參數不能修改,但是我們又必須有導入導出的功能,所以我們只能自建實例,自己做限制。
3、第三個問題
# mysqlimport -r --host="db1" --port="3306" --user="abc" --password="123" --fields-terminated-by="\t" --lines-terminated-by="\n" --columns="a,b,c,d" DB "/tmp/a.txt" mysqlimport: Error: 13, Can't get stat of '/data/codebase/a.txt' (Errcode: 2 "No such file or directory"), when using table: a
默認是找mysql庫所在服務器的/tmp目錄下的文件,而不是執行命令的機器。
加 --local 參數,表示指定的導入文件是執行命令的機器。