Perl DBD::mysql install fix on Mac OS X for error message "Library not loaded: libmysqlclient.18.dylib"


Issue:

While trying to install DBD::mysql on Mac OS X you receive the error message: "Library not loaded: libmysqlclient.18.dylib"

Failed Install Transcript:

sh-3.2# echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
sh-3.2# setenv PATH $PATH:"/usr/local/mysql/bin"
sh: setenv: command not found
sh-3.2# export PATH=$PATH:"/usr/local/src/mysql/bin"
sh-3.2# perl -MCPAN -e shell
Terminal does not support AddHistory.

cpan shell -- CPAN exploration and modules installation (v2.10)
Enter 'h' for help.

cpan[1]> install DBD::mysql
Reading '/var/root/.cpan/Metadata'
  Database was generated on Tue, 22 Sep 2015 15:29:02 GMT
Running install for module 'DBD::mysql'
Checksum for /var/root/.cpan/sources/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.032.tar.gz ok
Scanning cache /var/root/.cpan/build for sizes
............................................................................DONE
'YAML' not installed, will not store persistent state
Configuring C/CA/CAPTTOFU/DBD-mysql-4.032.tar.gz with Makefile.PL


PLEASE NOTE:

For 'make test' to run properly, you must ensure that the
database user 'wayne' can connect to your MySQL server
and has the proper privileges that these tests require such
as 'drop table', 'create table', 'drop procedure', 'create procedure'
as well as others.

mysql> grant all privileges on test.* to 'wayne'@'localhost' identified by 's3kr1t';

You can also optionally set the user to run 'make test' with:

perl Makefile.PL --testuser=username

I will use the following settings for compiling and testing:

  cflags        (mysql_config) = -I/usr/local/src/mysql/include   -g -fno-omit-frame-pointer -fno-strict-aliasing -arch x86_64
  embedded      (guessed     ) = 
  ldflags       (mysql_config) = 
  libs          (mysql_config) = -L/usr/local/src/mysql/lib -lmysqlclient
  mysql_config  (guessed     ) = mysql_config
  nocatchstderr (default     ) = 0
  nofoundrows   (default     ) = 0
  ssl           (guessed     ) = 0
  testdb        (default     ) = test
  testhost      (default     ) = 
  testpassword  (default     ) = 
  testport      (default     ) = 
  testsocket    (default     ) = 
  testuser      (guessed     ) = wayne

To change these settings, see 'perl Makefile.PL --help' and
'perldoc DBD::mysql::INSTALL'.

Checking if your kit is complete...
Looks good
Using DBI 1.631 (for perl 5.018002 on darwin-thread-multi-2level) installed in /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/auto/DBI/
Writing Makefile for DBD::mysql
Writing MYMETA.yml and MYMETA.json
  CAPTTOFU/DBD-mysql-4.032.tar.gz
  /usr/bin/perl Makefile.PL -- OK
