Ruby on Rails 3 Can't connect to local MySQL server through socket '/tmp/mysql.sock' on OSX
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
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
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)
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.
"/tmp/mysql.sock" will be created automatically when you start the MySQL server. So remember to do that before starting the rails server.