1

I am running some rails app on osx, but when I launch a rails generate command type, I get this message :

/$root/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)

For info, my mamp server is running, and the connection must pass through /Applications/MAMP/Library/bin/mysql, so I aliased it by setting in my bash profile :

alias mysql="/Applications/MAMP/Library/bin/mysql"

Btw, what is weird, is that my rails s command works perfectly, so my app can connect to the apache server in that case

epsilones
  • 11,279
  • 21
  • 61
  • 85

2 Answers2

4

So the trick was to simply /tmp/mysql.sock to /Applications/MAMP/tmp/mysql/mysql.sock

ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
epsilones
  • 11,279
  • 21
  • 61
  • 85
1

The error indicates that "mysql is not started/running". From what you describe, looks like the new location pointed to the alias has no mysql or its mysql is not started.

Look my.cnf file with the correct parameters in the new location: /Applications/MAMP/Library/bin/mysql

Another trick is to list your running processes and look for mysql. Also, are you sure, "rails c" is not using the sqlite3 vs mysql?

Updated:

*Courtesy of @bfavaretto MySQL my.cnf location on OS X?

By default, the OS X installation does not use a my.cnf, and MySQL just uses the default values. To set up your own my.cnf, you could just create a file straight in /etc.

OS X provides example configuration files at /usr/local/mysql/support-files/

Update:
Take a look at this: https://stackoverflow.com/questions/4788381/getting-cant-connect-through-socket-tmp-mysql-when-installing-mysql-on-m

Community
  • 1
  • 1
salah-1
  • 1,299
  • 11
  • 15
  • my rails app is for sure using mysql. Btw, I can't find any my.cnf /Applications/MAMP/MAMP.app/Contents/Resources (I am not really used to osx, coming from ubuntu rivers...) Were can I find it ? – epsilones Jun 24 '13 at 18:57
  • 1
    see updated answer. I think its just missing the correct location/pointers to your working mysql – salah-1 Jun 24 '13 at 19:01
  • thanks but in the post you indicated, this folder /usr/local/mysql/support-files/ is missing... – epsilones Jun 24 '13 at 19:06
  • btw, for information when I suppress alias mysql="/Applications/MAMP/Library/bin/mysql", rails s doesn't work. So with this alias it seems to work, but only for rails s – epsilones Jun 24 '13 at 19:07
  • oauh it is really weird, if I shut down the apache server and I run rails s, then it throughs that Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock', so it is wright because it recognizes the alias, but when I launch rails generate scaffold, I get Can't connect to local MySQL server through socket '/tmp/mysql.sock' ... – epsilones Jun 24 '13 at 19:13
  • in "/etc/my.cnf" can you create "my.cnf" and put some values. Then in socket value point to /tmp/mysql.sock...(you just posted the path) – salah-1 Jun 24 '13 at 19:15
  • So I created such a file and added this, but it still doesn't work [client] port=3306 socket=/Applications/MAMP/tmp/mysql/mysql.sock [mysqld] port=3306 socket=/Applications/MAMP/tmp/mysql/mysql.sock key_buffer_size=16M max_allowed_packet=8M – epsilones Jun 24 '13 at 19:24
  • ok now the problem is more clear, I am going to post a clearer question – epsilones Jun 24 '13 at 20:27
  • Maybe link if you start new question or answer close out this one – salah-1 Jun 25 '13 at 15:27