I recently upgraded to Mountain Lion and like every good little nerd, I did 7 pass writing of zeros to my harddrive. In other words, I erased the crap out of it and created a fresh install. A lot of people complain this is quite annoying as you have to reinstall and re-setup everything on your Mac. I actually enjoy it because it gives me a clean slate and a chance to de-clutter my machine and do fresh installs and configs of all my software. If you develop locally, one of the issues you’re going to run into is that you need to get MySQL, PHP, Ruby, Python, etc… and Apache all running again. Like I said before, one of the nice things about starting fresh is that you can try new things. One thing I have vowed to do this go around is pull all of my packages and installs down with Homebrew. So let’s get started.
After setting up a new machine today I realized this post uses an old version of MySQL from the Homebrew registry. If you were running into trouble with some of the code snippets it is because the post is making the assumption that Homebrew is pulling down MySQL version 5.5.25a, but currently Homebrew installs 5.6.10 (as of this update). I’ve updated the code snippets below, but be sure to check what version is being pulled down in the future. I also ran into an issue with trying to access localhost from PHP scripts and realized php.ini was the culprit. If you’re using the Homebrew package for MySQL and the default php.ini file (located in /etc/php.ini) then your trying to connect through the default_socket at /var/mysql/mysql.sock which very well might not exist depending on your situation. You can do one of two things, replace all occurrences of
with this instead
or alternatively if you don’t want to mess with the php.ini you could just run a symlink like so
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
You might need to create the /var/mysql directory if it doesn’t exist. Hope that helps someone.
First we need to open up our trusty Terminal app and install Homebrew. This is pretty darn simple thanks to ruby and git, just run the following command:
ruby <(curl -fsSk https://raw.github.com/mxcl/homebrew/go)
This will pull down Homebrew and get it all installed and running for you. It will prompt you to press enter after it shows you what it’s going to do. The great thing about Homebrew and the reason I will be using it is because of it’s modularity. It creates it’s own little “house” of packages and code that keeps everything quite tidy. So now that we have Homebrew, we can extremely simply pull down the newest copy of MySQL by running the following command:
$ brew install mysql
Yep, it’s that easy. Now, out of the box you won’t be up and running MySQL queries quite yet. You need to run a few more commands to get things situated. I like to have the mysql server start on startup and to do so you would run the following commands:
$ mkdir -p ~/Library/LaunchAgents $ cp /usr/local/Cellar/mysql/5.6.10/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/ $ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
You’ll also need to unset the temp directory and install the mysql system tables
$ unset TMPDIR $ mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
And lastly you’ll want to reset the root password
$ /usr/local/Cellar/mysql/5.6.10/bin/mysqladmin -u root password 'YOUR_NEW_PASSWORD'
Now you are ready to start the sql server and run your first query:
$ mysql.server start $ mysql
Doesn’t get much easier than this. Share your thoughts in the comments on how to get things back into running order after a fresh install.