Why can't I use LOAD DATA LOCAL with PDO even though I can from cli client?

拈花ヽ惹草 提交于 2019-12-29 06:30:12

问题


I recently upgraded my Ubuntu 10.04 dev server to 14.04. It is actually a fresh install. PHP version was 5.4.15, and is now PHP 5.5.9. MySQL went from 5.1.67 to 5.5.37.

I am trying to LOAD LOCAL DATA INFILE on a new server. It works running the command using the local mysql cli client. It does not work when executing using PDO:

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1148 The used command is not allowed with this MySQL version' in /home/ubuntu/mysqltest.php:21
Stack trace:
#0 /home/ubuntu/mysqltest.php(21): PDO->exec('LOAD DATA LOCAL...')
#1 {main}
  thrown in /home/ubuntu/mysqltest.php on line 21

The code that generates the error:

ini_set('mysql.allow_local_infile', 1);
echo "mysql.allow_local_infile: ".ini_get("mysql.allow_local_infile").PHP_EOL;

$options = array(PDO::MYSQL_ATTR_LOCAL_INFILE => 1);
$db = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPassword, $options);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$db->exec("CREATE TEMPORARY TABLE tmp_table ( id INT(4), col1 varchar(128) ) ENGINE MYISAM");

$loadSQL = <<<'EOT'
LOAD DATA LOCAL INFILE '/tmp/test.csv' INTO TABLE `tmp_table` FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' IGNORE 1 LINES (id, col1)
EOT;

$db->exec($loadSQL);

Steps taken so far to address problem:

  • I have added local-infile = 1 to both [mysql] and [mysqld] sections of /etc/mysql/my.cnf
  • I have checked that mysql.allow_local_infile = On is in /etc/php5/cli/php.ini.
  • DB user is root and so has all privileges.
  • /tmp/test.csv does exist and user has full privileges on it.

回答1:


This is a bug in php. Please see https://bugs.php.net/bug.php?id=68226 for explanation and workaround.




回答2:


Installing php5-mysqlnd to replace php5-mysql fixed the problem:

sudo apt-get install php5-mysqlnd

It will automatically remove php5-mysql.

I'm guessing this is a bug either in php5-mysql or the underlying libmysqlclient.



来源:https://stackoverflow.com/questions/23525111/why-cant-i-use-load-data-local-with-pdo-even-though-i-can-from-cli-client

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