Rails 应用程序无法连接到 mysql,无法通过套接字 '/var/run/mysqld/mysql.sock' 连接到本地 MySQL 服务器 (2) (Mysql2::Error)

Posted

技术标签:

【中文标题】Rails 应用程序无法连接到 mysql,无法通过套接字 \'/var/run/mysqld/mysql.sock\' 连接到本地 MySQL 服务器 (2) (Mysql2::Error)【英文标题】:Rails application unable to connect to mysql, Can't connect to local MySQL server through socket '/var/run/mysqld/mysql.sock' (2) (Mysql2::Error)Rails 应用程序无法连接到 mysql,无法通过套接字 '/var/run/mysqld/mysql.sock' 连接到本地 MySQL 服务器 (2) (Mysql2::Error) 【发布时间】:2012-07-06 05:55:46 【问题描述】:

我目前陷入了 Rails 项目的(我希望是新手)问题(这是我的第一个 Rails 项目,所以如果我错过任何重要的事情,我深表歉意)。我已经在这里待了几个小时,似乎找不到任何解决方案。我已经尝试了以下线程的解决方案,但无济于事。

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'

名单还在继续,无论如何,有些线程与我的情况相似,其他人只是抓着稻草。让我简要介绍一下我的情况。

项目已经在mac上开发 我最近设置了一个测试服务器来学习部署过程 服务器正在运行 Ubuntu Server 12.04

我设法按照一些教程进行设置,包括 Capistrano 部署教程,直到:

$ rake RAILS_ENV=production db:schema:load

这是我第一次遇到无法连接到本地 MySQL 服务器错误的地方。

如果我使用 --trace 运行它,我会得到以下输出:

** 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

我的database.yml文件如下:

# 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

请注意,我已经更改了套接字变量的位置以与我认为是 ubuntu 上的 .sock 文件相对应,因为这与 mac OSX 上的标准安装不同。

最后,我在运行时也遇到同样的错误

rails c

输出如下:

/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>'

我想指出位置 /usr/share/ruby-rvm/gems/ruby-1.9.2-p318@rails_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb 是有效的.此外,mysql 正在运行,我可以通过以下方式访问它:

mysql -u production -p

【问题讨论】:

在 Ubuntu 服务器上运行“locate mysql.sock”时,是否返回“/var/run/mysqld/mysql.sock”?另外,您确定 Rails 正在加载该 database.yml 文件吗? @Ryan,实际上我确实尝试定位 mysql.sock 但它什么也没返回,这就是我将它连接到 mysqld.sock 的原因,但这只是一个(未受过教育的)猜测。在建立 yml 文件的加载方面,我该如何找到它? 【参考方案1】:

您也可以在 database.yml 中从生产环境中删除套接字规范

production:
  # socket: /var/run/mysqld/mysql.sock

我知道性能略有下降,但它让我遇到了类似的问题。

【讨论】:

这已经奏效了,在注释掉你的建议后,我可以运行 rake 命令,$ rake RAILS_ENV=production db:schema:loadrails c 仍然抛出同样的错误。我现在也在开发部分评论了相同的socket: 规范,并且控制台现在可以正常工作。你能解释一下为什么会这样吗?除了性能问题之外,这种策略还有其他陷阱吗? 据我了解,当数据库和应用程序在同一台服务器上时,您可以通过 Unix 套接字连接到 MySQL。这通常比通过 TCP 连接更快。但是,较大规模的应用程序通常将数据库放在不同的服务器上,在这种情况下使用 TCP。我的意思是,它很可能不引人注意。 @Promethean_Sin rails c 在开发模式下运行控制台。使用rails c production 在生产模式下运行它。这就是为什么您的控制台(在开发模式下)在开发部分取消注释后开始工作的原因。【参考方案2】:

/tmp/mysqld.sock 是 OSX 的套接字位置。

在 linux Mint/Ubuntu 上,它应该位于 /var/run/mysqld/mysql.sock

如果不是,则应在名为my.cnf 的 MySQL 配置文件之一中引用该位置。输入这些文件的位置和加载顺序会打印出来

mysql --help

进入终端。如果在指定路径下找不到my.cnf文件,则不存在,忽略它,按加载顺序查找下一个。

^ 这是验证您的袜子配置的简单、可靠的方法。

Josh Lewis 的回答应该是有效的,因为我的 Rails 4.0.3 安装默认省略了套接字行 - 所以我假设它是由安装预定义或自动配置的,这不是你应该担心的事情。

不是要重提一个老问题,而是要在 Google 搜索中排名第一。

【讨论】:

谢谢,有更多关于此的信息总是很高兴。虽然我已经进入了 Postgresql,但我相信它会在未来对人们有所帮助 这只是救了我.. 谢谢【参考方案3】:

一些随机的想法:

/var/run/mysqld/mysql.sock 的所有者是谁?我知道它在您提到的第一个链接中,但也许您错过了它。运行命令的用户是否可以访问它?

可能默认 MySQL 端口 (3306) 被阻止?我想它可能会阻止 Rails,即使你在本地运行东西,因为在 Unix 设计中,端口无处不在。如果您在 Amazon 上运行,请注意默认情况下它们会关闭所有端口(以及默认情况下会丢弃所有 PING 请求,但这是另一个问题),您可能需要单击其仪表板中的选项

李>

尝试将host: 127.0.0.1port: 3306 添加到您的生产配置设置中

尝试删除 poolreconnect 选项,看看会发生什么(这完全是一个疯狂的猜测)

那里有一些想法,在这种情况下你应该卸载 mysql-clientmysql-server 软件包并重新安装它们,但我没有看到为什么会这样

李>

【讨论】:

不,等等……我想亚马逊阻塞端口应该不是问题。但是您仍然可以检查一下,我对 EC2 没有太多经验,也不知道他们的自定义防火墙在外部或内部是如何工作的。【参考方案4】:

$ mysqld

f 错误“mysql-files”目录

$ sudo mkdir /var/lib/mysql-files

$ sudo chown -R mysql:mysql /var/lib/mysql-files/

$ sudo chmod 700 /var/lib/mysql-files/

【讨论】:

以上是关于Rails 应用程序无法连接到 mysql,无法通过套接字 '/var/run/mysqld/mysql.sock' 连接到本地 MySQL 服务器 (2) (Mysql2::Error)的主要内容,如果未能解决你的问题,请参考以下文章

Rails 应用程序无法通过命名管道/套接字连接到本地 MySQL 服务器 --- Windows 7 x64

Rails 开发 - 无法连接到“localhost”上的 MySQL 服务器 (10061)

将 Rails 连接到 AWS MySQL 数据库

将 Ruby on Rails 连接到现有的 MySQL 数据库(以前安装的 XAMPP)

Hyperion Essbase 11.1.2.2 无法从 Essbase 管理控制台连接到 Essbase 管理员服务器

Rails 服务器正在运行,但无法连接到 localhost:3000