简体   繁体   English

拒绝访问 MySQL 中的加载数据 infile

[英]access denied for load data infile in MySQL

I use MySQL queries all the time in PHP, but when I try我在 PHP 中一直使用 MySQL 查询,但是当我尝试

LOAD DATA INFILE

I get the following error我收到以下错误

#1045 - Access denied for user 'user'@'localhost' (using password: YES) #1045 - 用户 'user'@'localhost' 的访问被拒绝(使用密码:是)

Does anyone know what this means?有谁知道这意味着什么?

I just ran into this issue as well.我也刚遇到这个问题。 I had to add LOCAL to my SQL statement.我不得不将LOCAL添加到我的 SQL 语句中。

For example, this gives the permission problem:例如,这给出了权限问题:

LOAD DATA INFILE '{$file}' INTO TABLE {$table}

Add LOCAL to your statement and the permissions issue should go away.LOCAL添加到您的语句中,权限问题应该会消失。 Like so:像这样:

LOAD DATA LOCAL INFILE '{$file}' INTO TABLE {$table}

I had this problem.我有这个问题。 I searched around and did not find a satisfactory answer.我四处寻找,没有找到满意的答案。 I summarise below the results of my searches.我总结了我的搜索结果。

The access denied error could mean that:访问被拒绝错误可能意味着:

  • 'user'@'localhost' does not have the FILE privilege ( GRANT FILE on *.* to user@'localhost' ); 'user'@'localhost' 没有 FILE 权限( GRANT FILE on *.* to user@'localhost' ); or,或者,
  • the file you are trying to load does not exist on the machine running mysql server (if using LOAD DATA INFILE);您尝试加载的文件在运行 mysql 服务器的机器上不存在(如果使用 LOAD DATA INFILE); or,或者,
  • the file you are trying to load does not exist on your local machine (if using LOAD DATA LOCAL INFILE);您尝试加载的文件在本地计算机上不存在(如果使用 LOAD DATA LOCAL INFILE); or,或者,
  • the file you are trying to load is not world readable (you need the file and all parent directories to be world-readable: chmod 755 directory; and, chmod 744 file.dat)您尝试加载的文件不是全局可读的(您需要该文件所有父目录都是全局可读的:chmod 755 目录;以及 chmod 744 file.dat)

Try using this command:尝试使用此命令:

load data local infile 'home/data.txt' into table customer;

This should work.这应该有效。 It worked in my case.它在我的情况下有效。

Ensure your MySQL user has the FILE privilege granted.确保您的 MySQL 用户已授予 FILE 权限。

If you are on shared web hosting, there is a chance this is blocked by your hosting provider.如果您使用共享网络托管,则您的托管服务提供商可能会阻止。

I found easy one if you are using command line如果您使用命令行,我发现了一个简单的方法

Login as mysql -u[username] -p[password] --local-infilemysql -u[username] -p[password] --local-infile

then SET GLOBAL local_infile = 1;然后SET GLOBAL local_infile = 1;

select your database by use [db_name] use [db_name]选择您的数据库

and finally LOAD DATA LOCAL INFILE 'C:\\\\Users\\\\shant\\\\Downloads\\\\data-1573708892247.csv' INTO TABLE visitors_final_test FIELDS TERMINATED BY ','LINES TERMINATED BY '\\r \\n' IGNORE 1 LINES;最后LOAD DATA LOCAL INFILE 'C:\\\\Users\\\\shant\\\\Downloads\\\\data-1573708892247.csv' INTO TABLE visitors_final_test FIELDS TERMINATED BY ','LINES TERMINATED BY '\\r \\n' IGNORE 1 LINES;

The string from Lyon gave me a very good tip: On Windows, we need to use slahes and not backslashes. Lyon 的字符串给了我一个很好的提示:在 Windows 上,我们需要使用斜杠而不是反斜杠。 This code works for me:这段代码对我有用:

    File tempFile = File.createTempFile(tableName, ".csv");
    FileUtils.copyInputStreamToFile(data, tempFile);

    JdbcTemplate template = new JdbcTemplate(dataSource);
    String path = tempFile.getAbsolutePath().replace('\\', '/');
    int rows = template.update(MessageFormat
            .format("LOAD DATA LOCAL INFILE ''{0}'' INTO TABLE {1} FIELDS TERMINATED BY '',''",
                    path, tableName));
    logger.info("imported {} rows into {}", rows, tableName);

    tempFile.delete();

