LOAD DATA LOCAL INFILE导致mysql2 gem格式错误的数据包错误 [英] LOAD DATA LOCAL INFILE causes Malformed packet error with mysql2 gem

查看:93
本文介绍了LOAD DATA LOCAL INFILE导致mysql2 gem格式错误的数据包错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发出LOAD DATA LOCAL INFILE查询,以在rails 3.1.1下使用mysql2 gem(0.3.11)将一些CSV数据加载到表中:

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

(这是一个示例应用,可重现此github问题的错误) .这会导致在OS X(Lion)上失败,并出现以下错误:

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

在服务器上启用了本地infile:

Local infile is enabled on the server:

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

,并通过application.rb中的此指令在客户端上:

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

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

相同的LOAD语句可从MySQL客户端正常工作.将数据库连接方法从套接字更改为TCP/IP没什么区别. MySql是通过自制程序安装的,版本是

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

在Linux下运行相同的代码,我没有收到此错误.如果我省略LOCAL修饰符,它也可以工作,但这不是一个选择,因为该文件实际上在生产环境中是本地文件,而在数据库服务器中则是远程文件.它与文件权限无关,如问题.

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.

推荐答案

原来,只需重新安装mysql2 gem即可解决.我想我已经使用从源代码编译的MySQL版本安装了它,然后又切换到通过brew安装的MySQL,这导致了gem所使用的客户端代码的不兼容性.这就解释了为什么它可以从命令行客户端运行而不能在使用gem时运行.

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 ...

这篇关于LOAD DATA LOCAL INFILE导致mysql2 gem格式错误的数据包错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