Question

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

Was it helpful?

Solution 3

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

OTHER TIPS

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)

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();

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

To enable the PDO::MYSQL_ATTR_LOCAL_INFILE option for PDO object in Symfony, we can add it into the yml config:

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        options:
            !php/const PDO::MYSQL_ATTR_LOCAL_INFILE: true
        # Skip the rest

Also note how to reference PHP constant here in yml file, and this format is used for Symfony 3.4. For older version, check out Symfony doc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top