MySQL launchd Service

Gotcha's

Use xmllint with –valid to validate your .plist file. I didn’t and wondered why launchctl just kept telling me there was nothing to load/unload for my file. Alternately use the PropertyListEditor application ... I prefer good old vi for the copy and paste.

Be carefull not to put OnDemand to false. I thought this was todo with the network services, which I didn’t want MySQL as. After all you don’t want MySQL started for each network request and stopped again ... that would be ridiculouse. But as I had it switched off, every time I stopped it with launchctl, it simply restarted. So leave this out (which defaults to on) unless you want a service that even you can’t stop.

If you really want to stop a service that is set to OnDemand false then you have to unload it. This means knowing where the plist file is, in case it’s not a service you created. I found this out when trying to stop my immortal mysqld process.

Do not edit your plist file while it’s still loaded. If you make a mess of it, you can’t unload it. It would appear launchd doesn’t simply unload a loaded service “from memory” but rather re-reads the plist file for it and then unloads it. So if you plist file is corrupt it simple tells you there is nothing to unload. It took me a while to figure this one out, and thus be able to stop my mysqld daemon.

The Program key is supposed to set the program name. However, from what I can see it just doesn’t, so you have to set it in the ProgramArguments list as the first argument. Which is not what is listed in the documentation for plist files.

Notes

Despite my optimistic hope it appears there is no integration with SysteemPreferences. so you can’t install a service in launchd and then have it magically appear for enabling and disabling there just like samba etc. Which is a bit of a shame as it would be useful if you have to uild many machines and then let the users decide what services they did or didn’t want.

I have chosen to implement my mysqld process directly rather than using safe_mysqld like most othre on the web who have done this. The reason for this is that all safe_mysqld does, as far as I can tell, is start mysqld in the right place as the right user. But I wanted to start mysqld directly as you can have apple change to the chosen user before starting mysqld rather than let mysqld do it. Which avoids a know possible issue with the way mysqld decided which user to run as. It also reduces the number of processes you have to run by one.

The particular version of MySQL I am using is from darwinports. So if you have a different version you would need to change my plist file. Also, I haven’t yet decided how to tackle the problem that all GUI clients insist that the local socket file should be in /tmp/ rather than looking at the /etc/my.cnf program. Currently I simply create a symlink myself, but it would be nice if I could easily do this with launchd without having to create another services.

I chose to put my Script in /Library/LaunchAgents/com.mysql.MySql.plist as I am the only person that uses MySQL and I only use it from my Laptop. This is where you put services that are only needed when someone is logged in. If however you wanted it to run at all times, even when nobody is logged in (ie. on a server) then you would put it in /Library/LaunchDaemons/com.mysql.MySql.plist.

plist File

Well, assuming you want to start off with my plist file rather than those others on the web, here it is. Just remember to change the directory paths. Oh, and make sure they are owned by and writable by mysql otherwise you won’t get any logging and it might not even work.

    <?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>com.mysql.MySql</string>
        <key>UserName</key>
            <string>mysql</string>
        <key>GroupName</key>
            <string>mysql</string>
        <key>Program</key>
            <string>/opt/local/libexec/mysqld</string>
        <key>ProgramArguments</key>
        <array> 
            <string>--datadir=/opt/local/var/db/mysql</string>
            <string>--basedir=/opt/local</string>
            <string>--pid-file=/opt/local/var/db/mysql/Wales.local.pid</string>
            <string>--user=mysql</string>
        </array>
        <key>WorkingDirectory</key>
            <string>/opt/local/mysql</string>
        <key>ServiceDescription</key>
            <string>Mysql 4.1 Database Server</string>
        <key>Umask</key>
            <integer>007</integer>
        <key>StandardOutPath</key>
            <string>/opt/local/var/log/mysql/stdout.log</string>
        <key>StandardErrorPath</key>
            <string>/opt/local/var/log/mysql/stderr.log</string>
    </dict>
    </plist>
 
technical/unix/mac_os_x/launchd_mysql.txt · Last modified: 2007/01/20 16:19 by daleroberts