Ruby on Rails 3 Cannot connect to local MySQL server through socket "/tmp/mysql.sock" in OSX

I have a standard Rails3 environment, RVM 1.2.9, Rails 3.0.5, Ruby 1.9.2p180, MySQL2 Gem 0.2.7, mysql-5.5.10-osx10.6-x86_64

Error starting rake db:migrate to create the database:

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

config / database.yml has

 development: adapter: mysql2 host: localhost username: root password: xxxx database: xxxx 

sure itโ€™s simple that i'm missing.

+74
mysql ruby-on-rails-3 macos
Mar 31 '11 at 11:24
source share
13 answers

First, to find the socket file:

 mysqladmin variables | grep socket 

For me it gives:

 | socket | /tmp/mysql.sock | 

Then add the line to config/database.yml :

 development: adapter: mysql2 host: localhost username: root password: xxxx database: xxxx socket: /tmp/mysql.sock 
+190
Jun 20 2018-11-11T00:
source share

Found it!

Change host: localhost in config / database.yml to host: 127.0.0.1 so that the rails connect via TCP / IP instead of the local socket.

 development: adapter: mysql2 host: 127.0.0.1 username: root password: xxxx database: xxxx 
+73
Mar 31 '11 at 11:34
source share

Maybe your mysql server is not working. The following explains how to start the server. This is an excerpt from the README file that comes with mysql download.

After installation, you can start MySQL by running the following commands in a terminal window. You must have administrator privileges to complete this task.

If you installed a startup item, use the following command:

  shell> sudo /Library/StartupItems/MySQLCOM/MySQLCOM start (ENTER YOUR PASSWORD, IF NECESSARY) (PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL) 

If you are not using the Startup Item, enter the following sequence of commands:

  shell> cd /usr/local/mysql shell> sudo ./bin/mysqld_safe (ENTER YOUR PASSWORD, IF NECESSARY) (PRESS CONTROL-Z) shell> bg (PRESS CONTROL-D OR ENTER "EXIT" TO EXIT THE SHELL) 
+10
Jan 19 2018-12-12T00:
source share

"/tmp/mysql.sock" will be created automatically when the MySQL server starts. Therefore, be sure to do this before starting the rails server.

+6
Jun 20 '11 at 1:30 p.m.
source share

With my installation of MAMP on OSX, the MySQL socket is in /Applications/MAMP/tmp/mysql/mysql.sock . I used locate mysql.sock to find the location of my socket in MySQL.

So my config/database.yml looks like this:

 development: adapter: mysql2 host: localhost username: root password: xxxx database: xxxx socket: /Applications/MAMP/tmp/mysql/mysql.sock 
+3
Apr 11 '13 at 2:32
source share

If you are on Mac OSX,

The default location for the MySQL Unix socket is different from Mac OS X and Mac OS X Server, depending on the type of installation you choose.

MySQL Unix Socket Locations on Mac OS X by Installation Type

  • Package installer from MySQL ------------------ / tmp / mysql.sock



  • Tarball from MySQL ------------------------------- / tmp / mysql.sock



  • MySQL related to Mac OS X server ------- / var / mysql / mysql.sock

So, just change database.yml < to indicate the desired location depending on which OS and installation type you are using

+3
Oct 02 '13 at
source share

The default location for the MySQL socket on Mac OS X is /var/mysql/mysql.sock .

+2
Mar 31 '11 at 11:35
source share

These are the options to fix this problem:

Option 1: change your host to 127.0.0.1

 staging: adapter: mysql2 host: 127.0.0.1 username: root password: xxxx database: xxxx socket: your-location-socket 

Option 2: It seems you have 2 connections to you on the MySql server. To find the location of the socket file, follow these steps:

 mysqladmin variables | grep socket 

for me gives:

 mysqladmin: connect to server at 'localhost' failed error: 'Can't connect to local MySQL server through socket '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' (2)' Check that mysqld is running and that the socket: '/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock' exists! 

or

 mysql --help 

I get this error because I installed XAMPP on my OS X version 10.9.5 for a PHP application. Select one of the default placements here.

I choose rails for default applications:

 socket: /tmp/mysql.sock 

For my PHP applications, I install XAMPP, so I install my socket here:

 socket: /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock 

OTHER Socket Location in OS X

For MAMPP:

 socket: /Applications/MAMP/tmp/mysql/mysql.sock 

For the package installer from MySQL:

 socket: /tmp/mysql.sock 

For MySQL shipped with a Mac OS X server:

 socket: /var/mysql/mysql.sock 

For Ubuntu:

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

Option 3: If all these settings do not work, you can delete the location of your socket:

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

Hope this helps you.

+2
Sep 22 '15 at 2:43
source share

I had the same problem, but none of the answers gave step by step what I needed to do. This error occurs because the socket file has not yet been created. All you have to do is:

  • Start your mysql server, so your /tem/mysql.sock created to do this: mysql.server start
  • Once this is done, go to config/database.yml and add socket: /tmp/mysql.sock
  • Run rake:dbmigrate again and everything should work fine.
+1
Nov 30 '16 at 18:38
source share

I found that the problem is that I only have a production environment. I do not have a development or testing environment.

By adding 'RAILS_ENV = production' to give the command

 bundle exec rake redmine:plugins:migrate RAILS_ENV=production 

he worked

0
Aug 18 '15 at 7:26
source share

If you are using MYSQL via XAMPP:

  • Open the XAMPP mysql configuration file (on OSX):

    /Applications/XAMPP/etc/my.cnf

  • Copy the socket path:

    socket = / Applications / XAMPP / xamppfiles / var / mysql / mysql.sock

  • Open rails project database configuration file: MyProject / config / database.yml

  • Add the socket configuration to the development database configuration:

->

 development: adapter: mysql2 encoding: utf8 reconnect: false database: difiuri_falcioni pool: 5 username: root password: host: localhost socket: /Applications/XAMPP/xamppfiles/var/mysql/mysql.sock 
  1. Restart Rail Server

Enjoy :)

0
Jun 15 '16 at 15:32
source share

You have problems with this: Unable to connect to the local MySQL server through the socket '/var/run/mysqld/mysqld.sock'

Ans: $ sudo service mysql start

0
Mar 22 '17 at 12:58 on
source share

On my machine, the mysqld service stopped, why did it give me the same problem.

1: - Go to the terminal and enter

 sudo service mysqld restart 

This will restart the mysqld service and create a new sock file in the right place.

0
Dec 16 '17 at 7:25
source share



All Articles