Song of The Day: Secret Crowds - Artist: AVA

Okay, so today I decided to fix the fact that my MySQL server doesn’t auto-load upon boot on OS X Leopard.

To be honest I’ve been doing the whole “mysqld_safe &”-thing for a while (i.e., pre-Leopard) and grew tired of that. So there is a simple way to do this right? Well, of course there is, but it’s surprising to find myriad articles on the Net explaining how to accomplish this in slightly different ways! You gotta love the Internet for access but not always for its uniformity :).

Most of the reason for the lack of uniformity on the subject concerns older versions of OS X and older versions of MySQL on OS X. However, as I learned all too well from my iPhone third-party application experience, the new–well somewhat new–direction for loading items during startup is with OS X’s system wide and per-user daemon and agent manager: launchd and launchctl. That service, launchd, replaces init.d and “rc” scripts of which most Unix-based users are familiar. Why? Well, you can read more about that here. The nutshell synopsis is that Apple merged the launch capabilities of init, inetd, and cron into “a single, standardized, interface to any and all programs started automatically by the system.”

As such, launchd is the preferred way for loading daemons from OS X Tiger (10.4) and beyond. There is a small learning curve, but not too bad after the first experience.

There are numerous articles that reference the older, but still widely used, mechanism of using OS X and MySQL /Library/StartupItems, good for pre-Tiger versions of OS X and for daemons not designed to run with launchd, but this entry focuses upon the newer launchd mechanism. Here’s another StartupItems how-to referenced from an older Apple document.

So here it is:

  • Check out “man launchd,” “man launchd.plist,” and “man launchctl.” Reading about its history isn’t bad either.
  • Add the following plist contents to a file, with your specific directories and option values, called org.mysql.launchd.mysql.plist:


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Label</key>
    <string>org.mysql.launchd.mysql</string>
    <key>ProgramArguments</key>
    <array>
    <string>/usr/local/mysql-max-5.0.24-osx10.4-i686/bin/mysqld_safe</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>SuccessfulExit</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
    <UserName>
    <string>_mysql</string>
    <GroupName>
    <string>_mysql</string>
    </dict>
    </plist>

  • Issue the following in launchctl or reboot:

    sudo launchctl
    launchd% load /System/Library/LaunchDaemons/org.mysql.launchd.mysql.plist
    launchd% exit

Interestingly, after writing a quick solution based upon my Joggame Server plist for the iPhone, I found this older blog entry on the net. It still appears mostly valid, but the author doesn’t mention the OnDemand key as a part of the service. The OnDemand option, deprecated by the KeepAlive option in Leopard, allows one to either allow a daemon to keep running continuously or to allow the launchd service to launch the daemon once needed.

Read the rest of this entry »