Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

macos - rails server fails to start with mysql2 using rvm & ruby 1.9.2-p0 on OSX 10.6.5

I'm getting the following error when I start rails server:

$ rails server
/Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6/lib/mysql2.rb:7:in `require':     dlopen(/Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6/lib/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.16.dylib (LoadError) 
Referenced from: /Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2- 0.2.6/lib/mysql2/mysql2.bundle
Reason: image not found - /Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-    
0.2.6/lib/mysql2/mysql2.bundle

I've installed mysql2 with the following command after the rvm use ruby-1.9.2-p0 command:

$ gem install mysql2 -- --with-mysql-dir=/usr/local/mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config
Building native extensions.  This could take a while...
Successfully installed mysql2-0.2.6
1 gem installed
Installing ri documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Client not known

I have mysql2 in my Gemfile as well as in the database.yml file and bundle install completes fine

$ bundle show mysql2
/Users/ssmith/.rvm/gems/ruby-1.9.2-p0/gems/mysql2-0.2.6

I understand the rails server error is due to it not knowing the mysql_config location on OSX, however on gem install I specified the correct location. Yet RVM's gem is not respecting that mysql_config location it seems.

Anyone have a solution to this?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem comes from the mysql2 gem missing the dynamic library from MySQL.

A cleaner solution than install_name_tool ... would need to update your DYLD_LIBRARY_PATH to add MySQL libs to it. To do so, update your ~/.bash_profile to add the MySQL library folder :

export DYLD_LIBRARY_PATH="/usr/local/mysql/lib:$DYLD_LIBRARY_PATH"

Note: You might want to update the MySQL location based on your install

This should keep things clean but also ensure that any gem or code requiring MySQL dynamic libraries will find them.

Reference : http://lightyearsoftware.com/2011/02/mysql-5-5-on-mac-os-x/

Reference Update July 2012: A change in OS X 10.8 makes the easy method above less elegant. If you set that variable, every time you run a setuid or setgid program, you get this warning on stderr:

dyld: DYLD_ environment variables being ignored because main executable (...) is setuid or setgid

Ruby developers using Phusion Passenger Standalone will see this message displayed in their console every five seconds. It gets really irritating, very fast.

I have filed a bug with Apple. It’s also at OpenRadar.

In the meantime, there is also a third way to fix the client library path problem that doesn’t require setting DYLD_LIBRARY_PATH (working around this 10.8 issue) or hacking .bundle files with install_name_tool:

$ brew install mysql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...