MySQL in Leopard

Nov08

Yes, it really does work. It just requires a bit of command-line TLC to appease the LaunchDaemon and filesystem hot wiring to make Apache/PHP and MySQL play together.

Actually, it’s not that bad in hindsight. Trying to figure it all out, though, took forever. For any other poor soul that is trying to get it working, I hope this helps.

Download & Install

I downloaded a fresh disk image of MySQL 5 from mysql.com and ran the install package as you would normally. I installed the StartupItem but then realized later there’s no need since Leopard is transitioning to the new LaunchDaemon. So you can skip it. Also, I installed the Preference Pane, but it too is useless. So don’t worry about it.

Permissions

MySQL is typically installed in /usr/local/mysql-5.0.45-osx10.4-[platform] unless you changed the install location during the installation process. There is a link at /usr/local/mysql to make it a little easier to get into. The first thing I noticed was the permissions being a bit off. The entire directory tree was owned by root. I saw one site recommend using $ chmod -R 777 /usr/local/mysql which would allow anyone and everyone to read/write to any directory/file. While that works, I wouldn’t recommend it. When MySQL installs it adds a user to your system that the MySQL server daemon will run under. When I looked it showed up as _mysql. I found that I could still reference it as mysql though.

So to fix the permissions enter the following :
$ sudo chown -R mysql:wheel /usr/local/mysql

Now the MySQL server daemon should run correctly. If you tried starting MySQL before fixing the permissions you will have noticed it started and stopped right away. Now that the permissions are right, we can fire it up.
$ /usr/local/mysql/bin/safe_mysqld &

Now you should be able to connect to the server with the command-line client.
$ mysql -uroot

Automatic Start-up

To get MySQL server up and running when you reboot your machine, you’ll need to setup a LaunchDaemon. With Tiger, Apple introduced the LaunchDaemon and began to transition away from StartupItems as a new system for starting background processes. Like StartupItems, LaunchDaemon files live under the Library folders (/System/Library/LaunchDaemons, /Library/LaunchDaemons, ~/Library/LaunchDaemons).

All you’ll need to do is add a LaunchDaemon config file for MySQL into your /Library/LaunchDaemons directory. You can grab this MySQL Launch Daemon configuration file and copy it into place. (Thanks to Joannou Ng at the TomatoCheese Blog for the config file).

$ sudo mv ~/Downloads/com.mysql.mysqld.plist /Library/LaunchDaemons

Be sure to change the MySQL Launch Daemon config file’s owner to root or launchd will complain about funky permissions.
$ sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist

Now you’ll be able to launch MySQL at start-up. If you’d like to test it, you’ll have to kill the MySQL server daemon if you started it earlier.
$ mysqladmin -uroot shutdown

Now start MySQL using launchd:
$ sudo launchctl load /Library/LaunchDaemons/com.mysql.mysqld.plist

Getting PHP & MySQL to Play Together

When the MySQL server runs, it creates a socket file for other processes to communicate with it. In Leopard, that file is located in /private/tmp. Problem is, by default PHP’s built-in settings mean that PHP expects to find the socket at /var/mysql/mysql.sock.

You can fix that a few ways. You can create a symbolic link in the filesystem or you could edit PHP’s configuration so it knows where the socket file is. I prefer to create a symbolic link that way we don’t have to change configuration files. I like to mess as little with configuration files as possible so that upgrades are easier later on since upgrades can sometimes alter configuration files. Some people may consider the symbolic link method a hack, but I find it’s the method of least interference.

The Link

You’ll have to create a directory for the symbolic link.
$ sudo mkdir /private/var/mysql

Now you can create the link.
$ sudo ln -s /private/tmp/mysql.sock /private/var/mysql/mysql.sock

Fixing the PHP Config

This is probably the intended “correct” way to solve the problem since config files are intended to allow you to customize an installation for your environment.

In this case you’ll have to edit the PHP config file at /private/etc/php.ini. If you don’t have a php.ini file there, you’ll have to make one.
$ sudo cp /private/etc/php.ini.default /private/etc/php.ini

Now edit the file and change the following settings:

  • mysql.default_socket = /tmp/mysql.sock (Line 760 in the default php.ini)
  • mysqli.default_socket = /tmp/mysql.sock (Line 795 in the default php.ini)

Now you have a fully working copy of MySQL that PHP can use.

Enable PHP

As a side note, in order for Apache to use PHP, you have to edit the httpd.conf file (/private/etc/apache2/httpd.conf) to enable PHP. Find the PHP module line (line 114 in the default file) that looks like this:
#LoadModule php5_module        libexec/apache2/libphp5.so

Just uncomment the line by deleting the pound-sign (#).
LoadModule php5_module        libexec/apache2/libphp5.so

Save the file and restart Apache and PHP will be working.

More Help

If what I’ve shared still doesn’t get you up and running, you might check these sites for more tidbits than I covered. These sites were the resources I found most helpful getting my rig running right.

2 Comments

Leave a Reply