Question

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.
Was it helpful?

Solution 2

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

OTHER TIPS

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.

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