问题
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