MySQL and PDO on OS X Leopard, Intel

I don't know if I'm missing something big, but getting Perl, Apache, PHP, PDO and MySQL to play nice on my Mac OS X 10.5 install on my Intel Core 2 Duo (Penryn) MacBook Pro hasn't been easy. This is partly thanks to Apache being compiled with x86_64 support, and Perl with i386 only.

64ishAnyway, Googling about, I notice that others have tried this arrangement with varying degrees of success. The easy answer is to install something like MAMP or replace the Apache and PHP installation with a custom build.

However, I prefer to keep my installation as stock as possible, and I'm not a huge fan of proprietary packaging systems like MacPorts and Fink. Don't get me wrong: those systems do what they're meant to do, and as a long-time FreeBSD user, I appreciate the approach. However, they don't fit into the Mac mindset too well, plus I'm too lazy to keep them up-to-date.

In other words, I want Apple to do it all for me, via Software Update where possible. The aim is to keep the stock Apache, PHP and Perl in place, and just add stuff.

The 10.5.3 Apache install does include PDO, but it's fairly crippled: the only drivers are the SQLite ones, even though standard "mysql" support is included. So, "pdo_mysql" will need to be installed... no problem: just install it as a module. But which architecture?

Since 10.5.3 Apache is compiled for i386 and x86_64, it'll run by default as 64-bit, requiring the 64-bit MySQL client libraries.

However, the included 10.5.3 Perl build is compiled only for i386, so if you want to build DBD::mysql for Perl as well, you'll need 32-bit MySQL client libraries.

The MySQL 5.1 official Leopard binary isn't currently a universal build, so right now if you download a binary distribution, you have to pick either 32-bit i386, or 64-bit x86_64. This will knacker either PHP/PDO (64-bit) or Perl/DBI (32-bit) So, assuming we don't want to replace -- or otherwise mess with -- the stock Apache, Perl and PHP, recompiling MySQL seems the way to go: we need to build a fat MySQL with both architectures.

To do this, download the MySQL source (I'm using 5.1.25), unzip and compile with something like:

MACOSX_DEPLOYMENT_TARGET=10.5 \
CFLAGS='-O3 -fno-common -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64' \
LDFLAGS='-O3 -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64' \
CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64' \
./configure \
'--disable-dependency-tracking' \
'--prefix=/usr/local/mysql' \
'--localstatedir=/usr/local/mysql/data' \
'--libexecdir=/usr/local/mysql/bin' \
'--with-comment=MySQL Community Server (GPL)' \
'--enable-thread-safe-client' \
'--enable-local-infile' \
'--with-big-tables'

make

sudo make install

You'll then probably want to do something like:

sudo -s

cd /usr/local

export MYSQL_VERSION=mysql-`mysql/bin/mysql_config --version`-osx10.5-universal

mv mysql $MYSQL_VERSION

ln -s $MYSQL_VERSION mysql

chown -R _mysql:staff $MYSQL_VERSION

cd mysql

ln -s share/mysql support-files

ln -s var data

bin/mysql_install_db --user=_mysql

I'm not sure whether this is the right procedure, but it works for me. A lot of this will change depending on your requirements and circumstances, so don't blame me if it kills your pets.

Next up is to install the PDO_mysql module. This is easy. Firstly, download the PHP source. As of Mac OS X 10.5.3, the current PHP is 5.2.5, but I downloaded 5.2.6 and it seemed fine. Unzip it, and go into the distribution directory (ie. php-5.2.6)

Then:

cd ext/pdo_mysql

phpize

MACOSX_DEPLOYMENT_TARGET=10.5 \
CFLAGS='-O3 -fno-common -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64' \
LDFLAGS='-O3 -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64' \
CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64 -arch ppc7400 -arch ppc64' \
./configure --prefix=/usr --with-pdo-mysql=/usr/local/mysql

make

sudo make install

You might need to add the following line to /etc/php.ini (creating that file, if necessary):

extension=pdo_mysql.so

Then, restart Apache and you should have a working PDO_mysql driver.

Installing DBD::mysql for the stock Perl is a different matter. If you just do a CPAN install, then the multiple -arch tags that the mysql_config from your new Universal build of MySQL will return are going to foul it up. So instead, you can just build DBD::mysql for i386, as Perl is going to be running in 32-bit mode anyway.

There are probably better, easier ways of doing this, but I resorted to just doing a manual DBD::mysql build, stating the flags by hand:

perl Makefile.PL \
 --cflags="-I/usr/local/mysql/include/mysql -Os -arch i386 -fno-common" \
 --libs="-L/usr/local/mysql/lib/mysql -lmysqlclient -lz -lm"

make

sudo make install

Anyway, if you can think of any improvements to the above routine, your comments are welcome.

Tags

Tracking

Comments

  1. I just noticed that this PHP installation prevents command-line PHP from working correctly, with the error:

    dyld: NSLinkModule() error
    dyld: Symbol not found: _core_globals
      Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20060613/pdo_mysql.so
      Expected in: dynamic lookup
    

    I suspect that this is because the php command is compiled 32-bit only. I'm not sure of the "proper" way to fix this, but I got around it by using a different php.ini file for Apache.

    First, delete the /etc/php.ini file (or at least, remove the offending extension lines)

    Then, create /etc/apache2/php.ini with the extension lines.

    Then, add:

            PHPIniDir /etc/apache2
    

    to the middle of /etc/apache2/other/php5.conf

  2. Another possible option would be to ignore this entire article, avoid the whole 64-bit mess and force Apache into 32-bit mode.

    Since the original Intel Macs used 32-bit Core processors, rather than the 32/64-bit Core 2, the version of Apache must be capable of running 32-bit, so there shouldn't be anything in Mac OS X that needs 64-bit Apache... right?

    I can think of two ways of forcing this: either modifying httpd by using lipo to remove all but the i386 executable, or by overriding /System/Library/LaunchDaemons/org.apache.httpd.plist

    Rather than modifying the file in /System/Library which is rarely a good idea, just override it by copying /System/Library/LaunchDaemons/org.apache.httpd.plist to /Library/LaunchDaemons/org.apache.httpd.plist and change:

                    <string>/usr/sbin/httpd</string>
                    <string>-D</string>
                    <string>FOREGROUND</string>
    

    to

                    <string>arch</string>
                    <string>-i386</string>
                    <string>/usr/sbin/httpd</string>
                    <string>-D</string>
                    <string>FOREGROUND</string>
    

    Activate it by rebooting or:

    sudo launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist
    sudo launchctl load /Library/LaunchDaemons/org.apache.httpd.plist
    

    Anyway, I don't know if this approach will work, but you should be able to operate everything in 32-bit mode, which should be a lot simpler. Use the standard 32-bit PDO, MySQL and so forth. If you actually need a 64-bit Apache, then you're on your own... the approach in the article above might be the way to go.

  3. Thanks Tom for this great article ! Facing the same problem, I did the following, based on your suggestion:

    - Install mysql (32 bit) from the package on mysql web site
    - Compile & install the pdo_mysql extension
    - Force apache to run in 32 bit following your advice

    It works great; now having PDO_MYSQL both in Apache and command line PHP.

  4. You could save us some time giving the compiled packets for x86_64 ;-)

  5. Heh... and keep track of all the versions? I think not! :)

    Anyway, I'm convinced the 32-bit mode approach in the comments above is a far better way than I suggested in the main body of the post.

  6. Thanks for the article. Good Information is rare in the MAC PHP development area. Forcing apache to run in 32 bit also allows you to use extensions packaged with other distributions like MAMP free and get things like mcrypt to work to allow you to run packages like Magento.

Leave a Reply

Powered by WP Hashcash