Dev Mashup: Building a dual MySQL 4.1 and MySQL 5 dev environment in OS X 10.5 Leopard (and more)

I've been wanting to upgrade to MySQL 5 for a while now, and after hearing that MySQL 5 is required for Drupal 7 I decided to bump up my upgrade schedule, pronto. If all one wants is MySQL 5 then that is easy enough -- go download MAMP and you're done. But what if you want to be able to dev on MySQL 4 for projects that require it for whatever reason (e.g., clients who are run it and are not willing/able to upgrade MySQL at the moment)?

Well, with not a lot of effort - it very easy to have a PHP 5, PHP 4, MySQL 5, and MySQL 4.1 environment - all sharing the same doc root and servernames (e.g., urls):

Step one:
Configure Apple's native Apache, PHP, and add a free-standing MySQL 4.1 install (since MAMP comes with MySQL 5, right). Here are two good links to get you on your way to doing this:
Install Drupal on Mac OS X 10.5 Leopard
Working with PHP 5 in Mac OS X 10.5 (Leopard)

...if you are migrating a free-standing MySQL install from a previous install you may want to check these articles out (1, 2) as well.

Note: Getting all of this totally set up and working right, including setting up your virtualhosts and /etc/hosts file, before installing MAMP will pay off.

Step two:
Download MAMP and follow the readme.txt instructions and/or use these links to setup the basics:
HowTo: Create a local environment using MAMP
Install a Local Web Server on Mac OSX

Step three:
Now it's time to integrated these two completely separate environments so that you can keep a common doc root, virtualhosts, and urls, and systems tools (we'll get terminal access going for MAMP's MySQL):

We'll make it so that we can keep all of our sites, no matter which enviroment they require, in a common place -- and so that no matter what we're running we will always be able type in "localhost", or and know that it will take us to our doc root (instead of having to remember to type "localhost:8888/" for MAMP:). Relatedly, doing the steps below lets us keep common server names (e.g., 'mysite.local') across environments.

So now:

* Change the docroot in MAMP's httpd.conf (line 368) to:
DocumentRoot "/Library/WebServer/Documents"

* Change the port in MAMP's preferences so that it uses port 80 instead of 8888 and add all of your virtualhosts from you native mac's Apache configuration file to MAMP's httpd.conf file.

Step four:
This step is optional, only useful if you want to be able to use terminal with MAMP's MySQL.

The mysql.sock symlink. If you've done everything correctly (see above links), the standalone MySQL will already have a symlink for mysql.sock at /var/mysql/mysql.sock AND /tmp/mysql.sock. To get MAMP's MySQL working on the commandline we have a situation where both MySQL installs need to have a symlink that exists within /tmp/ and which points to their own respective mysql.sock files (for whatever reason MAMP doesn't seem to care about having a symlink at /var/mysql). This is bit of an annoyance since two files with the same directory cannot share the same name. At the moment I manage this just by pasting the following code into terminal when I want to be able to be able to access MAMP's MySQL with terminal.

Setup mamp to work with terminal/command line:
First add the following line to /etc/profile:
export PATH="/usr/local/bin:/usr/local/sbin:/Applications/MAMP/Library/bin:$PATH"

To reload the file/change from terminal do:
. /etc/profile

Then from terminal do:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
sudo chown _mysql /tmp/mysql.sock
sudo chmod 777 /tmp/mysql.sock

It's an admittedly inelegant solution. I should probably make a script that does this automatically, or change all the paths within on of the MySQL installs so that it would look for a mysql-2.sock file or something like that, but it works in a pinch. :-)

Fyi, though the two MySQL instances seems to be able to work concurrently, with this setup you cannot run two HTTPD (apache) instances at one time. Not a big deal in my workflow, but I thought I should mention in case anyone is wondering.

15 February, 2008