Running make for C/CA/CAPTTOFU/DBD-mysql-4.032.tar.gz
cp lib/DBD/mysql.pm blib/lib/DBD/mysql.pm
cp lib/DBD/mysql/INSTALL.pod blib/lib/DBD/mysql/INSTALL.pod
cp README.pod blib/lib/DBD/README.pod
cp lib/DBD/mysql/GetInfo.pm blib/lib/DBD/mysql/GetInfo.pm
cp lib/Bundle/DBD/mysql.pm blib/lib/Bundle/DBD/mysql.pm
cc -c  -I/System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/auto/DBI -I/usr/local/src/mysql/include   -g -fno-omit-frame-pointer -fno-strict-aliasing -arch x86_64 -DDBD_MYSQL_INSERT_ID_IS_GOOD -g  -arch i386 -arch x86_64 -g -pipe -fno-common -DPERL_DARWIN -fno-strict-aliasing -fstack-protector -Os   -DVERSION=\"4.032\" -DXS_VERSION=\"4.032\"  "-I/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE"   dbdimp.c
/usr/bin/perl -p -e "s/~DRIVER~/mysql/g" /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/auto/DBI/Driver.xst > mysql.xsi
/usr/bin/perl /System/Library/Perl/5.18/ExtUtils/xsubpp  -typemap /System/Library/Perl/5.18/ExtUtils/typemap  mysql.xs > mysql.xsc && mv mysql.xsc mysql.c
Warning: duplicate function definition 'do' detected in mysql.xs, line 242
Warning: duplicate function definition 'rows' detected in mysql.xs, line 754
cc -c  -I/System/Library/Perl/Extras/5.18/darwin-thread-multi-2level/auto/DBI -I/usr/local/src/mysql/include   -g -fno-omit-frame-pointer -fno-strict-aliasing -arch x86_64 -DDBD_MYSQL_INSERT_ID_IS_GOOD -g  -arch i386 -arch x86_64 -g -pipe -fno-common -DPERL_DARWIN -fno-strict-aliasing -fstack-protector -Os   -DVERSION=\"4.032\" -DXS_VERSION=\"4.032\"  "-I/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE"   mysql.c
Running Mkbootstrap for DBD::mysql ()
chmod 644 mysql.bs
rm -f blib/arch/auto/DBD/mysql/mysql.bundle
LD_RUN_PATH="/usr/local/src/mysql/lib" /usr/bin/perl myld cc -mmacosx-version-min=10.10.5  -arch i386 -arch x86_64 -bundle -undefined dynamic_lookup -fstack-protector dbdimp.o mysql.o  -o blib/arch/auto/DBD/mysql/mysql.bundle 	\
	   -L/usr/local/src/mysql/lib -lmysqlclient  	\
	  
chmod 755 blib/arch/auto/DBD/mysql/mysql.bundle
cp mysql.bs blib/arch/auto/DBD/mysql/mysql.bs
chmod 644 blib/arch/auto/DBD/mysql/mysql.bs
Manifying blib/man3/Bundle::DBD::mysql.3pm
Manifying blib/man3/DBD::mysql::INSTALL.3pm
Manifying blib/man3/DBD::mysql.3pm
Manifying blib/man3/DBD::README.3pm
  CAPTTOFU/DBD-mysql-4.032.tar.gz
  /usr/bin/make -- OK
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/00base.t ........................... 1/6 
#   Failed test 'use DBD::mysql;'
#   at t/00base.t line 15.
#     Tried to use 'DBD::mysql'.
#     Error:  Can't load '/private/var/root/.cpan/build/DBD-mysql-4.032-weGLMJ/blib/arch/auto/DBD/mysql/mysql.bundle' for module DBD::mysql: dlopen(/private/var/root/.cpan/build/DBD-mysql-4.032-weGLMJ/blib/arch/auto/DBD/mysql/mysql.bundle, 2): Library not loaded: libmysqlclient.18.dylib
#   Referenced from: /private/var/root/.cpan/build/DBD-mysql-4.032-weGLMJ/blib/arch/auto/DBD/mysql/mysql.bundle
#   Reason: image not found at /System/Library/Perl/5.18/darwin-thread-multi-2level/DynaLoader.pm line 194.
#  at (eval 8) line 2.
# Compilation failed in require at (eval 8) line 2.
# BEGIN failed--compilation aborted at (eval 8) line 2.
Bailout called.  Further testing stopped:  Unable to load DBD::mysql
FAILED--Further testing stopped: Unable to load DBD::mysql
make: *** [test_dynamic] Error 255
  CAPTTOFU/DBD-mysql-4.032.tar.gz
  /usr/bin/make test -- NOT OK
//hint// to see the cpan-testers results for installing this module, try:
  reports CAPTTOFU/DBD-mysql-4.032.tar.gz
Failed during this command:
 CAPTTOFU/DBD-mysql-4.032.tar.gz              : make_test NO

cpan[2]> 

The Fix:

ln -s /usr/local/mysql-5.6.10-osx10.7-x86_64/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib