Skip to content

Instantly share code, notes, and snippets.

@jakebellacera
Created August 22, 2012 20:29
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jakebellacera/3429066 to your computer and use it in GitHub Desktop.
Save jakebellacera/3429066 to your computer and use it in GitHub Desktop.
Instructions on how to use MAMP with the mysql2 gem and Ruby 1.9.3-p194 via rbenv

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

  1. Install homebrew
  2. Install rbenv: brew install rbenv, follow any instructions homebrew gives you after the installation is complete.
  3. Install rbenv ruby-build plugin: brew install ruby-build, follow any instructions homebrew gives you after the installation is complete.
  4. 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.
  5. Test install is complete by running which ruby, it should point to something like /Users/<account>/.rbenv/shims/ruby.

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!

  1. 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.
  2. Extract contents to /tmp
  3. Go inside. cd /tmp/mysql-5.5.27
  4. Install cmake via homebrew. brew install cmake
  5. Cmake it. cmake . -DMYSQL_UNIX_ADDR=/Applications/MAMP/tmp/mysql/mysql.sock -DCMAKE_INSTALL_PREFIX=/Applications/MAMP/Library
  6. Then make it. make -j 2
  7. copy/paste libmysql's .dylib files into MAMP's build of MySQL cp libmysql/*.dylib /Applications/MAMP/Library/lib/
  8. Make a directory containing MySQL includes in the MAMP build of MySQL. mkdir -p /Applications/MAMP/Library/include/mysql
  9. Paste in missing include files into MAMP's MySQL. cp include/* /Applications/MAMP/Library/include/mysql
  10. 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
  11. 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
  12. 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
  13. 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.

@sayanee
Copy link

sayanee commented May 17, 2013

Thanks Jake for the clear and updated instructions! This works for me as well.

I have made slight changes to the url and the version numbers for MySQL downloads. Since I don't think I can pull request, here are my changes.

@admbtlr
Copy link

admbtlr commented Jan 20, 2014

This was immensely helpful, thank you. A couple of things:

  1. I didn't have libmysqlclient.16.dylib in my installation, so I had to skip the first install_name_tool (step 11)
  2. I had socket problems. MAMP doesn't use /tmp/mysql.sock, so I had to link it:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

@alonpeer
Copy link

Great instructions, thanks!

@karanrai
Copy link

Hi I get an error - The source directory does not appear to contain CMakeLists.txt while running cmake. Can't seem to figure out how to address that.

@rickytribbia
Copy link

Great instructions! I think that compile the mysql source code to make things work with MAMP is a bit too much, but however awesome! ;-)
Only one correction in my procedure:
At 9) I use the "-r" option to copy also the subfolder, is it useful?
cp -r include/* /Applications/MAMP/Library/include/mysql

@alatui
Copy link

alatui commented Jun 17, 2017

Only
$ gem install mysql2 -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
worked for me

@angelicarosa-spindox
Copy link

angelicarosa-spindox commented Mar 14, 2018

Thank you so much! Worked perfectly also with MAMP 4.4, mysql 5.6.39 and rvm instead of rbenv!

@TheBigSteph
Copy link

Only
$ gem install mysql2 -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config
worked for me

Thank you so much. I was stuck for 2 days

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment