LOAD DATA LOCAL INFILE causes Malformed packet err

2019-04-29 19:26发布

问题:

I'm trying to issue a LOAD DATA LOCAL INFILE query to load some CSV data into a table using the mysql2 gem (0.3.11) under rails 3.1.1:

class Foo < ActiveRecord::Base
  def self.load_csv
    query = "LOAD DATA LOCAL INFILE 'test/foo.csv' REPLACE INTO TABLE foos LINES TERMINATED BY '\n' (title)"
    ActiveRecord::Base.connection.execute(query)
  end
end

(This is a sample app to reproduce the error for this github issue). This keeps failing on OS X (Lion) with the following error:

Mysql2::Error: Malformed packet: LOAD DATA LOCAL INFILE 'test/foo.csv' REPLACE INTO TABLE foos LINES TERMINATED BY '
' (title)

Local infile is enabled on the server:

mysql> show variables where variable_name like '%local%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| local_infile  | ON    |
+---------------+-------+

and on the client via this directive in application.rb:

Mysql2::Client.default_query_options[:connect_flags] |= Mysql2::Client::LOCAL_FILES

The same LOAD statement works fine from the MySQL client. Changing the DB connection method from socket to TCP/IP makes no difference. MySql is installed via homebrew and the version is

mysql  Ver 14.14 Distrib 5.5.15, for osx10.7 (i386) using readline 5.1

I do NOT get this error running the same code under Linux. It also works if I omit the LOCAL modifier, but that is not an option as the file is in fact local in production and the database server remote. It has nothing to do with file permissions as in this question.

This is driving me nuts, any insights are greatly appreciated.

回答1:

Issue should be fixed for mysql2 v > 0.3.12b4

It did not work for me using LOCAL_FILE flag, but adding :local_infile => true to options did the trick

1.9.3p194 :011 > a = Mysql2::Client.new(:username=>'root', :host=>'localhost', :password=>'', :database=>'bhl_indexer', :local_infile => true)

https://github.com/brianmario/mysql2/issues/293



回答2:

Turns out this is simply solved by re-installing the mysql2 gem. I think I had installed it with a MySQL version compiled from source, and then later switched to MySQL installed via brew, which led to an incompability in the client code used by the gem. That explains why it did work from the command line client but not when using the gem.

gem uninstall mysql2
gem install mysql2

Doh...