2012-07-05 28 views
4

我目前被困在什麼(我希望有一個newby)問題與一個鐵軌項目(這是我的第一個鐵軌項目,所以我很抱歉如果我錯過任何關鍵)。我已經在這個幾個小時,似乎無法找到任何解決方案。我已嘗試解決方案以下線程無濟於事。Rails應用程序無法連接到mysql,無法通過套接字'/var/run/mysqld/mysql.sock'(2)(Mysql2 :: Error)連接到本地MySQL服務器

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'

這樣的例子不勝枚舉,反正一些線程是類似我的情況在其他秸稈只是抓着。讓我簡單介紹一下我的情況。

  • 該項目已在
    • 我最近成立了一個測試服務器作爲學習部署過程的一種手段
    • 服務器運行的是Ubuntu的服務器12.04
  • 的Mac開發

我設法按照一些教程進行設置,包括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/[email protected]_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection' 
/usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:230:in `loop' 
/usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:229:in `checkout' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:95:in `connection' 
/usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_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/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:308:in `clear_cache!' 
/usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:418:in `_run__427268845__prepare__407057367__callbacks' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `instance_exec' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `run' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:55:in `block in run_initializers' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `each' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `run_initializers' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/application.rb:136:in `initialize!' 
/usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `block in require' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:236:in `load_dependency' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/application.rb:103:in `require_environment!' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/application.rb:292:in `block (2 levels) in initialize_tasks' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute' 
/usr/share/ruby-rvm/gems/[email protected]/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/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:176:in `block in invoke_prerequisites' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `each' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:174:in `invoke_prerequisites' 
/usr/share/ruby-rvm/gems/[email protected]/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/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run' 
/usr/share/ruby-rvm/gems/[email protected]/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>' 
/usr/share/ruby-rvm/gems/[email protected]/bin/rake:19:in `load' 
/usr/share/ruby-rvm/gems/[email protected]/bin/rake:19:in `<main>' 
/usr/share/ruby-rvm/gems/[email protected]_3_2/bin/ruby_noexec_wrapper:14:in `eval' 
/usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_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/[email protected]_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `initialize' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new' 
    from /usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_3_2/gems/activerecord-3.2.2/lib/active_record/model_schema.rb:308:in `clear_cache!' 
    from /usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_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/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:405:in `__run_callback' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/actionpack-3.2.2/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `instance_exec' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:30:in `run' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:55:in `block in run_initializers' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `each' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/initializable.rb:54:in `run_initializers' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/application.rb:136:in `initialize!' 
    from /usr/share/ruby-rvm/gems/[email protected]_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/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `block in require' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:236:in `load_dependency' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/activesupport-3.2.2/lib/active_support/dependencies.rb:251:in `require' 
    from /usr/share/ruby-rvm/gems/[email protected]_3_2/gems/railties-3.2.2/lib/rails/application.rb:103:in `require_environment!' 
    from /usr/share/ruby-rvm/gems/[email protected]_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 /共享/紅寶石RVM /gems/[email protected]_3_2/gems/mysql2-0.3.11/lib/mysql2/client.rb是有效的。此外,MySQL正在運行,我可以訪問它:

mysql -u production -p 
+0

當您運行Ubuntu的服務器上的 「定位的mysql.sock」,它返回「/ var/run中/ MySQL的d /的mysql.sock「?另外,你確定Rails正在加載那個database.yml文件嗎? – sigre 2012-07-05 20:21:18

+0

@Ryan,其實我試着找到mysql.sock,但它沒有任何迴應,這就是爲什麼我把它連接到mysqld.sock,但這只是一個(未受教育的)猜測。在建立yml文件的加載方面,我將如何發現? – DazBaldwin 2012-07-05 22:31:24

回答

11

您也可以從生產環境在database.yml中

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

我明白性能卸下插槽規格略有減少,但它讓我通過類似的問題。

+0

這已經奏效了,在註釋掉你的建議之後,我可以運行rake命令'$ rake RAILS_ENV = production db:schema:load',但'rails c'仍然拋出相同的錯誤。我現在在開發部分中也註釋了相同的'socket:'規範,並且控制檯現在可以正常工作。你能解釋爲什麼這個工作嗎?除了這個策略之外,還有其他一些缺陷,除了性能問題嗎? – DazBaldwin 2012-07-06 10:55:18

+0

據我所知,當數據庫和應用程序在同一臺服務器上時,您可以通過Unix套接字連接到MySQL。這通常比通過TCP連接更快。但是,較大規模的應用程序通常將數據庫放在不同的服務器上,在這種情況下使用TCP。我的觀點是,這很可能不明顯。 – 2012-07-06 14:07:07

+1

@Promethean_Sin'rails c'以開發模式運行控制檯。使用'rails c production'在生產模式下運行它。這就是爲什麼您的控制檯(在開發模式下)在開發部分取消註釋之後開始工作的原因。 – 2012-07-06 15:34:45

0

一些隨機的想法:

  • 誰是/var/run/mysqld/mysql.sock主人?我知道這是你提到的第一個鏈接,但也許你錯過了它。是否可以從您運行命令的用戶訪問?

  • 也許默認的MySQL端口(3306)被阻止?我猜想它可能會阻止Rails,即使在本地運行時也是如此,因爲在Unix中,設計端口無處不在。如果您在亞馬遜上運行,請注意,他們保持默認關閉所有端口(並且默認情況下會丟棄所有PING請求,但那是另一個問題),您可能需要點擊其儀表板上的選項。

  • 嘗試添加host: 127.0.0.1port: 3306到生產配置設置

  • 嘗試刪除poolreconnect選項,並看看會發生什麼(這是一個完全瞎猜)

  • 有一些想法在那裏,在這種情況下,你應該卸載mysql-clientmysql-server包,再次安裝它們,但我從來沒見過一個點,爲什麼是

+0

不,等等......我想,亞馬遜阻塞端口應該不是問題。但是你仍然可以檢查,我沒有太多的EC2經驗,也不知道他們的定製防火牆是如何在外部或內部工作的。 – 2012-07-05 20:48:16

3

/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搜索的首選列表。

+0

謝謝,總是有很多關於這方面的信息。雖然我已經進入Postgresql,但我確信它會幫助未來的人 – DazBaldwin 2014-02-28 14:38:23

+0

這只是救了我..謝謝 – Cheyne 2014-08-22 15:22:19

0

$ mysqld的

F錯誤 「的mysql-文件」 DIR

$ sudo的MKDIR在/ var/lib中/ MySQL的檔案

$ sudo的喬敦 - R的MySQL的:MySQL的在/ var/LIB/MySQL的-文件/

$ sudo的搭配chmod 700的/ var/lib中/ MySQL的-文件/

相關問題