title: Lost connection to MySQL server during query on random simple queries
body: <p>FINAL UPDATE: We fixed this problem by finding a way to accomplish our goals without forking. But forking was the cause of the problem.</p>
  
  <p>---Original Post---</p>
  
  <p>I'm running a ruby on rails stack, our mysql server is separate, but housed at the same site as our app servers. (we've tried swapping it out for a different mysql server with double the specs, but no improvement was seen.</p>
  
  <p>during business hours we get a handful of these from no particular query.</p>
  
  <pre><code>ActiveRecord::StatementInvalid: Mysql2::Error: Lost connection to MySQL server during query
  </code></pre>
  
  <p>most of the queries that fail are really simple, and there seems to be no pattern between one query and another. This all started when I upgraded from Rails 4.1 to 4.2.</p>
  
  <p>I'm at a loss as to what to try. Our database server is less than 5% CPU throughout the day. I do get bug reports from users who have random interactions fail due to this, so it's not queries that have been running for hours or anything like that, of course when they retry the exact same thing it works.</p>
  
  <p>Our servers are configured by cloud66.</p>
  
  <p>So in short: our mysql server is going away for some reason, but it's not because of lack of resources, it's also a brand new server as we migrated from another server when this problem started.</p>
  
  <p>this also happens to me on localhost while developing features sometimes, so I don't believe it's a load issue.</p>
  
  <p>We're running the following:</p>
  
  <ul>
  <li>ruby 2.2.5</li>
  <li>rails 4.2.6</li>
  <li>mysql2 0.4.8</li>
  </ul>
  
  <p>UPDATE: per the first answer below I increased our max_connections variable to 500 last night, and confirmed the increase via
  <code>show global variables like 'max_connections';</code></p>
  
  <p>I'm still getting dropped connection, the first one today was dropped only a few minutes ago....
  <code>ActiveRecord::StatementInvalid: Mysql2::Error: Lost connection to MySQL server during query</code></p>
  
  <p>I ran <code>select * from information_schema.processlist;</code> and I got 36 rows back. Does this mean my app servers were running 36 connections at that moment? or can a process be multiple connections?</p>
  
  <p>UPDATE: I just set net_read_timeout = 60 (it was 30 before) I'll see if that helps</p>
  
  <p>UPDATE: It didn't help, I'm still looking for a solution...</p>
  
  <p>Heres my Database.yml with credentials removed.</p>
  
  <pre><code>production:
    adapter: mysql2
    encoding: utf8
    host: localhost
    database:
    username: 
    password: 
    port: 3306
    reconnect: true
  </code></pre>