mysql2宝石在使用Homebrew安装的MySQL 5.6.12版本的OS X上编译失败。

39

我使用Homebrew更新了所有已安装的软件包。MySQL升级到5.6.12(从大约5.5.27):

$ mysql --version
mysql  Ver 14.14 Distrib 5.6.12, for osx10.8 (x86_64) using  EditLine wrapper

现在mysql2 gem无法编译了:

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

        /Users/pupeno/.rvm/rubies/ruby-1.9.3-p429-perf/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/pupeno/.rvm/rubies/ruby-1.9.3-p429-perf/bin/ruby
    --with-mysql-config
    --without-mysql-config

文件mysql.h位于/usr/local/Cellar/mysql/5.6.12/include/mysql.h。有什么想法吗?

/usr/local中没有出现mysql.h文件,因为符号链接似乎比它应该的深:

$ ls -la /usr/local/include/mysql
lrwxr-xr-x  1 pupeno  admin  36 21 Jun 15:18 /usr/local/include/mysql@ -> ../Cellar/mysql/5.6.12/include/mysql

取而代之

/usr/local/Cellar/mysql/5.6.12/include

话虽如此,我手动修复了符号链接,但编译仍然失败。所以我卡住了。


1
你是否已经安装了MySQL开发库? - Hunter McMillen
@HunterMcMillen 我以为所有的开发文件都随着mysql brew包一起安装了。我错过了什么吗? - pupeno
Homebrew + rbenv 对我一直都很有效。这可能是 RVM 的问题吗?另外,mysql.h 是否确实存在于你认为的位置? - tadman
@tadman 在升级之前它是可以工作的,mysql.h也在我所描述的位置。我不确定rvm怎么会搞砸这个,但我不排除这个可能性。 - pupeno
这里有相同的问题,已经找到解决方法了吗? - Rubytastic
同样的问题,尝试切换回旧版本的Ruby,但没有帮助,我能想到的唯一其他事情就是MySQL的升级搞砸了一些东西。 - iain
7个回答

33

我曾经遇到同样的问题,但我成功地解决了它。我尝试了很多方法,不确定哪个起了作用,但似乎升级到MySQL 5.6.10 已经解决了问题。

卸载MySQL 5.6.12:

brew unlink mysql
brew uninstall mysql

进入Homebrew目录:

cd /usr/local

前往版本5.6.10(您可以通过运行brew versions mysql查找版本列表):

git checkout 48f7e86 Library/Formula/mysql.rb

然后重新安装mysql:

brew install mysql

现在gem install mysql2对我来说可用了。

我还从源码(brew install mysql --build-from-source)安装了mysql,但是那并没有解决问题,但可能是问题的一部分。


7
Iain的答案对我很有帮助。在Homebrew 0.9.4中(不确定其他版本),您可以在降级到5.6.10之后使用brew pin mysql,这将防止它在使用brew upgrade进行常规升级时被更新。 - B. Ruschill
如果你使用brew uninstall命令,会删除你的datadir吗?我不确定,所以在我的情况下,我使用了brew switch命令切换回我已经安装的旧版mysql版本,因为升级到最新版本对我来说并不是很重要。 - stockli

23

就这样 ;)

gem install mysql2 -- --with-mysql-config=/usr/local/Cellar/mysql/5.6.10/bin/mysql_config

14

1
只有在该路径上实际安装了mysql 5.6.10,此方法才能正常工作。 - Jan
1
运行得很好!只需要确保路径指向已安装的MySQL版本即可。(在我的情况下,我需要针对5.1编译) - Chris Bloom
1
这会重写 ~/.bundle/config 文件。 - Yuki Matsukura

11

这里的最佳答案已经过时了...酿酒者已经修复了mysql的问题:

brew update
brew upgrade mysql
gem install mysql2

这将把mysql更新到最新版本,在OSX上似乎可以很好地与mysql2 gem一起使用。


11

我必须指定ldflagscppflags才能正确编译,就像这样:

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

2
这是我在2020年1月唯一有效的方法;请注意,他指示的标志在运行“brew info mysql@5.6”时会列出。 - subelsky
1
这也是我唯一可行的方法,可能是因为升级到openssl 1.1的缘故。gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include - spume
1
这解决了我的 ld: library not found for -lssl clang: error: linker command failed with exit code 1 (use -v to see invocation) mysql2 错误。 - jmoon90

6

虽然有更加新的MySQL版本,但是对于一个安装在Mac OS X Mojave上的旧项目,我仍然需要运行MySQL 5.6并将其链接到Rails 5应用程序。

我配置了bundler,然后安装了bundle,一切就准备就绪了。

$> brew install mysql@5.6
$> bundle config build.mysql2 --with-mysql-config=/usr/local/Cellar/mysql\@5.6/5.6.42/bin/mysql_config
$> bundle install

请注意,在路径中需要更新 MySQL 发行版版本。

1

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