title: Rails application unable to connect to mysql, Can't connect to local MySQL server through socket '/var/run/mysqld/mysql.sock' (2) (Mysql2::Error)
body: <p>I'm currently stuck on what is (I hope a newby) problem with a rails project (this is my first rails project and so I apologise if I miss anything crucial). I've been at this for a couple of hours and can't seem to find any solutions. I have tried the solutions to the following threads with no avail.</p>
  
  <p><a href="https://stackoverflow.com/questions/6525917/connect-to-local-mysql-server-through-socket">connect to local MySQL server through socket</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2182919/cant-connect-to-local-mysql-server-through-socket">Can't connect to local MySQL server through socket</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7843731/cant-connect-to-local-mysql-server-through-socket-tmp-mysql-sock-2">Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7310967/cant-connect-to-local-mysql-server-through-socket-tmp-mysql-sock">Can't connect to local MySQL server through socket '/tmp/mysql.sock'</a></p>
  
  <p>The list goes on, anyway some of the threads are similar to my situation others are just clutching at straws. Let me give you a quick overview of my situation.</p>
  
  <ul>
  <li>The project has been developed on a mac
  
  <ul>
  <li>I have recently set up a test server as a means of learning the deployment process</li>
  <li>The server is running Ubuntu Server 12.04</li>
  </ul></li>
  </ul>
  
  <p>I managed to follow some tutorials to set it up including the <a href="https://github.com/capistrano/capistrano/wiki/2.x-From-The-Beginning" rel="nofollow noreferrer">Capistrano</a> deployment tutorial up to the point of:</p>
  
  <pre><code>$ rake RAILS_ENV=production db:schema:load
  </code></pre>
  
  <p>which was where I first encountered the Can't connect to local MySQL server error.</p>
  
  <p>if I run it with --trace I get the following output:</p>
  
  <pre><code>** Invoke db:schema:load (first_time)
  ** Invoke environment (first_time)
  ** Execute environment
  rake aborted!
  Can't connect to local MySQL server through socket '/var/run/mysqld/mysql.sock' (2)
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:277:in `new_connection'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:287:in `checkout_new_connection'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:235:in `block (2 levels) in checkout'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in `loop'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in `block in checkout'
  /usr/share/ruby-rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:308:in `clear_cache!'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/railtie.rb:91:in `block (2 levels) in &lt;class:Railtie&gt;'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:418:in `_run__427268845__prepare__407057367__callbacks'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/application/finisher.rb:47:in `block in &lt;module:Finisher&gt;'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `instance_exec'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `run'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:55:in `block in run_initializers'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `each'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `run_initializers'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/application.rb:136:in `initialize!'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
  /var/www/little_oven_deploy/releases/20120705162026/config/environment.rb:5:in `&lt;top (required)&gt;'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `block in require'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:236:in `load_dependency'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/application.rb:103:in `require_environment!'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/application.rb:292:in `block (2 levels) in initialize_tasks'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
  /usr/share/ruby-rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:157:in `block in invoke_with_call_chain'
  /usr/share/ruby-rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/gems/rake-0.9.2.2/bin/rake:33:in `&lt;top (required)&gt;'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/bin/rake:19:in `load'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@global/bin/rake:19:in `&lt;main&gt;'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/bin/ruby_noexec_wrapper:14:in `eval'
  /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/bin/ruby_noexec_wrapper:14:in `&lt;main&gt;'
  Tasks: TOP =&gt; db:schema:load =&gt; environment
  </code></pre>
  
  <p>my database.yml file is as follows:</p>
  
  <pre><code># MySQL.  Versions 4.1 and 5.0 are recommended.
  #
  # Install the MYSQL driver
  #   gem install mysql2
  #
  # Ensure the MySQL gem is defined in your Gemfile
  #   gem 'mysql2'
  #
  # And be sure to use new-style password hashing:
  #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
  development:
    adapter: mysql2
    encoding: utf8
    reconnect: false
    database: little_oven_devel
    pool: 5
    username: devel
    password: Development1
    socket: /var/run/mysqld/mysql.sock
  
  # Warning: The database defined as "test" will be erased and
  # re-generated from your development database when you run "rake".
  # Do not set this db to the same as development or production.
  test:
    adapter: mysql2
    encoding: utf8
    reconnect: false
    database: little_oven_test
    pool: 5
    username: root
    password:
    socket: /tmp/mysql.sock
  
  production:
    adapter: mysql2
    encoding: utf8
    reconnect: false
    database: little_oven_production
    pool: 5
    username: production
    password: Production1
    socket: /var/run/mysqld/mysql.sock
  </code></pre>
  
  <p>Note that I have changed the location of the socket variable to correspond with what I believe is the .sock file on ubuntu as this differs from a standard install on mac OSX.</p>
  
  <p>finally, I also get the same error when running</p>
  
  <pre><code>rails c
  </code></pre>
  
  <p>with the following output:</p>
  
  <pre><code>/usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Can't connect to local MySQL server through socket '/var/run/mysqld/mysql.sock' (2) (Mysql2::Error)
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:277:in `new_connection'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:287:in `checkout_new_connection'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:235:in `block (2 levels) in checkout'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in `loop'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in `block in checkout'
      from /usr/share/ruby-rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:308:in `clear_cache!'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activerecord-3.2.2/lib/active_record/railtie.rb:91:in `block (2 levels) in &lt;class:Railtie&gt;'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:418:in `_run__485859349__prepare__1069885904__callbacks'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/application/finisher.rb:47:in `block in &lt;module:Finisher&gt;'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `instance_exec'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `run'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:55:in `block in run_initializers'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `each'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `run_initializers'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/application.rb:136:in `initialize!'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/railtie/configurable.rb:30:in `method_missing'
      from /var/www/little_oven_deploy/releases/20120705162026/config/environment.rb:5:in `&lt;top (required)&gt;'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `block in require'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:236:in `load_dependency'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/application.rb:103:in `require_environment!'
      from /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/railties-3.2.2/lib/rails/commands.rb:40:in `&lt;top (required)&gt;'
      from script/rails:6:in `require'
      from script/rails:6:in `&lt;main&gt;'
  </code></pre>
  
  <p>I would like to point out that the location /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb is valid. Also, mysql is running and I can access it with:</p>
  
  <pre><code>mysql -u production -p
  </code></pre>