/tmp/mysql.sock issue with Ruby On Rails under mac os X

I was retaking my self taught course of RoR yesterday and I started getting this error when accessing the database, each time I wanted to generate the scaffolding of one model:

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

Oh well, I thought, aggghh. When I tried to install php, ror, mysql, etc… I did a great mess here, and although I hadn’t had any problem with php (once I installed xampp), seems like I have a separate mysql installation (the remaining of the first, separated one, I tried first).

After trying to find out what was the problem with that socket, specially because it shouldn’t look at that file, but at one under xampp files, I found a quicker solution:

In the config/database.yml file, I replaced all appearances of localhost with 127.0.0.1.

And voila, it started working. Although it’s not very efficient, as it makes every connection to go through TCP, and not via a socket, as it would do if the host was localhost.

I think I’ll have to take my time once and clean this horrible mess, but by the moment it works.

7 Replies to “/tmp/mysql.sock issue with Ruby On Rails under mac os X”

  1. Try –

    development:

    socket: /var/run/mysqld/mysqld.sock

    and comment the host/port entries. The actual socket file varies with how the mysql install was configured. The mysql adapter of RoR defaults to “/tmp/mysql.sock” but many linux distros or packaged installers of mysql change it to something more serious. Look for the “socket =” entry in your mysql config file (/etc/mysql/my.cnf or something similar.) Local file I/O is much faster than TCP even on localhost, so it is recomended to never use TCP when the both servers are on the same machine.

  2. Exactly, I was thinking that it had to be something of mysql in RoR defaulting to that, and I tried to find out if there was anywhere else where it could be changed.
    Very good spot! I didn’t know it was possible to assign it like that in the .yml file.

    Well if somebody has the problem with xampp, I used this socket:
    socket: /Applications/xampp/xamppfiles/var/mysql/mysql.sock

    Thanks ccm! 🙂

  3. Thank you SO much !

    It’s taken me about 4 days to get my Apache-Red Hat-MySQL box running Rails properly: the “socket” line in database.yml was the last piece which made it run.

    🙂

  4. Hello !! I am using XAMPP for developing my project.bt i am getting the same problem as asked..bt as per ur instruction i find nthing as database.yml in the whole lampp directory. one more problem with this is while connecting PHP with Mysql if i use localhost then it works fine nd if i give my machines ip address then it gives error as it cant connect. Plz Help Me…

  5. Kunal, that is for Ruby On Rails if you happen to use the same mysql server from Xampp.

    Also, you can use “but” instead of “bt” and so on, it will make way more sense.

Comments are closed.