memcachedをいれてセッション管理をそっちにする

プログラムがいけないのかもしれないけど、railsアプリでいきなり

Mysql::Error: Lost connection to MySQL server during query: SELECT * FROM `sessions`   WHERE (session_id = 'bfcaa018e1d135fb5f2fc1020d8d7e90')  LIMIT 1

というエラーがでることがあって、ぐぐると結構みんなぶつかってて、けっこうみんな解決できないぽかったので、memcachedを試してみることにした

インストールから起動

yum install memcached
/etc/init.d/memcached start
chkconfig  memcached on

gem install memcache-client

railsの設定

environment.rb

(省略)
Rails::Initializer.run do |config|
(省略)
  config.action_controller.session = {
    :session_key => 'セッションキー',
    :secret      => 'ランダムで充分長い文字列'
  }
  config.action_controller.session_store = :mem_cache_store
(省略)
end
memcache_options = {
  :readonly => false
}
memcache_servers = [ 'IPアドレス:ポート番号' ]
SESSION_CACHE = MemCache.new(memcache_servers,
                             memcache_options.merge(:namespace => "アプリケーション名-session-#{ENV['RAILS_ENV']}"))
ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS.merge!('cache' => SESSION_CACHE)

application.rb
コメント外す

# See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery :secret => 'hogehogehogehoge'