87

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 I get when running rake db:migrate to create database is:

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 something simple I'm missing.

3
  • 1
    Is the socket file present? Maybe it's present somewhere else (/var/lib/mysql/mysql.sock)?
    – Eimantas
    Mar 31, 2011 at 11:26
  • Nope, neither exist. I'll try create one. Mar 31, 2011 at 11:30
  • the core of this issues or at the OS level..
    – raddrick
    Jan 7, 2016 at 18:37

14 Answers 14

213

First, to find your socket file:

mysqladmin variables | grep socket

For me, this gives:

| socket                                            | /tmp/mysql.sock                                                                                                        |

Then, add a line to your config/database.yml:

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /tmp/mysql.sock
6
  • 17
    If you have a password on root, do: "mysqladmin -pxxxx variables | grep socket" where xxxx is your root password Jan 14, 2012 at 13:49
  • I'm confused about how this works; where/how do you enter in the first command you listed? When I enter that into the terminal, it reports: mysqladmin: command not found
    – CodeBiker
    Nov 4, 2013 at 20:07
  • 2
    You can enter the command in any folder. Command not found indicates you don't have mysql installed.
    – Magne
    Oct 9, 2014 at 11:06
  • 2
    Even though I had no password I still had to specify the user as root: mysqladmin -u root variables | grep socket
    – bkunzi01
    Dec 16, 2015 at 15:11
  • 1
    @CodeBiker FYI: Linux shells tend not to execute unqualified program names from the working directory (unless it's explicitly in the PATH) - you have to be explicit, e.g. ./program_name .... This means that the actual program executed tends to be independent of where you try to run it from.
    – mwfearnley
    Jul 18, 2016 at 10:57
91

Found it!

Change host: localhost in config/database.yml to host: 127.0.0.1 to make rails connect over TCP/IP instead of local socket.

development:
  adapter: mysql2
  host: 127.0.0.1
  username: root
  password: xxxx
  database: xxxx
6
  • 7
    It would be better to find the mysql.sock file location and adding this line to config/database.yml: "socket: /path/to/mysql.sock"
    – gparis
    Mar 31, 2011 at 11:39
  • 2
    What's the benefit of using a socket over TCP/IP? Sorry - I'm pretty new to Rails and Unix Mar 31, 2011 at 11:55
  • 5
    Unix sockets have less overhead than TCP/IP sockets. Jun 20, 2011 at 13:49
  • 1
    Thank you! I ran into this problem outside of Rails (using mysql gem) and this solution worked. I don't have root access so I can't modify the gem to use another location for the socket file. This trick saved me headaches.
    – Sojoodi
    Dec 8, 2011 at 17:42
  • 2
    This answer has helped me in a different way. I'm running my rails application against a dockerized mysql instance, with localhost 3306 exposed. One side effect of this setup is that even though you can connect through localhost, you can only connect thought TCP since mysql is running in a VM, rather than on the same machine - so no socket connection. In lieu of this, mandating Rails use TCP is the answer here, because sadly there doesn't seem to be a better option that I've found.
    – Brian
    Apr 24, 2016 at 23:23
11

Your mysql server may not be running. Below explains how to start the server. This is an excerpt from the README file that comes with the mysql download.

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

If you have installed the Startup Item, use this 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 don't use the Startup Item, enter the following command sequence:

 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)
2
  • This answer helped me fix this issue on my MAC...not sure why this comment is not ranked higher. When you install mysql there are some notes displayed in the console. I had to run the following commands. unset TMPDIR and mysql_install_db --verbose --user=whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp....then I had to start the server mysql.server start`
    – dmillion
    Apr 7, 2012 at 22:27
  • It is possible for the service to stop working, this is definitely the first thing to check.
    – Tass
    Nov 6, 2013 at 22:51
8

These are options to fix this problem:

Option 1: change you host into 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 like you have 2 connections into you server MySql. To find your socket file location do this:

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 in my OS X Version 10.9.5 for PHP application. Choose one of the default socket location here.

I choose for default rails apps:

socket: /tmp/mysql.sock

For my PHP apps, I install XAMPP so I set my socket here:

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

OTHERS Socket Location in OS X

For MAMPP:

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

For Package Installer from MySQL:

socket: /tmp/mysql.sock

For MySQL Bundled with Mac OS X Server:

socket: /var/mysql/mysql.sock

For Ubuntu:

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

Option 3: If all those setting doesn't work you can remove your socket location:

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

I hope this help you.

1
  • Nice explaination this command: socket: /var/run/mysqld/mysql.sock worked for me Oct 7, 2020 at 11:33
6

"/tmp/mysql.sock" will be created automatically when you start the MySQL server. So remember to do that before starting the rails server.

3

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

So my config/database.yml looks like:

development:
  adapter: mysql2
  host: localhost
  username: root
  password: xxxx
  database: xxxx
  socket: /Applications/MAMP/tmp/mysql/mysql.sock
3

If you are on Mac OSX,

The default location for the MySQL Unix socket is different on Mac OS X and Mac OS X Server depending on the installation type you chose

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 Bundled with Mac OS X Server -------/var/mysql/mysql.sock

So just change your database.yml in socket: /tmp/mysql.sock to point to the right place depending on what OS and installation type you are using

2

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

0
2

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

  1. Start you mysql server, so your /tmp/mysql.sock is created, to do that you run: mysql server start
  2. Once that is done, go to your app directory end edit the config/database.yml file and add/edit the socket: /tmp/mysql.sock entry
  3. Run rake:dbmigrate once again and everything should workout fine
1
0

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

By adding 'RAILS_ENV=production' to give the command

bundle exec rake redmine:plugins:migrate RAILS_ENV=production

it worked

0

If you are running MYSQL through XAMPP:

  1. Open XAMPP mysql configuration file (on OSX):

    /Applications/XAMPP/etc/my.cnf

  2. Copy the socket path:

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

  3. Open rails project's database configuration file: myproject/config/database.yml

  4. Add the socket config to the development database config:

-->

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 rails server

Enjoy :)

0

You have problem with like this: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Ans: $ sudo service mysql start

0

On my machine mysqld service stopped that's why it was giving me the same problem.

1:- Go to terminal and type

sudo service mysqld restart

This will restart the mysqld service and create a new sock file on the required location.

0

If you are running MYSQL through XAMPP or LAMPP on Ubuntu or other Linux, try:

socket: /opt/lampp/var/mysql/mysql.sock

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.