Installing MySQL for Mac OS X Leopard (10.5)

mysql

Back in 2004, I wrote Foundation Mac OS X Web Development for friends of ED. The book covered all aspects of Web Development on the Mac OS X platform and sold well around the world. At the time of writing, Mac OS X was at version 10.3 (Panther). The majority of the book still holds up today, but for one wildly out of date section: installing and configuring the MAMP environment.

I figured it was time to address this on my blog. As well as updating these instructions, I’ll be covering the much requested setting up of virtual hosts.

Firstly, I’ll tackle the problem of installing MySQL, as some of you will be happy enough to use the pre-installed Apache and PHP. I’ll cover installing those from source those in a forthcoming article.

Here’s what we’ll be covering in this article:

  1. Installation Requirements
  2. Set Your Path
  3. Install MySQL 5.1.30
  4. Run MySQL on Boot

Right then: let’s get started.

Installation Requirements

This install was performed on an old iBook G4 which I’ve crowbarred Leopard (10.5.6) and Xcode onto. It was a vanilla install on a clean disk, so there were no remnants from earlier versions of MAMP activity, other than the pre–installed MySQL in /usr/bin.

You’ll be using the command line on this job, for which you’ll need administrator access to do all this tinkering under the hood. As such, I’m going to assume you know what you’re doing and what not to do. If it’s not in your Dock already, you can find Terminal in the Utilities folder in your Applications directory: /Applications/Utilities/Terminal.app (or press cmd + U in Finder).

Set Your Path

To enable you to keep in control of things and because you keep a tight ship, you’ll be installing into /usr/local rather than /usr/bin. For reasons why you should do this, Dan Benjamin has written up a most excellent explanation: Using /usr/local. With that in mind, you’ll need to have your path set correctly, or this stuff ain’t gonna get you anywhere.

  1. You need to add the following line into a file called .bash_profile, so open up a new Terminal window and type the following:

    1. vim ~/.bash_profile

    If the file already exists, it will open it. If no such file exists, you’ve just created it.

  2. Press i to enter insert mode and type:

    1. export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql:$PATH"
  3. Press esc to exit insert mode and save your work by typing:

    1. :wq

That’s your path set, so let’s get on with the main feature: installing MySQL.

Install MySQL 5.1.30

Basically, the steps are as follows: you’ll be creating a directory, downloading some files to it, building and then installing the files before finally telling OS X what to do with them. Right, let’s have at it.

  1. Create a directory for the source files and then download & unpack them, entering your password when prompted:

    1. sudo mkdir /usr/local/src
    2. cd /usr/local/src
    3. sudo curl -O http://mysql.he.net/Downloads/MySQL-5.1/mysql-5.1.30.tar.gz
    4. sudo tar xzvf mysql-5.1.30.tar.gz
    5. cd mysql-5.1.30
  2. Now that you’ve got the files, it’s time to configure them with this chunk of code [source for the curious]:

    1. CC=gcc CFLAGS="-O3 -fno-omit-frame-pointer" CXX=gcc \
    2. CXXFLAGS="-O3 -fno-omit-frame-pointer -felide-constructors \
    3.     -fno-exceptions -fno-rtti" \
    4.     sudo ./configure --prefix=/usr/local/mysql \
    5.     --with-extra-charsets=complex --enable-thread-safe-client \
    6.     --enable-local-infile --disable-shared
  3. With the configuration done, it’s time to build (make) and install the files. This is the part where you might want to nip off and put the kettle on, because building MySQL takes ages. Type:

    1. sudo make
    2. sudo make install
  4. MySQL installation requires a Mac OS X user account named mysql, but a user account with this name exists by default since Mac OS X 10.2. So, start MySQL off with some default databases:

    1. cd /usr/local/mysql
    2. sudo ./bin/mysql_install_db --user=mysql
    3. sudo /usr/local/mysql/bin/mysqld_safe --socket=/private/tmp/mysql.sock --user=mysql &
  5. Now that’s done, you can close that window. Open a new one and type the following to check mysqld is running:

    1. ps aux | grep mysql

    What you’ve done there is search through the processes that are running and asked for information about any mysql processes. If all has gone to plan, you should see a screen like this:

    _mysql 89722 0.0 0.1 613740 4380 ?? S 11:53am 0:00.03 /usr/local/mysql/libexec/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --log-error=/usr/local/mysql/var/macbook.local.err --pid-file=/usr/local/mysql/var/macbook.local.pid

    Success!

  6. Now you need to set the admin password. In this code snippet, I’ve set the password to new_password, but you should add your own password in place of new_password:

    1. mysqladmin -u root password new_password

    If you get an error here telling you that mysqladmin: command not found, then you didn’t set your path properly earlier. /usr/local/mysql is the part of the path that comes into play here.

  7. Next, you want to login to the mysql shell:

    1. mysql -h localhost -u root -p

    Type in the password you specified in the previous step, when prompted. You should see a screen like this:

    Welcome to the MySQL monitor. Commands end with ; or \g.
    Your MySQL connection id is 1
    Server version: 5.1.30 Source distribution

    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

    mysql>

  8. You’re almost done, but first you have to give the mysql user the right permissions

    1. GRANT ALL PRIVILEGES ON *.* TO mysql@localhost IDENTIFIED BY 'new_password';

    Obviously, replace new_password with the password you specified in the previous step.

  9. Now you’re good to go, so check to see what databases are present

    1. SHOW DATABASES;

    That should give you a screen that looks something like this:

    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | test               |
    +--------------------+
    3 rows in set (0.05 sec)

