Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby ActiveRecord Basics Migrations and Relationships Migrations

Can't connect to local mySQL server through socket

I've been reading the previous posts and checking up on StackOverflow and I still can't seem to be able to fix this. I installed mySQL via Homebrew on the terminal.

When I try to create the database it fails:

pedroruiz:biller pedroruiz$ rake db:create

<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>

Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"biller_development"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"} (If you set the charset manually, make sure you have a matching collation)

<Mysql2::Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)>

Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"biller_test"}, {:charset=>"utf8", :collation=>"utf8_unicode_ci"} (If you set the charset manually, make sure you have a matching collation)

This is the error code that pops up in terminal whenever I try to access mysql:

pedroruiz:biller pedroruiz$ mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

This seems to be the location of the socket file:

pedroruiz:bin pedroruiz$ mysql_config --socket /tmp/mysql.sock

mysql seems to be running based on this:

pedroruiz:bin pedroruiz$ ps ax | grep mysql 75806 s001 R+ 0:00.00 grep mysql

However, I can't seem to be able to locate or change where mysql is looking for the .sock file.

Can anybody help me fixing this?

Roger Nordqvist
Roger Nordqvist
7,007 Points

Not sure what os, but if it is Linux you should find your configurationfiles for the daemon under /etc/mysql or something like that.

If you use netstat, does mysql listen on tcp/3306?

Hi Roger, thanks for the reply. I'm on Mac OS X Yosemite (10.10.2)

Roger Nordqvist
Roger Nordqvist
7,007 Points

Hi Pedro ...

Then I suggest you take a look at this StackOverflow post: StackOverflow post.

If you have installed a .dmg package from MySQL's official site you should have another installation package included (MySQLStartupItem.pkg) that should setup the daemon for you and then you restart.

My way, when on OSX, is to use Homebrew and if this is the way you have installed it, then there is an answer in the thread telling you how to make a link to the sock file to your /tmp dir (command used: ln -s)

3 Answers

Katherine Pawlak
Katherine Pawlak
12,609 Points

I had the same problem- also on a Mac. I went into system preferences clicked on the MySQL logo and started up the connection. From that point on, the steps worked and I no longer got that error message.

Did you set a password for your mySQL server before starting your project? I did that on my Ubuntu Linux system and I had to include my password in my database.yaml file to get it to work. I also used the Linux version of -uroot -p to get to the point of being able to access the server. If I would have known that I was going to have so many challenges with the server I would not have set the password.

Bob

juliuslopez company
juliuslopez company
8,100 Points

This might be more of an elephant gun for taking out a mouse, but this worked for me. Coderwall.com offers a post that steps you through removing all installs of mysql (using brew) and reinstall a single instance that works cleanly. I need to reset everything after having similar problems. Hope this helps.

The original post can be found here: http://bit.ly/1SbQrZ5

Remove MySQL completely

ps -ax | grep mysql
stop and kill any MySQL processes
brew remove mysql
brew cleanup
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/var/mysql
sudo rm -rf /usr/local/mysql*
sudo rm ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/My*
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
edit /etc/hostconfig and remove the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/My*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /private/var/db/receipts/*mysql*
restart your computer just to ensure any MySQL processes are killed
try to run mysql, it shouldn't work
Brew install MySQL per user Sedorner from this StackOverflow answer

brew doctor and fix any errors
brew update
brew install mysql
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
mysql.server start
run the commands Brew suggests, add MySQL to launchctl so it automatically launches at startup
mysql should now work and be running all the time as expected