简体   繁体   English

Rails - 在文本列中插入超过7786个字符时,获取'Mysql服务器已消失'

[英]Rails - Get 'Mysql server has gone away' when inserting more than 7786 caracters into text column

I have a 'blog_articles' class which contains a text column named 'content'. 我有一个'blog_articles'类,其中包含一个名为'content'的文本列。 Here is the migration : 这是迁移:

create_table :blog_articles do |t|
  t.references :blog_user
  t.string :title
  t.text :summary
  t.text :content

  t.boolean :published, :default => false
  t.timestamps
end

I also have a custom rake task rake db:rebuild , which executes drop , create , migrate then seed . 我还有一个自定义rake任务rake db:rebuild ,它执行dropcreatemigrate然后seed Here is the rake task : 这是rake任务:

namespace :db do
  desc "run db:drop, db:create, db:migrate and db:seed tasks in a row."
  task :rebuild => :environment do
    if Rails.env == "production"
      # Do nothing
    else
      Rake::Task["db:drop"].invoke
      Rake::Task["db:create"].invoke
      Rake::Task["db:migrate"].invoke
      Rake::Task["db:seed"].invoke
    end
  end
end

-- -

So my problem is, when I try to insert an article (in the seeds.rb file), with a content of more than 7768 caracters, I get a Mysql2::Error: MySQL server has gone away : INSERT INTO 'blog_articles' [...] error, using the rake db:rebuild task. 所以我的问题是,当我尝试插入一篇文章(在seeds.rb文件中),内容超过7768个caracters时,我得到一个Mysql2::Error: MySQL server has gone away : INSERT INTO 'blog_articles' [...]错误,使用rake db:rebuild任务。

There is no problem at all if I run the rake db:seed manualy, or if I create the article in a controller. 如果我运行rake db:seed manualy,或者如果我在控制器中创建文章,则完全没有问题。

How can I fix this ? 我怎样才能解决这个问题 ? I use a lot of my new db:rebuild task. 我使用了很多新的db:rebuild任务。


edit : 编辑:

So, I solve my problem by changing my rebuild task to this 所以,我通过将重建任务更改为此来解决我的问题

namespace :db do
  desc "run db:drop, db:create, db:migrate and db:seed tasks in a row."
  task :rebuild => :environment do
    if Rails.env == "production"
      # Do nothing
    else      
      system "rake db:drop"
      system "rake db:create"
      system "rake db:migrate"
      system "rake db:seed"
    end
  end
end

I don't understand what difference it makes but this works. 我不明白它有什么不同,但这有效。 Does anyone have an explication ? 有人有解释吗?

Try modifying your my.cnf , increasing max_allowed_packet 尝试修改my.cnf ,增加max_allowed_packet

[mysqld]
max_allowed_packet=512M

http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html

When a MySQL client or the mysqld server receives a packet bigger than max_allowed_packet bytes, it issues an ER_NET_PACKET_TOO_LARGE error and closes the connection. 当MySQL客户端或mysqld服务器收到大于max_allowed_pa​​cket字节的数据包时,它会发出ER_NET_PACKET_TOO_LARGE错误并关闭连接。 With some clients, you may also get a Lost connection to MySQL server during query error if the communication packet is too large. 对于某些客户端,如果通信包太大,您在查询错误期间也可能会失去与MySQL服务器的连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM