rails3.0 安装mysql2 失败,发生lmysqlclient 错误

rails3.0使用的mysql驱动是mysql2,默认安装使用 bundle install,在安装过程中出现如下错误

/opt/ruby/bin/ruby extconf.rb
checking for rb_thread_blocking_region()… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lm… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lz… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lsocket… no
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lnsl… yes
checking for mysql_query() in -lmysqlclient… no
checking for main() in -lmygcc… no
checking for mysql_query() in -lmysqlclient… no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers. Check the mkmf.log file for more
details. You may need configuration options.

解决方法

既然要安装 mysql2 驱动,我们完全可以抛开bundle,而使用传统的gem的安装方式,并指明mysql的以下几个选项即可
sudo gem install mysql2 -- --with-mysql-dir=/opt/mysql --with-mysql-include=/opt/mysql/include/mysql --with-mysql-lib=/opt/mysql/lib/mysql/

然后再运行 bundle intall顺利完成其它gem组件的安装。看到网上有的解决方法是安装mysql-deve这个库,其实知道原因后对症下药即可,本博没有安装mysql-deve,仅是指明了mysql的相关选项,即可解决问题。

PS:bundle这个gem真的被集成到rails3.0里了。