开发者

Can't connect to local MySQL server through socket '/tmp/mysql.sock'

开发者 https://www.devze.com 2023-04-03 07:10 出处:网络
I am brand-new to web development and RoR - an uber-noob! - and I\'m 开发者_开发问答taking an online RoR course via lynda.com.Excited to join the StackOverflow community, and would really appreciate s

I am brand-new to web development and RoR - an uber-noob! - and I'm 开发者_开发问答taking an online RoR course via lynda.com. Excited to join the StackOverflow community, and would really appreciate some help with a roadblock.

MySQL and localhost were working fine for several weeks as I set up my rudimentary app and successfully ran some migrations, but today I am getting this MySQL error when I launch my rails server and view on http://localhost:3000/ in my browser:

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

I don't think I've changed anything relevant in my environment, so I wonder if I need to update gems or something simple like that?

I've hunted around on Google and StackOverflow for a couple hours now and still can't figure out what went wrong here and how to fix it. Other solutions seem to relate to new set-ups as opposed to a working set-up that inexplicably breaks.

Here's some context:


From my 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

I saved a command line from a couple weeks ago when MySQL and localhost were working fine:

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

etc... (worked fine from here)


But then today I tried the same commands and receive the error:

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)

So I went back to the database notes from my training and tried connecting to MySQL from my command line which had worked before but no longer:

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$ 

One StackOverflow thread gave me this clue but it only seems to reinforce that my set up is correct.

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

As I said, I am a total beginner so any help would be much appreciated, thanks!


This question is somewhat dated, but it I came across it while researching a similar issue. Confirming the socket location and name with the mysql_config command was instrumental in resolving my problem.

I was getting the same error and had adjusted the socket entry path in config/database.yml to match where I found the .sock file.

However beyond the path change, the socket entry in my database.yml file referred to "mysql.sock", as do most of the discussions related to this issue during my search.

The output of mysql_config was slightly different. It revealed that the file name was "mysqld.sock"...notice the 'd', not mysql.sock. I missed it because I was focussing on the path.

The mysql installation is on a server only VPS, no client, which may explain the difference in the socket file name.


Update: I tried again today without having changed anything at all, and it worked fine. Go figure. Wish I knew what happened, though.


I run on the same error and for me the both socket information has shown mysqld. I had two sessions defined in mysql client. I resolved my problem by deleting one session definition and leaving only localhost. After deleting and re-running the application, the connection worked.

I don't know how it is connected to one another, but it worked, so it might work for someone else too.


rename CLEARDB_DATABASE_URL to DATABASE_URL in heroku configs


I had the same error and installing mysql again (from the official website) fixed the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消