Installing A Standalone Instance of MySQL on Mac OS X
Recently I decided I needed to broaden my horizons and learn a bit more about Ruby on Rails, whilst using a MySQL database back-end. I’m a fan of XAMPP and have been using it for years. Of course MySQL comes with XAMPP however, I was reluctant to mess around with this installation when it came to setting up Ruby on Rails. Instead, I opted to install a standalone version of MySQL for use with Ruby on Rails. I encountered a few difficulties along the way so thought I’d write this blog. It’s not exactly a step-by-step guide but hopefully something in here will help others.
1. Install XCode
XCode is Apple’s suite of developer tools. It’s a free download from the Mac App Store. Download it from there and it should install itself, no problem.
2. Install Homebrew
Next, you need to install Homebrew. Homebrew is an open source package manager for OS X. This is what we’re going to use to help install MySQL. Before we installing Homebrew, we need to make a folder for the packages we will download. This will be done via the Terminal (if you’re unsure where to find this, it’s under Applications > Utilities > Terminal).
mkdir -p /usr/local/Cellar
Now we are ready to install Homebrew using the following command in the Terminal
/usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
If you’re having trouble installing Homebrew, more information can be found at https://github.com/mxcl/homebrew/wiki/installation
This may take a short while (I’ve forgotten how long it took me). If you want to check it has installed correctly, run the “brew doctor” command. This will alert you to any issues, such as problems with the PATH variables. I’m not going to cover errors with Homebrew in this particular blog however, if you do encounter troubles, there are a number of sites out there providing useful help.
3. Installing MySQL
Once Homebrew has installed and any errors have been resolved, run the following command to install MySQL:
brew install mysqlAgain, I can’t remember how long this took to install but it didn’t take an overly long amount of time, if that’s some comfort. It would be a great time to go and get a cup of tea though.
After this, we need to set the MySQL server to launch at start-up, though I’m not convinced this worked as it should on my machine. Just remember to change the version number in the command below, in accordance with the one you are using.
mysql_install_db --verbose --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp mkdir -p ~/Library/LaunchAgents cp /usr/local/Cellar/mysql/5.5.20/com.mysql.mysqld.plist ~/Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
4. Running 2 instances of MySQL simultaneously
Here’s where the fun and games begin. We already have MySQL installed on the Mac- the version which came with XAMPP. MySQL usually runs on port 3306 and obviously, this will cause chaos if we try and run 2 versions at once (slight exaggeration there: it won’t cause chaos but it just won’t work!).
In order to prevent any conflicts, I decided to force the standalone version of MySQL to run on another free port. In my case, I chose port 3307. If you’re unsure which port to use, there’s a port scanner included with OS X under Applications > Utilities > Network Utility and this will help you identify a free one. Still, 3307 should be ok.
Navigate to usr/local/etc/my.cnf. If there is no my.cnf file, make one. Copy and paste the following information into it (this information sets MySQL to run on port 3307 on localhost):
[client] port = 3307 socket = /tmp/mysql.sock [mysqld] event_scheduler = ON skip-character-set-client-handshake collation_server = utf8_unicode_ci character_set_server = utf8 bind-address = 127.0.0.1 port = 3307 socket = /tmp/mysql.sock max_connections = 20
Before we go any further, I would suggest starting up XAMPP and navigating to http://localhost/phpmyadmin in your browser, just to check that it’s still running correctly. There’s no reason the standalone version should interfere with the XAMPP version but you might want to double-check for peace of mind (I did).
5. Here come the errors (probably)….
At this stage, you’re likely to get errors with the MySQL installation. Want to test it and see if that’s the case? Enter the following into the Terminal:
mysql.server start
mysqlChances are, this will generate the “ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)” error. That’s because we still have a few things to change to resolve any issues.
Navigate to /usr/local/Cellar/mysql/5.5.20/support-files/mysql.server
Near the start of the file you should see the following lines:
basedir= datadir=
Change these lines to:
basedir=/usr/local/Cellar/mysql/5.5.20/ datadir=/usr/local/Cellar/mysql/5.5.20/data
It may seem obvious but check your version number and add in the appropriate one- I’m running 5.5.20 but off the top of my head, I’m not sure if that’s the latest version.
Try starting the server again by using:
mysql.server start mysql
You may still get an error which says something along the lines of “ERROR! Manager of pid-file quit without updating file.” I’m not entirely sure what that’s all about but it’s not good.
Again, you need to navigate back to /usr/local/Cellar/mysql/5.5.20/support-files/mysql.server
Locate the following line:
mysqld_pid_file_path=$datadir/`/hostname`.pid
Change it to:
mysqld_pid_file_path=$datadir/`/bin/hostname`.pid
Now try:
mysql.server start mysql
Hey presto, it should work! Go and have a celebratory cup of tea. If the installation still gives an error, get a cup of tea anyway- you’ll probably be needing one by this stage. Most likely, there will be an issue with file permissions some where along the line. I found this thread on stackoverflow to be quite useful. Basically, you may need to “chown” a few folders.
6. Installing the MySQL Preference Pane
There’s nothing to see here, move along. What I’m trying to say is, I battled with the prefpane. I wanted it to work, I really did but it didn’t. In short, it was a pain in the backside. It only wanted to start/stop the MySQL installation in the default location which is /usr/local/mysql as opposed to /usr/local/Cellar/mysql. Just use commands in the Terminal instead to start/stop the server:
mysql.server start mysql.server stop
7. Useful Information
Whilst trying to install MySQL, I found both Frederico Araujo’s blog and Jason Seifer’s blog to be useful resources.
Additionally, you may find the MySQL Workbench tool handy when managing various databases. It serves as a neat front-end and allows you to either type SQL or interact with a user interface.
* * * *
As I previously mentioned at the beginning of this post, this isn’t a fine-detailed guide to installing a standalone version of MySQL whilst running XAMPP- there are a number of errors which could appear and I couldn’t possibly anticipate them all. However, if there are people out there who have been struggling, I’m hoping this post at least provides some guidance.
Now I just need to get started on building something with Ruby on Rails….






