无法在Mac OS X上安装mysql gem

4

我使用http://dev.mysql.com/downloads/mysql上的安装程序安装了MySQL服务器。然后,按照几个指南的说明,我想通过以下命令安装mysql gem。

sudo gem install mysql

这不起作用,并输出以下内容。

Building native extensions.  This could take a while...
ERROR:  Error installing mysql:
    ERROR: Failed to build gem native extension.

        /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/bin/ruby extconf.rb --with-mysql-dir=/usr/local/mysql/
checking for mysql_ssl_set()... *** 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/luzi/.rvm/rubies/ruby-1.9.3-p429/bin/ruby
    --with-mysql-config
    --without-mysql-config
/Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:381:in `try_do': The compiler failed to generate an executable file. (RuntimeError)
You have to install development tools first.
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:461:in `try_link0'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:476:in `try_link'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:619:in `try_func'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:894:in `block in have_func'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:790:in `block in checking_for'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:284:in `block (2 levels) in postpone'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:284:in `block in postpone'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:254:in `open'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:280:in `postpone'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:789:in `checking_for'
    from /Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/1.9.1/mkmf.rb:893:in `have_func'
    from extconf.rb:45:in `<main>'


Gem files will remain installed in /Users/luzi/.rvm/gems/ruby-1.9.3-p429/gems/mysql-2.9.1 for inspection.
Results logged to /Users/luzi/.rvm/gems/ruby-1.9.3-p429/gems/mysql-2.9.1/ext/mysql_api/gem_make.out

我多次重新安装了ruby 1.9.3,同时也安装了XCode命令行工具。我也使用homebrew安装了mysql服务器(没有带任何东西)。
我还尝试了一些命令的选项,比如--with-mysql-dir=/usr/local/mysql--with-mysql-config=/usr/local/mysql/bin/mysql_config。传递标志(sudo env ARCHFLAGS="-arch x86_64" gem install mysql)也没有帮助。
更新:
我还尝试了mysql2 gem,结果出现了同样的错误,可能是因为mysql2 gem依赖于mysql gem。
更新2: mkmf.log的内容:
"/opt/local/bin/gcc-apple-4.2 -o conftest -I/Users/luzi/.rvm/rubies/ruby-1.9.3-p429/include/ruby-1.9.1/x86_64-darwin12.4.0 -I/Users/luzi/.rvm/rubies/ruby-1.9.3-p429/include/ruby-1.9.1/ruby/backward -I/Users/luzi/.rvm/rubies/ruby-1.9.3-p429/include/ruby-1.9.1 -I. -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE   -I/opt/local/include -I/usr/local/mysql/include  -Wno-null-conversion -Os -g -fno-strict-aliasing -arch x86_64  -O3 -ggdb -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration  -fno-common -pipe conftest.c  -L. -L/Users/luzi/.rvm/rubies/ruby-1.9.3-p429/lib -L/opt/local/lib -L. -L/usr/local/lib -L/opt/local/lib     -L/usr/local/mysql/lib -lmysqlclient  -lruby.1.9.1  -lpthread -ldl -lobjc "
cc1: error: unrecognized command line option "-Wno-null-conversion"
checked program was:
/* begin */
1: #include "ruby.h"
2: 
3: int main(int argc, char **argv)
4: {
5:   return 0;
6: }
/* end */

你尝试过mysql2 gem吗?我的理解是,mysql2已经成为标准选择。 - Rebitzele
是的,我也尝试过那个。同样的错误也出现在那里,可能是因为它依赖于mysql gem。 - lulezi
当你使用rvm时,为什么要sudo? - Sławosz
@Sławosz 在我参加的教程中也有这样的建议。我猜 rvm @global do gem install mysql 也可以工作。 - lulezi
3个回答

9
问题在这里解释:http://www.randomactsofsentience.com/2013/05/gem-install-mysql2-missing-mysqlh-on-os.html。简而言之:
修改文件/usr/local/mysql/bin/mysql_config的第120行左右。
cflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF -arch x86_64 " #note: end space!
cxxflags="-I$pkgincludedir  -Wall -Wno-null-conversion -Wno-unused-private-field -Os -g -fno-strict-aliasing -DDBUG_OFF -arch x86_64 " #note: end space!

to

cflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF -arch x86_64 " #note: end space!
cxxflags="-I$pkgincludedir  -Wall -Os -g -fno-strict-aliasing -DDBUG_OFF -arch x86_64 " #note: end space!

我现在使用的是Mavericks系统,有趣的是,那一行已经被调整以匹配第二行了... 不知何故,我仍然看到相同的错误。 - counterbeing
我曾经遇到过这个问题,通过使用brew安装mysql-client并覆盖了/usr/local/mysql/bin/mysql_config文件来解决。之前是通过dmg安装mysql而不是使用brew。 - stackdump

1
问题不在于 gem 本身。你缺少 XCode(从 AppStore 安装),可能也缺少 https://github.com/kennethreitz/osx-gcc-installer,正如错误日志所说:

您首先需要安装开发工具。

为了更好地解释这个问题,请像错误日志建议的那样查看 mkmf.log。

1
如上所述,我已安装了XCode和命令行工具。我甚至单独安装了你提供的OS X GCC编译器。很遗憾,这并不是解决方案。 - lulezi
你看过mkmf.log了吗?没有更多的信息,很难理解问题并帮助你。 - zekus
看起来是gcc然后:你能给出"gcc -v"的输出吗? - zekus
1
完美 :) 只需将解决方案作为答案添加并自行接受,这样更容易被看到,并且您也会获得声望 ;) - zekus
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/32389/discussion-between-lulezi-and-zekus - lulezi

0

我在 Fedora 21 上安装 mysql2psql 时遇到了问题,后来通过从 mysql.org 下载 mysql 源代码 tar.gz 并将 --with-mysql-config 设置为该提取目录中的 bin/mysql_config 文件来解决了这个问题。

gem install mysql2psql -- --with-mysql-config=/pathtodownloadedsource/mysql../bin/mysql_config


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