Lost connection to MySQL server during query on ra

2019-07-16 12:51发布

FINAL UPDATE: We fixed this problem by finding a way to accomplish our goals without forking. But forking was the cause of the problem.

---Original Post---

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.

during business hours we get a handful of these from no particular query.

ActiveRecord::StatementInvalid: Mysql2::Error: Lost connection to MySQL server during query

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.

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.

Our servers are configured by cloud66.

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.

this also happens to me on localhost while developing features sometimes, so I don't believe it's a load issue.

We're running the following:

  • ruby 2.2.5
  • rails 4.2.6
  • mysql2 0.4.8

UPDATE: per the first answer below I increased our max_connections variable to 500 last night, and confirmed the increase via show global variables like 'max_connections';

I'm still getting dropped connection, the first one today was dropped only a few minutes ago.... ActiveRecord::StatementInvalid: Mysql2::Error: Lost connection to MySQL server during query

I ran select * from information_schema.processlist; 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?

UPDATE: I just set net_read_timeout = 60 (it was 30 before) I'll see if that helps

UPDATE: It didn't help, I'm still looking for a solution...

Heres my Database.yml with credentials removed.

production:
  adapter: mysql2
  encoding: utf8
  host: localhost
  database:
  username: 
  password: 
  port: 3306
  reconnect: true

8条回答
祖国的老花朵
2楼-- · 2019-07-16 13:18

Find out if your database is limited in terms of multiple connections. Because normally a SQL database is supposed to have more than one active connection. (Contact your network provider)

查看更多
\"骚年 ilove
3楼-- · 2019-07-16 13:19

Things you did not mention but you should take a look:

  • Are you using unicorn? If so, are your reconnecting and disconnecting in your after_fork and before_fork?
  • Is reconnect: true set in your database.yml configuration?
查看更多
登录 后发表回答