How to use MAMP with the mysql2 gem and Ruby 1.9.3-p194 via rbenv
Let's say you're a web developer who happens to work with both MAMP and Ruby when building different types of websites. Let's say you also like to keep your MySQL stuff in one place and don't like having to juggle both a local MySQL install as well as a MAMP MySQL install. Well, you can indeed connect your ruby apps to MAMP's MySQL. Here's a tutorial on how to do it.
Important! Before you do anything, download and install MAMP. MAMP Pro will work as well. At the time of this writing, MAMP 2.1.1 is the latest.
First, install Ruby via rbenv
- Install homebrew
- Install rbenv:
brew install rbenv, follow any instructions homebrew gives you after the installation is complete.
- Install rbenv ruby-build plugin:
brew install ruby-build, follow any instructions homebrew gives you after the installation is complete.
- Install ruby. We'll install 1.9.3-p194 for this tutorial.
rbenv install 1.9.3-p194. After the install is complete, run
rbenv rehash; exec $SHELL.
- Test install is complete by running
which ruby, it should point to something like
Getting the mysql2 gem to work with MAMP
Essentially, all you need to do is copy a few files from the MySQL source. MAMP doesn't ship with a 100% complete install because it doesn't need it. You'll be building MySQL from source and pasting the required files into the MAMP MySQL install.
This portion of the tutorial is modified from this tutorial. Thanks, YJ!
- MAMP 2.1.1 comes with MySQL 5.2.25. Despite the slight version number change, download the 5.2.27 source from MySQL's servers.
- Extract contents to
- Go inside.
- Install cmake via homebrew.
brew install cmake
- Cmake it.
cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
- Then make it.
make -j 2
- copy/paste libmysql's .dylib files into MAMP's build of MySQL
cp libmysql/*.dylib /Applications/MAMP/Library/lib/
- Make a directory containing MySQL includes in the MAMP build of MySQL.
mkdir -p /Applications/MAMP/Library/include/mysql
- Paste in missing include files into MAMP's MySQL.
cp include/* /Applications/MAMP/Library/include/mysql
- Install the mysql2 gem with MAMP's MySQL config.
env ARCHFLAGS="-arch x86_64" gem install mysql2 -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
- Run the name_tool install script.
sudo install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.16.dylib /Applications/MAMP/Library/lib/libmysqlclient.16.dylib ~/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
- And one more time:
sudo install_name_tool -change /tmp/mysql-5.5.9/libmysql/libmysqlclient.18.dylib /Applications/MAMP/Library/lib/libmysqlclient.18.dylib ~/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundle
- Remove the temporary mysql install:
rm -rf /tmp/mysql-5.5.27
Once you're done, run
rbenv rehash; exec $SHELL to refresh your terminal/rbenv shims and restart MAMP if it was running. mysql2 should now be able to connect to your MAMP install. If you are still having connection issues you may need to manually define the socket (to MAMP's socket) whenever you connect to MySQL via the mysql2 gem.