ERROR 2068 (HY000):LOAD DATA LOCAL INFILE 文件请求因访问限制而被拒绝

ERROR 2068 (HY000): LOAD DATA LOCAL INFILE file request rejected due to restrictions on access

评论我正在尝试,

mysql> 

LOAD DATA LOCAL INFILE '/var/tmp/countries.csv' 
INTO TABLE countries 
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' LINES 
TERMINATED BY '\n' 
IGNORE 1 LINES 
(CountryId,CountryCode,CountryDescription,CountryRegion,LastUpdatedDate,created_by,created_on)
SET created_by = 'DH_INITIAL_LOAD', created_on = current_timestamp();

ERROR 2068 (HY000): LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.`

工作正常,我为 python 脚本下载了 pymysql 和 mysql 连接器。我卸载并检查它仍然无法正常工作。 verion 和 infile 是 ON,

 select version() -| 8.0.17



mysql> SHOW GLOBAL VARIABLES LIKE 'local_infile';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile  | ON    |
+---------------+-------+
1 row in set (0.00 sec)

已知问题:https://bugs.mysql.com/bug.php?id=91872

尝试连接到 mysql 本身时将 local-infile 设置为 1 并执行加载命令: mysql --local-infile=1 -h$MASTER_DB_HOST -u$MASTER_DB_USER -p$MASTER_DB_PASSWD -D $MASTER_DB_NAME

使用MySql Workbench 8或以上介绍了这个问题。这为我修复了它:

This restriction can be removed from MySQL Workbench 8.0 in the following way. Edit the connection, on the Connection tab, go to the 'Advanced' sub-tab, and in the 'Others:' box add the line 'OPT_LOCAL_INFILE=1'.

This should allow a client using the Workbench to run LOAD DATA INFILE as usual.

引自此link: https://bugs.mysql.com/bug.php?id=91872

对于ubuntu:

  1. 编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf并在末尾添加以下行:

    # secure_file_priv=""

  2. 重启服务

     systemctl stop mysql 
     systemctl start mysql
    
  3. 运行: mysql -u root -p 并查看本地infile变量

      mysql> show global variables like 'local_infile';
    
      +---------------+-------+ 
      | Variable_name | Value |
      +---------------+-------+
      | local_infile  | OFF   |
      +---------------+-------+
      1 row in set (0.00 sec)
    
  4.   mysql> set global local_infile=true;
    
      +---------------+-------+ 
      | Variable_name | Value |
      +---------------+-------+
      | local_infile  | ON    |
      +---------------+-------+
      1 row in set (0.00 sec)
    
  5.   mysql> exit
      Bye
    
  1. 运行

     mysql --local-infile=1 -u root -p
    
  2.   LOAD DATA INFILE '/var/lib/mysql-files/filename' INTO TABLE tablename;
    

要修复此错误 (mysql 8):

ERROR 2068 (HY000): LOAD DATA LOCAL INFILE file request rejected due to restrictions on access.

将以下行添加到服务器配置文件的“客户端”部分下:

[client]
loose-local-infile=1

这将修复错误。但是,这假设您已经在“mysqld”部分下设置了以下内容:

[mysqld]
local_infile=1

在配置文件中设置这两个参数将允许从任何目录加载数据。

这是一个简单的使用示例。 运行 在 mysql 终端:

load data local infile '/path/to/file/data.tsv' into table my_table_name
 fields terminated by '\t'
 LINES TERMINATED BY  '\n'
(
 `col1`,
 `col2`,
 `col3`,
 `col4`
);

对于Mac用户,如果上述解决方案不起作用(我正在使用mac OS Big Sur)

转到终端并输入

$ vi ~/.my.cnf

然后将其添加到文件中

[mysqld]
secure_file_priv = ''

保存并退出window。现在转到终端并使用

重新启动 mysql 服务器
$mysql.server restart

使用

进入mysql服务器
$ mysql -u <your username> -p

现在在mysql

中输入以下内容
mysql> SHOW VARIABLES LIKE "secure_file_priv";

这应该将变量的值显示为空白。 现在,如果您使用查询

load data local infile '<complete_file_path>' 
into table <table_name>
fields terminated by ',';

这应该有效!还要确保您已使用以下变量将以下变量设置为“ON”:

set global local_infile = 1;

更新

对于windows用户 - 安装了mysql 8.0 下面是上面提到的安全文件变量

secure-file-priv="C:/ProgramData/MySQL/MySQL Server 8.0/Uploads"

在 windows 上:
在客户端和服务器以及设置中启用本地后对我来说(my.ini)

        [client]
          local_infile=1
        [mysqld]
          secure_file_priv=""

 the sql command ...
   load data local infile "FILE" 
 change to 
    load data infile "FILE"

works every time ...
so much so I do not even use the load local anymore and am not sure of the point of the command ...