Installing MySQL on Mac OS X 10.5+
This is a very brief description on how to download the source tarball for MySQL, compile and install MySQL on OS X.
I’m currently using a Mac Book Pro 15″ with the maximum amount of memory 3 GB, 2.33 GHz, Intel Core 2 Duo, 667 MHz Bus and a 120 GB internal disk. I have several external drives including a LaCie 250 GB 800 Firewall drive and a USB Seagate FreeAgent 120 GB drive.
Why You Would Build Instead of Use a Package to Install MySQL
When new Operating Systems are released sometimes there aren’t installers available for the new release or the installer may break when a minor release update package is installed. Compiling source files along with choosing the appropriate configuration options can produce optimized, machine-specific binaries, etc.
Step One is to determine what the latest version of MySQL is. There is now a packaged installer for Leopard, that was not the case at the end of last year when Leopard shipped in the late fall.
The following link points to a page on Sun Microsystem’s web site:
The heading for this page is 2.4.10 Installing MySQL on Mac OS X
There are installation links for the various operating system that MySQL runs on listed in the right side bar. There are several other links on this page that point to various locations within Sun Microsystem’s web site that covered the various tasks, features, etc. One of the links points to the location where you can obtain MySQL. That link is:
Following the above link will take you to the MySQL Downloads Page which contains the latest version information as well as the various MySQL Products offered by Sun Microsystems (Sun purchased MySQL last year). There are Quick Jump links to various community sites. Sun offers the MySQL Community Server as well as an Enterprise edition.
Towards the bottom of this page you’ll see a section with the title MySQL Community Server. It is here where the information for the Current Release, Upcoming Releases and Older Releases is displayed. Typically all vendors suggest using the Current (Stable) Release and if you are looking for a binary package this is your best shot at locating a pre-built packaged installer.
This link: will lead you to the Mac OS X (package format) downloads and the Tar Packages downloads are listed right below. There are hot links to pull up the platform notes for each section and you can browse down the page until you find your OS X version. There is a packaged formatted installer for Leopard now (OS X 10.5). I’ve tested the package and I was able to easily install MySQL and immediately start using it without even rebooting.
After testing the package install I moved on to downloading the appropriate Tar Package for my Mac Book Pro notebook and proceeded to download the software, compile and test the installation of MySQL. I encountered a few issues after the installation was complete and there were related to DNS resolution and how MySQL resolves connection data.
What follows is brief narrative of my experience today installing MySQL from a Tar Package. Before beginning I will encourage anyone who is installing ANY software to make a current backup of their system. I’ve been using Time Machine for the past month and a half and I had one occasion where I actually recovered a preference file from my Time Machine backups. I generally use a combination of backup methods, I will use tar, Apple’s Backup Application, Apple’s Sync to backup preferences, bookmarks, etc. as well as Retrospect (EMC’s OS X Backup Solution, formerly owned and developed by Dantz). I’m not overly attached to any one backup solution and if you have the luxury, space, can afford external storage then Time Machine is not a bad solution. Time Machine offers very basic configuration options, essentially you turn it on or off and you can specify directories to exclude. It executes every hour and uses the Finder interface when you need to enter Time Machine to research and/or restore a file/directory previously backed up.
Prerequisites
You’ll need to install XCode which can be found on your Mac OS X Installation Media under the Optional Installs. You can also download the latest version of Apple’s Developer Tools (XCode) by creating an account on Apple’s Developer Connection website and navigating to Developer Tools then selecting the appropriate version for your system. A membership to ADC is free and the website URL is:
Existing MySQL Installations
If you already have a MySQL installation on your system you need to remove it or move it out of the way. Depending on whether it was installed from a Package or Source Code will determine how to remove the existing software. If it was installed from a Package then you can simply rename or delete a single file, actually the symbolic link in either /usr/local or /opt/local (the two most common locations for third-party or external software). By renaming or deleting the symbolic link you’ll disable that version from being at the minimal auto-started at boot time. If you choose to install or create your own Startup Item entry, you’ll want to remove that as well. Keep in mind that if you ever want to auto-start the old version of MySQL you’ll need to either re-download the package installer, reinstall the Startup Item(s) or copy/move them back into place.
Examples for renaming/deleting the Symbolic Link:
cd /usr/local
ls -l - you’ll see a directory entry with the name mysql -> if the links is present
sudo rm /usr/local/mysql – this will remove the symbolic link
sudo mv /usr/local/mysql /usr/local/mysql-oldversion – this will rename the link
To remove the previous installation:
cd /usr/local
rm -rf mysql
Exporting or migrating older data isn’t difficult but I’ll cover that topic in a different post.
Set Up Environment
To install the software you will need to open a Terminal Session, use an X11 Xterm session if desired or iTerm. The point is you will need to execute the commands using the Unix shell of your choice. It generally works best to pick a fixed size font for software installations to avoid discrepancies in typesetting, etc.
The first decision you need to make is where to put the new software installation. Most people choose either /usr/local or /opt/local. I believe the recent trend for OS X is to use /usr/local. It really is a user preference, however, most configuration defaults are geared to use /usr/local for the prefix. You can control that by passing the argument –prefix=/usr/local to the configuration script. For my install I choose /usr/local and that is what I will use here.
Set the Environment Variables
You can choose to either edit your .bashrc file at this time and just dynamically update your PATH variable. First, just check to see if /usr/local (or /opt/local) is in your PATH. You can do this by typing echo $PATH at the command prompt. If you do not see /usr/local in the PATH string you can add it by:
export PATH=$PATH:/usr/local:.
You can edit your .bashrc (assuming you are using the default OS X bash shell). Search for the location where PATH is defined and add /usr/local to the end of the string.
The second decision you need to make is where to put the tarball you will be downloading and what location will you use to compile and install from (this is just a scratch area). Typically, you already have a Downloads location specified in your FTP/SFTP tool of choice. I generally just expand the tarball in its download location, compile and install from there as well. Otherwise, you will need to make a directory to hold the source, objects and executable(s).
I’ll use /Users/auser for my example, where auser is the user who is currently logged into the operating system. Assuming auser has already defined a downloads directory named downloads there is nothing to create, we can start downloading the software.
Download, Extract, Compile, …
Now we are ready to begin the real work. Using the version information you discovered when researching the current stable release of MySQL above substitute that version in the below example:
cd $HOME/downloads
curl -O http://mysql.he.net/Downloads/MySQL-5.0/mysql-5.0.51.tar.gz
tar xvf mysql-5.0.51.tar.gz
cd mysql-5.0.51
Note: If you receive an error after you type in the curl command line most likely you do not have curl on your computer and you will need to download that software package before proceeding.
Next you’ll need to configure MySQL using the standard configuration script:
CC=gcc CFLAGS=”-03 -fno-omit-frame-pointer” CXX=gcc CXXFLAGS=”-03 -fno-omit-frame-pointer \
-felide-consructors -fno-exceptions -fno-rtti” \
./configure –prefix=/usr/local/mysql –with-extra-charsets=complex –enable-thread-safe-client \
–enable-local-infile –enable-shared
When the above process completes, you can then initiate the compilation process:
make
This part may take awhile, it depends on how fast of computer you have. The next step below is the step that actually installs the software in its final resting place which is the /usr/local/mysql directory structure.
sudo make install
You will most likely be prompted to enter in the administrative password for your machine.
Now that the software is in place you need to execute a couple of administrative tasks as well as scripts to populate the MySQL system tables, generate the root user and grant the basic security permissions and roles to the delivered MySQL user accounts.
cd /usr/local/mysql
sudo chown -R mysql ./var
chmod -R 777 /private/var/folders
sudo ./bin/mysql_install_db –user=mysql
That concludes the initial installation of MySQL.
Setting up Machine to Auto-Start MySQL
With the release of Leopard, OS X is strongly encouraging the use of launchctl to start processes at boot time. Other methods such as using the /Library/StartupItems directory to initiate processes during boot or system restart still work but the preferred method is to use launchctl.
One method to accomplish this is to create a plist file for MySQL and place it in /Library/LaunchDaemons. The following plist file was taken from Dan Benjamin’s blog on Installing MySQL on Mac OS X located at:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>Program</key>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>mysql</string>
<key>WorkingDirectory</key>
<string>/usr/local/mysql</string>
</dict>
</plist>