12

I've got a Rails 2.3 app that is keeping too many MySQL connections open. After less than a day (at ~400rpm) one process had 83 ESTABLISHED connections to the two mysql servers we use.

We're using the mysql2 gem (0.2.18), and the mysql client is: mysql Ver 14.12 Distrib 5.0.77, for redhat-linux-gnu (i686) using readline 5.1.

How can I troubleshoot where these leaks are happening? In our testing, we're never able to leak connections, its only in production.

In MySQL, we can run show processlist; to see the open connections. On the app server, we can count the number of connections per pid with sudo netstat -ntp | grep 3306 | grep ESTABLISHED | awk '{print $7}' | sort | uniq -c | sort -n.

4

4 に答える 4

7

I fixed this by adding "wait_timeout: 300" to our database.yml. While that does close the unused mysql connections, it doesn't explain where they came from.

于 2013-01-16T21:38:33.373 に答える
2

ランダムなアイデアの1つ:mysql2 gemをフォークし、Mysql2 :: Client#initializeにデバッグを追加して、通常どおりアプリを実行します。クライアントの初期化時にスタックの数行を出力し、リークの原因を突き止めることができます。


于 2013-01-04T21:52:55.657 に答える
0

For what it's worth, the same problem was occurring on our staging server - it was reaching the max_connections number of connections to mysql. I found that running the service directly from the command line rather than using the start up script got around the issue somehow.

I haven't yet found out what it was in the start up script that was causing the problem.

于 2013-11-18T05:59:49.280 に答える
0

I was getting the Too many connections error, because I used establish_connection for accessing other databases in multiple models.

I had these models

class InternetReference < ActiveRecord::Base
  establish_connection :db_webserver
end

class InternetEmployee < ActiveRecord::Base
  establish_connection :db_webserver
end

The solution is to open the connection in an abstract model and inherit from this model:

class AppsWebserver < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :apps_webserver
end

class InternetReference < AppsWebserver
end

class InternetEmployee < AppsWebserver
end

Now the connections are handled correctly by Rails.

于 2015-10-30T10:42:29.770 に答える