An exception occurred while executing 'LOAD DATA LOCAL INFILE

给你一囗甜甜゛ 提交于 2019-12-12 09:27:08

问题


I have all previleges to the mysql user for the database but still getting below error

[Doctrine\DBAL\DBALException]
An exception occurred while executing 'LOAD DATA LOCAL INFILE '/tmp/uk_insiders/tfo.uktrades_transaction_code.out' IGNORE INTO TABLE uktrades_transaction_codes FIELDS TERMINATED BY '|' (id,description)':

Warning: PDOStatement::execute(): LOAD DATA LOCAL INFILE forbidden in /Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 138

[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: PDOStatement::execute(): LOAD DATA LOCAL INFILE forbidden in /Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php line 138


回答1:


Got the same exception message. I was able to perform this by using PDO dirtectly.




回答2:


I have the same problem... I solve it by replace:

LOAD DATA LOCAL INFILE ...

by

LOAD DATA INFILE

Local option: When this switch is specified the input file will be read from the host the MySQL client is running on instead of the MySQL server. When this switch is not specified, the input file will be read from the data directory on the MySQL host. To use LOAD DATA without the LOCAL switch, the connected user must have the "FILE" privilege.(http://geekology.co.za/article/2010/02/how-to-import-or-export-csv-or-text-files-to-from-mysql-databases)




回答3:


I had tried to pass PDO::MYSQL_ATTR_LOCAL_INFILE parameter through doctrine configuration options parameter, but did not work, This is the solution finally working for me.

    $dbhost = $this->getContainer()->getParameter('database_host');
    $dbuser = $this->getContainer()->getParameter('database_user');
    $dbpass = $this->getContainer()->getParameter('database_password');
    $connParams = $em->getConnection()->getParams();

    $pdoConn = new \PDO('mysql:host=' . $dbhost . ';dbname=' . $connParams['dbname'], $dbuser, $dbpass, array(
        \PDO::MYSQL_ATTR_LOCAL_INFILE => true
    ));

    $sql = "SET FOREIGN_KEY_CHECKS=0";
    $stmt = $pdoConn->prepare($sql);
    $stmt->execute();

    $sql = "LOAD DATA LOCAL INFILE '$file' IGNORE INTO TABLE $tablename FIELDS TERMINATED BY '|' $columns";

    $stmt = $pdoConn->prepare($sql);
    $stmt->execute();



回答4:


The two working answers are workaround :

  • The accepted answer proposes to use a PDO connection
  • Another answer proposes to remove the LOCAL keyword, but it's not always possible

A third, clean solution, is to enable the LOCAL option in Doctrine, but it is a bit tricky :

https://stackoverflow.com/a/24759583/425204

Note : this requires also that LOCAL is enabled in MySQL config on the server : my.cnf



来源:https://stackoverflow.com/questions/18328594/an-exception-occurred-while-executing-load-data-local-infile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!