如何在Ruby 1.9.3下安装mysql2 gem?在2.0.0下可以工作。

6
我在我的电脑上安装mysql2 gem遇到了问题。
我使用的是OS X 10.8系统,已经安装了最新版本的Xcode和通过Homebrew安装的mySql。所有的rubies均是通过最新的RVM安装的。
在ruby 2.0.0-p195版本下,我可以轻松地成功安装mysql2。
[~]$ rvm use 2.0.0                                                              [ruby-1.9.3-p429]
Using /Users/aselder/.rvm/gems/ruby-2.0.0-p195
[~]$ gem in mysql2                                                              [ruby-2.0.0-p195]
Fetching: mysql2-0.3.11.gem (100%)
Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.11
1 gem installed

在Ruby 1.9.3和1.8.7中,无法找到mysql.h文件。
    [~]$ rvm use 1.9.3                                                              [ruby-1.9.3-p429]
    Using /Users/aselder/.rvm/gems/ruby-1.9.3-p429
    [~]$ gem in mysql2                                                              [ruby-1.9.3-p429]
    Fetching: mysql2-0.3.11.gem (100%)
    Building native extensions.  This could take a while...
    ERROR:  Error installing mysql2:
        ERROR: Failed to build gem native extension.

            /Users/aselder/.rvm/rubies/ruby-1.9.3-p429/bin/ruby extconf.rb
    checking for rb_thread_blocking_region()... yes
    checking for rb_wait_for_single_fd()... yes
    checking for mysql.h... no
    checking for mysql/mysql.h... no
    -----
    mysql.h is missing.  please check your installation of mysql and try again.
    -----
    *** 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.

    Provided configuration options:
        --with-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/Users/aselder/.rvm/rubies/ruby-1.9.3-p429/bin/ruby
        --with-mysql-config
        --without-mysql-config


    Gem files will remain installed in /Users/aselder/.rvm/gems/ruby-1.9.3-p429/gems/mysql2-0.3.11 for inspection.
    Results logged to /Users/aselder/.rvm/gems/ruby-1.9.3-p429/gems/mysql2-0.3.11/ext/mysql2/gem_make.out

我也尝试了各种命令行选项来让gem找到mysql的安装位置:

例如: gem install mysql2 -- --with-mysql-config=/usr/local/opt/mysql/bin/mysql_config


1
我也遇到了完全相同的问题,但我还没有找到解决办法。 - vanhowen
我遇到了完全相同的问题。我使用的是全新的机器(10.8)。Ruby 2.0可以正常运行,但1.9.2和1.9.3会报告缺少mysql.h文件的错误。 - fflyer05
你的MySQL版本是什么?我使用5.6.12时遇到了同样的问题。卸载它并安装5.6.10解决了问题。 - Fuad Saud
5个回答

4
我找到了这个答案,它解决了我的问题:Error installing mysql2: Failed to build gem native extension 引用如下:
在Ubuntu/Debian以及其他使用aptitude的发行版上:
sudo apt-get install libmysql-ruby libmysqlclient-dev

在Red Hat/CentOS和其他使用yum的发行版上:

sudo yum install mysql-devel

在Mac OS X上使用Homebrew:

brew install mysql

我使用的是Debian系统,在运行上述命令后,我执行了以下命令:

gem install mysql2

它很好地运行了。


1

我解决了安装gem的问题,只需要在mysql服务器启动时进行安装!

brew install mysql

mysql.server start

bundle install

在 ruby-1.9.3-p125 下。
希望这可以帮到你。

0
我对此有一个非常愚蠢的解决方案:我打开了Xcode并接受了许可协议。更新下载了一个新版本,直到手动接受新协议之前,我无法编译任何东西。

0

我通过使用Homebrew将MySQL从5.6降级到5.5来解决了这个问题。无法提供更详细的解释,但它已经修复了。


我目前也遇到了同样的问题,正在使用5.5.31版本。 - kyle

0
尝试使用Homebrew安装mysql,并使用Bundler安装mysql2 gem。
brew update
brew install mysql

请将以下内容添加到您的 Gemfile 文件中。
gem "mysql2"

运行:

bundle install

1
我已经尝试过这个方法,但仍然出现了@aselder看到的相同错误。 - vanhowen
好的,尝试跟随这个人的答案:https://dev59.com/OGbWa4cB1Zd3GeqPYqCQ 希望这能解决你们的问题 :( - Pierre-Louis Gottfrois

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接