I ran into the same issue, and solve it by folowing those steps :我遇到了同样的问题,并通过以下步骤解决它:

  • activate load_infile variable激活 load_infile 变量
  • grand file permission to my custom mysql user我的自定义 mysql 用户的大文件权限
  • deactivate secure_file_priv variable (my file was uploaded by the webserver to the /tmp folder which is of course not the secured directory of myslq /var/lib/mysql-file)停用 secure_file_priv 变量(我的文件由网络服务器上传到 /tmp 文件夹,这当然不是 myslq /var/lib/mysql-file 的安全目录)

For this 3rd point, you can refer to : https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_priv对于这第三点,您可以参考: https : //dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_secure_file_priv

BR, BR,

AD广告

This happened to me as well and despite having followed all the steps described by Yamir in his post I couldn't make it work.这也发生在我身上,尽管按照 Yamir 在他的帖子中描述的所有步骤,我还是无法让它工作。

The file was in /tmp/test.csv with 777 permissions.该文件位于 /tmp/test.csv 中,具有 777 权限。 The MySQL user had file permissions, LOCAL option was not allowed by my MySQL version, so I was stuck. MySQL 用户有文件权限,我的 MySQL 版本不允许 LOCAL 选项,所以我被卡住了。

Finally I was able to solve the problem by running:最后我能够通过运行来解决问题:

sudo chown mysql:mysql /tmp/test.csv

I discovered loading MySQL tables can be fast and painless (I was using python / Django model manager scripts):我发现加载 MySQL 表既快速又轻松(我使用的是 python/Django 模型管理器脚本):

1) create table with all columns VARCHAR(n) NULL eg: 1)创建包含所有列 VARCHAR(n) NULL 的表,例如:

mysql> CREATE TABLE cw_well2( api VARCHAR(10) NULL,api_county VARCHAR(3) NULL);

2) remove headers (first line) from csv, then load (if you forget the LOCAL, you'll get “#1045 - Access denied for user 'user'@'localhost' (using password: YES)”): 2) 从 csv 中删除标题(第一行),然后加载(如果您忘记了 LOCAL,您将收到“#1045 - 用户 'user'@'localhost' 访问被拒绝(使用密码:YES)”):

mysql> LOAD DATA LOCAL INFILE "/home/magula6/cogswatch2/well2.csv" INTO TABLE cw_well2 FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n' -> ; Query OK, 119426 rows affected, 19962 warnings (3.41 sec)

3) alter columns: 3)改变列:

mysql> ALTER TABLE cw_well2 CHANGE spud_date spud_date DATE;

mysql> ALTER TABLE cw_well2 CHANGE latitude latitude FLOAT;

voilà!瞧!

If you are trying this on MySQL Workbench,如果您在 MySQL Workbench 上尝试此操作,

Go to connections -> edit connection -> select advanced tab转到连接 -> 编辑连接 -> 选择高级选项卡

and add OPT_LOCAL_INFILE=1 in the 'Others' text field.并在“其他”文本字段中添加OPT_LOCAL_INFILE=1

Now restart the connection and try.现在重新启动连接并尝试。

在此处输入图片说明

I was trying to insert data from CSV to MYSQL DB using python.我试图使用 python 将数据从 CSV 插入 MYSQL DB。 You can try the below method to load data from CSV to Database.您可以尝试以下方法将数据从 CSV 加载到数据库。

  1. Make a connection with the Database using pymysql or MySQL.connector any library you want in python.使用 pymysql 或 MySQL.connector 或 python 中所需的任何库与数据库建立连接。
  2. Make Sure you are able to use the in-line while connecting for that while providing host, user, and password try to add local_inline=True.确保您能够在连接时使用内联,同时提供主机、用户和密码尝试添加 local_inline=True。

Skipping to load data part.跳过加载数据部分。 sql = f'''LOAD DATA LOCAL infile "filename.csv" INTO TABLE schema.tablename FILED TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'''' Note: If you have column names in CSV, use IGNORE ROW 1 LINES. The execute the sql by: cursor.execute(sql) conn.commit() conn.close()

这可能意味着您为'user'@'localhost'提供的密码不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM