Problems installing DBD::mysql on OS X Snow Leopard

After a colleague showed me the excellent MySQL Workbench the other day I thought I’d install the packaged MySQL on my Macbook, to do a bit of Catalyst developing.

When it comes to extra Perl modules, I always install everything in my home directory, using local::lib – it keeps everything neat and tidy and means I don’t need to install anything under sudo.

However, I’ve just been struggling to get DBD::mysql to work properly with the packaged MySQL. It would compile just fine, but then refused to load for testing, seemingly unable to find the libmysqlclient dynamic library:

#   Failed test 'use DBD::mysql;'
#   at t/00base.t line 21.
#     Tried to use 'DBD::mysql'.
#     Error:  Can't load '/Users/markp/.cpan/build/DBD-mysql-4.018-hYHpKY/blib/arch/auto/DBD/mysql/mysql.bundle' for module DBD::mysql:
dlopen(/Users/markp/.cpan/build/DBD-mysql-4.018-hYHpKY/blib/arch/auto/DBD/mysql/mysql.bundle, 2): Library not loaded:
libmysqlclient.16.dylib

After a bit of Googling around, various resources led me to the fact the compiled binary was lacking an RPATH. So all I needed to do was link the binary with an RPATH. But it seems things are not as simple on OS X.

According to Joe Di Pol’s blog, OS X works slightly backwards to what most of us from Linux and Solaris backgrounds understand – compiled binaries look at a dynamic library, which in turns says where it is, rather than the traditional way of thinking which is to include a library search path in the compiled binary.

To see what I mean, look at the mysqlclient dynamic library with otool:

DBD-mysql-4.018-hYHpKY$ otool -D `mdfind libmysqlclient.16.dylib`
/usr/local/mysql-5.5.9-osx10.6-x86_64/lib/libmysqlclient.16.dylib:
libmysqlclient.16.dylib

There is no path listed there, which means binaries compiled to use the library won’t be able to find it unless it’s in the system search path of /usr/lib.

We can fix this with install_name_tool though:

DBD-mysql-4.018-hYHpKY$ sudo install_name_tool -id /usr/local/mysql-5.5.9-osx10.6-x86_64/lib/libmysqlclient.16.dylib
/usr/local/mysql-5.5.9-osx10.6-x86_64/lib/libmysqlclient.16.dylib

DBD-mysql-4.018-hYHpKY$ otool -D `mdfind libmysqlclient.16.dylib`
/usr/local/mysql-5.5.9-osx10.6-x86_64/lib/libmysqlclient.16.dylib:
/usr/local/mysql-5.5.9-osx10.6-x86_64/lib/libmysqlclient.16.dylib

Now when we run the tests:

DBD-mysql-4.018-hYHpKY$ make testPERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')"
t/*.t
t/00base.t .................. ok
t/10connect.t ............... ok

It works!

There may be a better way to do this, and if I find it I’ll update this post. Otherwise, that works just fine for me.

Leave a comment ?

11 Comments.

  1. Thanks, your blog saved me hours of time wasted.

  2. I echo Aaron’s appreciation. I have tried intermittently for months to get the MySQL driver installed, and I finally succeeded after finding this post this afternoon. Thanks!

  3. while installing DBD::mysql I am facing a big problem for a number of days, this message prompts in the terminal when i try to install it:

    CPAN: Storable loaded ok (v2.18)
    Going to read /Users/ahmedarslan/.cpan/Metadata
    Database was generated on Thu, 18 Aug 2011 13:37:41 GMT
    Cannot create directory /var/root/.cpan/prefsCatching error: “\cJ\cI…propagated at /System/Library/Perl/5.10.0/CPAN.pm line 3266, line 1.\cJ” at /System/Library/Perl/5.10.0/CPAN.pm line 281
    CPAN::shell() called at -e line 1
    can any one help me out ??

    • Have you run CPAN as the root user at some point? Perhaps under sudo? It looks like it’s referencing a path in /var/root, which your user now cannot access.

  4. I copied the libmysqlclient.16.dylib file to /usr/lib and it worked fine for Bugzilla.

    Thanks for the post

    • Hi Kevin. Thanks for dropping by. That is indeed one solution, as it’s the OS default look up path for libraries. But it’s a bit ‘untidy’ – if you upgrade, you could forget it’s there, not put the new one in place, etc etc. I realise the compilation solution is also fraught with ‘ah but!’s, but I feel it’s a bit more stable.

  5. Install apache-php-mysql and perl on Macbook « Linux tutorials - pingback on January 31, 2012 at 3:01 am
  6. Thanks for the post! There is an alternative suggestion in a DBD::myql bug thread to point mysql.bundle at the MySQL dylib directly using install_name_tool -change.

  7. I did the above with the install tool and it WORKS!!! I am using 10.7.4 OSX server. Thank God for this solution :-)

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks and Pingbacks: