Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, PHP, and whatever else I happen to feel like hacking at.

Adding PDO_MYSQL to Mac OS X Leopard Server

Jerry Stratton, June 2, 2009

Like many dynamic libraries on OS X Server, PDO_MYSQL runs into complexity due to architecture considerations. Fortunately, it doesn’t have the added complexity of requiring lots of additional libraries. All you need is the appropriate version of MySQL.

Here’s the first error I received trying to use an existing compilation:

  • PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20060613/pdo_mysql.so' - (null) in Unknown on line 0

This is caused by an architecture mismatch: the file exists, but it doesn’t contain the code needed for your architecture. On our Leopard Server, that meant x86_64: the file contained only i386.

You can check the architecture(s) available in a file using the “file” command:

  • $ file /usr/bin/php
  • /usr/bin/php: Mach-O universal binary with 2 architectures
  • /usr/bin/php (for architecture ppc7400): Mach-O executable ppc
  • /usr/bin/php (for architecture i386): Mach-O executable i386

After I solved that, PHP had no trouble loading the library, and it appeared to work fine—except that query results using pdo_mysql produced garbage characters. This turned out to be because I had downloaded the file for MySQL 5.1, and our MySQL server uses 5.0. (This could mean things are going to suck when we upgrade the server from 5.0 to 5.1.)

So here are the steps I used to compile and install pdo_mysql:

  1. Download and install MySQL for Mac OS X 10.5 (x86_64)
  2. cd /usr/local/mysql/lib
  3. sudo ln -s . mysql
  4. cd
  5. Download PDO_MYSQL-1.0.2.tgz
  6. gunzip PDO_MYSQL-1.0.2.tgz
  7. tar -xvf PDO_MYSQL-1.0.2.tar
  8. cd PDO_MYSQL-1.0.2
  9. phpize
  10. LDFLAGS='-arch x86_64' ./configure --with-pdo-mysql=shared,/usr/local/mysql
  11. make
  12. sudo make install
  13. In /etc/php.ini, add “extension=pdo_mysql.so”. You might have to copy php.ini.default to php.ini first.
  14. In /etc/php.ini, add “extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20060613/"”.

If you have to play around with it, remember that you can use “phpize --clean” and “make clean” to clear out your old attempts.

Also, you can install DMG files using (for example, for installing MySQL 5.0.82):

  1. hdiutil attach mysql-5.0.82-osx10.5-x86_64.dmg
  2. cd /Volumes/mysql-5.0.82-osx10.5-x86_64/
  3. sudo installer -verbose -pkg mysql-5.0.82-osx10.5-x86_64.pkg -target /
  4. cd
  5. hdiutil detach mysql-5.0.82-osx10.5-x86_64/

Hopefully this will be unnecessary in Snow Leopard, though now that MySQL is managed by Oracle who knows what the default open source database will be by then.

Oh, and don’t forget that you have to have Xcode installed to compile software.

Updated August 12 2009 for extension_dir note.

  1. <- Add SimpleXML Nodes
  2. Django Twitter ->