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.
connect to local MySQL server through socket
Can't connect to local MySQL server through socket
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Can't connect to local MySQL server through socket '/tmp/mysql.sock'
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.
I managed to follow some tutorials to set it up including the Capistrano deployment tutorial up to the point of:
$ rake RAILS_ENV=production db:schema:load
which was where I first encountered the Can't connect to local MySQL server error.
if I run it with --trace I get the following output:
** 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 <class:Railtie>'
/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 <module:Finisher>'
/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 `<top (required)>'
/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 `<top (required)>'
/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 `<main>'
/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 `<main>'
Tasks: TOP => db:schema:load => environment
my database.yml file is as follows:
# 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
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.
finally, I also get the same error when running
rails c
with the following output:
/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 <class:Railtie>'
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 <module:Finisher>'
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 `<top (required)>'
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 `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
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:
mysql -u production -p
It means either the MySQL server is not installed/running, or the file mysql. sock doesn't exist in /var/lib/mysql/ .
MySQL manages connections to the database server through the use of a socket file, a special kind of file that facilitates communications between different processes. The MySQL server's socket file is named mysqld. sock and on Ubuntu systems it's usually stored in the /var/run/mysqld/ directory.
Ruby on Rails uses SQLite as its database by default, but it also supports the use of MySQL.
You can alternatively remove the socket specification from the production environment in database.yml
production:
# socket: /var/run/mysqld/mysql.sock
I understand performance is reduced slightly, but it got me through a similar problem.
/tmp/mysqld.sock
is the location of the socket for OSX.
On linux Mint/Ubuntu, it should be at /var/run/mysqld/mysql.sock
.
If it's not, then the location should be referenced in one of the MySQL config files, named my.cnf
. The location and load order of these files will print out if you enter
mysql --help
into a terminal. If you can't find the my.cnf
file at the specified path, it doesn't exist, so ignore it, and look to the next one in the load order.
^ That is the simple, surefire way to verify your socks configuration.
Josh Lewis's answer should be valid, as my Rails 4.0.3 install defaults to omitting the socket line - so I'm assuming it's predefined or auto-configured by the installation, and it's not something you should have to worry about.
Not to revive an old question, but it comes up as a top listing on Google search.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With