Nov
25
Perl DBD::mysql on OS X Lion
Filed Under Computers & Tech, System Administration on November 25, 2011 at 5:12 pm
I ran into a problem this week when the perl libraries for accessing MySQL databases refused to work on Mac OS X Lion. I did a ‘regular’ install:
- Install the 64bit version of MySQL Community Server (being sure to use the .dmg version so as to get the .pkg installer)
- use CPAN to install the needed database libraries:
$ perl -MCPAN -e shell
cpan[1]> install Bundle::DBI
cpan[1]> install DBD::mysql
There were no errors during the install, so I assumed all we well, until I tried to actually use the libraries to access a database that is! Using DBI to try connect to a MySQL database gave the following error:
install_driver(mysql) failed: Can't load '/Library/Perl/5.12/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle' for module DBD::mysql: dlopen(/Library/Perl/5.12/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle, 1): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/Perl/5.12/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle Reason: image not found at /System/Library/Perl/5.12/darwin-thread-multi-2level/DynaLoader.pm line 204.
After a lot of googling I found plenty of people with the same problems, including people on Snow Leopard, so I figured this was not a new problem. I tried a number of the suggested solutions, and most did not work, but after two days of trying, I found one that did, and it was wonderfully simple!
The problem is that the MySQL libraries are not in the OS’s library path, so they are not being found, most of the proposed solutions tried to tackle the problem at compile time, or to use simlinks to hack the libraries into the path, but like I say, these solutions didn’t work for me. What did work is simply updating the library path in my environment!
If you run the command below before executing your Perl script the library is found and all is well!
export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"
It’s a little awkward to do this all the time, so I added the line to my ~/.bash_profile file, and now it just works for me!
I spent two frustrating days trying to fix this, so hopefully I’ll save others some time by sharing my solution.
Hey. I’ve spent two days on this. Somebody at PerlMonks made the problem go away by replacing the regular MySQL with the MacPorts distro. That didn’t work for me, though. So I revisited your hack — but I don’t know enough about which “lib” DBD::MySQL wants to find. I tried this line in .bashrc:
export DYLD_LIBRARY_PATH=”/opt/local/lib/mysql5/$DYLD_LIBRARY_PATH”
which seems be where the thing DBD::MySQL is looking for resides.
Any suggestions appreciated.
Bruce
Running MythWeb on Mac OS X, the server is not serving video due to a Perl / MySQL problem complaining of an error similar to what you describe. Terminal is not starting in bash but I added the line to .bash_profile and it did not help. Any help is greatly appreciated.
Bruce & Jean-Luc, I’m wondering how you guys are trying to use DBD::MySQL – I was working on the commandline, so adding to my .bashrc was enough because that put it into the environment of my terminal. If you’re not using bash, or if you are trying to use DBD::MySQL from a web server or something, then the edit will not be in the process’s environment, so it will be as if you didn’t do anything. The bottom line is that you need the environment variable to set for the process that is trying to use DBD::MySQL.
thank. you. seriously saved me some time and hair-pulling
\
I have been looking for a solution for this problem for 2 weeks.
THANK YOU SO MUCH!!!!
And you mentioned that you added the line to your ~/.bash_profile file
I am very new to using terminal, could you explain this or link me to the solution? Thanks again!!!
Blake – the easiest commandline editor to use on OS X is pico, the keyboard commands a all listed along the bottom of the screen, so you can’t get lost.
So, you would start editing the file with:
pico ~/.bash_profile
You’d add in the line from the instructions, hit ctrl+x to exit, it will ask if you want to save, say yes. Then, for the changes to take effect, close the Terminal and open a fresh one.
Hope that helps,
Bart.
I am new in both mac and perl programming. I have done what you said, however the ~/.bash_profile, i wasn’t sure where to find it. and also after installing mysql when i run my perl program i get the following error. i would appreciate any help in advance.
dyld: lazy symbol binding failed: Symbol not found: _mysql_init
Referenced from: /Library/Perl/5.12/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle
Expected in: flat namespace
dyld: Symbol not found: _mysql_init
Referenced from: /Library/Perl/5.12/darwin-thread-multi-2level/auto/DBD/mysql/mysql.bundle
Expected in: flat namespace
Trace/BPT trap: 5
I kept on getting permission errors when using “perl -MCPAN -e shell”. You might want to try placing sudo before the commend.
Example: “sudo perl -MCPAN -e shell”
Well, first off much thanks for the post. I’ve been struggling with this for a while and finally gave up and used a linux box to do my work. But now I have to use my mac for some perl dbi stuff and I’ve upgraded to Mountain Lion (os/x 10.8) and it appears that gcc and clang and others have gone away (Thank you Mr. Apple).
Just wondering if there’s any alternative as without clang there’s no possibility to get the CPAN installs to work.
Kinda makes me think it’s time to just go Linux and forget Apple.
::sigh::
Thank you very much!
John – gcc etc have not gone away, they are, and always were, part of the free OS X Developer tools. You can now get them in the Mac App Store (still free).
Bart.
Awesome detective work, it works like a champ now, just like it’s supposed to.