【问题标题】:Can't connect to local MySQL server through socket '/tmp/mysql.sock'无法通过套接字“/tmp/mysql.sock”连接到本地 MySQL 服务器
【发布时间】:2011-11-10 18:39:00
【问题描述】:

我是 Web 开发和 RoR 的新手 - 超级菜鸟! - 我正在通过 lynda.com 参加在线 RoR 课程。很高兴加入 StackOverflow 社区,并且非常感谢您在路障方面的一些帮助。

MySQL 和 localhost 在我设置我的基本应用程序并成功运行了一些迁移时工作了好几个星期,但是今天当我启动我的 Rails 服务器并在浏览器中查看 http://localhost:3000/ 时,我收到了这个 MySQL 错误:

Mysql2::Error (Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)):

我认为我没有更改与我的环境相关的任何内容,所以我想知道是否需要更新 gems 或类似的简单内容?

我已经在 Google 和 StackOverflow 上搜索了几个小时,但仍然无法弄清楚这里出了什么问题以及如何解决它。其他解决方案似乎与新设置有关,而不是莫名其妙地中断的工作设置。

这里有一些上下文:


来自我的 config/database.yml:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: simple_cms_development
  pool: 5
  username: simple_cms
  password: XXXXXXXXX
  socket: /tmp/mysql.sock

我保存了几周前 MySQL 和 localhost 工作正常时的命令行:

Last login: Mon Aug 15 21:34:23 on ttys000
sdemessieres$ cd sites/simple_cms
sdemessieres$ pwd
/Users/sdemessieres/sites/simple_cms
sdemessieres$ rails server
=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-08-22 22:28:01] INFO  WEBrick 1.3.1
[2011-08-22 22:28:01] INFO  ruby 1.8.7 (2009-06-12) [universal-darwin10.0]
[2011-08-22 22:28:01] INFO  WEBrick::HTTPServer#start: pid=66952 port=3000


Started GET "/" for 127.0.0.1 at Mon Aug 22 22:28:20 -0400 2011
  Processing by DemoController#index as HTML

等...(从这里工作正常)


但是今天我尝试了相同的命令并收到错误:

Last login: Mon Sep  5 12:13:22 on ttys000
sdemessieres$ cd sites/simple_cms
sdemessieres$ pwd
/Users/sdemessieres/sites/simple_cms
sdemessieres$ rails server
=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-09-05 12:33:09] INFO  WEBrick 1.3.1
[2011-09-05 12:33:09] INFO  ruby 1.8.7 (2009-06-12) [universal-darwin10.0]
[2011-09-05 12:33:09] INFO  WEBrick::HTTPServer#start: pid=11027 port=3000


Started GET "/" for 127.0.0.1 at Mon Sep 05 12:33:41 -0400 2011

Mysql2::Error (Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)):


Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.7ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (6.3ms)

所以我回到了培训中的数据库笔记,并尝试从我的命令行连接到 MySQL,这在以前可以工作但不再有效:

sdemessieres$ pwd
/Users/sdemessieres/sites/simple_cms
sdemessieres$ which mysql
/usr/local/mysql/bin/mysql
sdemessieres$ mysql --version
mysql  Ver 14.14 Distrib 5.5.13, for osx10.6 (i386) using readline 5.1
sdemessieres$ mysql -u root -p
Enter password: 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
sdemessieres$ 

一个 StackOverflow thread 给了我这个线索,但它似乎只是强化了我的设置是正确的。

sdemessieres$ mysql_config |grep -- --socket
        --socket         [/tmp/mysql.sock]

正如我所说,我是一个完全的初学者,所以任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: mysql ruby-on-rails-3


    【解决方案1】:

    这个问题有点过时了,但我在研究类似问题时遇到了它。使用mysql_config 命令确认套接字位置和名称有助于解决我的问题。

    我遇到了同样的错误,并调整了config/database.yml 中的套接字入口路径以匹配我找到.sock 文件的位置。

    但是除了路径更改之外,我的database.yml 文件中的套接字条目引用了"mysql.sock",在我的搜索过程中与此问题相关的大多数讨论也是如此。

    mysql_config 的输出略有不同。它显示文件名是"mysqld.sock"...注意'd',而不是mysql.sock。我错过了它,因为我专注于道路。

    mysql 安装在服务器上,只有 VPS,没有客户端,这可以解释套接字文件名的差异。

    【讨论】:

    • 这很有帮助。感觉像是新手的错误,但是是的,您可能很容易错过检查mysqld.sock 文件路径中的d...
    【解决方案2】:

    更新:我今天又试了一次,根本没有改变任何东西,效果很好。去搞清楚。不过,我希望我知道发生了什么。

    【讨论】:

    • 请编辑您自己的问题。不要以编辑的身份回答自己!
    • @Mr.DDD 虽然我同意这个原则,但这也是这个问题的自我回答,我称之为“有效”-ish。但是,在这种情况下,我会简单地删除问题。
    • 我不会删除这个问题。对于其他可能意识到如果他们稍等片刻并再次运行该命令,它就会起作用的人,这个答案可能会有所帮助。
    • 这不是一个有用的答案。看起来它更适合作为评论。
    【解决方案3】:

    我运行同样的错误,对我来说,两个套接字信息都显示mysqld。 我在 mysql 客户端中定义了两个会话。我通过删除一个会话定义并只留下localhost 解决了我的问题。删除并重新运行应用程序后,连接正常。

    我不知道它是如何相互连接的,但它确实有效,所以它可能也适用于其他人。

    【讨论】:

      【解决方案4】:

      在 Heroku 配置中将 CLEARDB_DATABASE_URL 重命名为 DATABASE_URL

      【讨论】:

        【解决方案5】:

        我遇到了同样的错误,再次安装mysql(从官方网站)解决了这个问题。

        【讨论】:

          猜你喜欢
          • 2012-07-20
          • 2011-12-12
          • 2013-01-27
          • 2019-02-28
          • 1970-01-01
          • 2014-03-28
          • 2013-11-12
          相关资源
          最近更新 更多