Run MySQL on Boot

You’re almost there, don’t worry. To get MySQL to startup when you boot your Mac, you need a launchd file.

  1. Open a new Terminal window and type:

    1. vim ~/Desktop/com.mysql.mysqld.plist
  2. Press i to enter insert mode and type:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST
    3.     1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    4. <plist version="1.0">
    5. <dict>
    6.     <key>KeepAlive</key>
    7.     <true/>
    8.     <key>Label</key>
    9.     <string>com.mysql.mysqld</string>
    10.     <key>Program</key>
    11.     <string>/usr/local/mysql/bin/mysqld_safe</string>
    12.     <key>RunAtLoad</key>
    13.     <true/>
    14.     <key>UserName</key>
    15.     <string>mysql</string>
    16.     <key>WorkingDirectory</key>
    17.     <string>/usr/local/mysql</string>
    18. </dict>
    19. </plist>
  3. Press esc to exit insert mode and save your work by typing:

    1. :wq
  4. Now you need to move the file into the correct place and set the permissions on it. Type the following and type in your password when asked to do so:

    1. sudo mv ~/Desktop/com.mysql.mysqld.plist /Library/LaunchDaemons
    2. sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist
  5. All that’s left to do now is tell launchd to load and startup MySQL when your Mac boots, so type:

    1. sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist

Hopefully, everything went as planned and you now have a fully working installation of MySQL 5.1.30 on your Mac.

I will try to answer any questions raised in the comments, time permitting, but feel free to help each other out in the meantime. =)

This article is copyright © 2012 Phil Sherry.

3 Responses to Installing MySQL for Mac OS X Leopard (10.5)

  1. This was a great walkthrough! Worked exactly as described. I feel all warm and mushy inside having compiled MySQL from source for the first time. Thanks so much!

    If anyone’s worried about the line numbers, don’t. They won’t get pasted when you highlight and copy.

    Success on MacBook 2.16GHz, 3GB mem, 320GB 7200, 10.5.6, XCode Tools from 10.5.0.

  2. Great walk through except it didn’t work with the last build 5.1.31. when I ran the configuration script I received a “./configure” file not found error. Also, ‘make’ didn’t work, no target in the file. Any way it was a great tutorial and helped me better understand how mySQL is laid out. I did find this tutorial on how to configure the .pkg installed version http://wolfpaulus.com/journal/mac-osx/mysqlosx.html
    while I was pulling my fairly sparse hair on why I was getting errors.
    Thank you for your efforts.
    Phil

  3. Pingback: How to fix a MySQL “packet bigger than ‘max_allowed_packet’ bytes” error (Error 1153) — Strong as an Ox

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>