使用Mac OSX 10.6为Ruby on Rails安装mysql2 gem

时间:2011-03-23 18:16:44

标签: mysql ruby-on-rails ruby osx-snow-leopard gem

我在安装mysql2 gem时遇到问题。

当我gem install mysql2时出现:

Marks-MacBook-Pro:~ Mark$ gem install mysql2
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
ERROR: Failed to build gem native extension.

/Users/useruser/.rvm/rubies/ruby-1.9.2-p136/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... yes
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... no
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.

Provided configuration options:
    --with-opt-dir
    --without-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/useruser/.rvm/rubies/ruby-1.9.2-p136/bin/ruby
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mlib
    --without-mlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-zlib
    --without-zlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-socketlib
    --without-socketlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-nsllib
    --without-nsllib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mygcclib
    --without-mygcclib
    --with-mysqlclientlib
    --without-mysqlclientlib


Gem files will remain installed in /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6 for inspection.
Results logged to /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql2-0.2.6/ext/mysql2/gem_make.out

当我使用rails server时,会出现这个问题:

Could not find gem 'mysql2 (>= 0, runtime)' in any of the gem sources listed in your Gemfile.

11 个答案:

答案 0 :(得分:50)

似乎找不到MySQL库。你安装了MySQL吗? 尝试通过homebrew安装它:

brew install mysql

或通过macports或fink或任何你喜欢的方式安装它,然后再试一次:

gem install mysql2

答案 1 :(得分:37)

这个命令对我有用:

gem install mysql2 -- --srcdir=/usr/local/mysql/include

答案 2 :(得分:22)

以下命令成功地为我工作。

  

x.x.x =您要安装的mysql2版本。

gem install mysql2 -v 'x.x.x' -- --srcdir=/usr/local/mysql/include

答案 3 :(得分:17)

我在Mac OS X上不需要MySQL,因为我在Vagrant盒子上安装了MySQL。因此,我刚刚安装了mysql-connector-c。

brew install mysql-connector-c
gem install mysql2

答案 4 :(得分:4)

如果您使用自制来安装mysql brew install mysql,这对我有用:

gem install mysql2 -v 'x.x.x' -- --with-mysql-config=/usr/local/Cellar/mysql/y.y.y/bin/mysql_config

x.x.x =您要安装的mysql2 gem的版本
y.y.y =您已安装ls /usr/local/Cellar/mysql的mysql版本,以便找到它。

获取mysql的版本

brew info mysql                                                                                                                                                                                                     
mysql: stable 5.7.19 (bottled)
               ...
/usr/local/Cellar/mysql/5.7.19 (322 files, 233MB) *

               ...

然后如果你想用bundle安装:

bundle config build.mysql --with-mysql-config=/usr/local/Cellar/mysql/y.y.y/bin/mysql_config

答案 5 :(得分:3)

您必须指定一些其他环境变量才能在Mac OSX 10.6的64位架构上安装此gem:

env ARCHFLAGS="-arch x86_64" gem install mysql2

如果您使用的是rvm,可以在〜/ .rvmrc文件中将其添加为默认选项:

rvm_archflags="-arch x86_64"

答案 6 :(得分:1)

我有完全相同的问题和错误尝试安装mysql2。我认为我的mamp安装的MySQL可以正常工作并花了几个小时来玩这些工作的路径 - 没有成功。

终于从ALoR发现了这篇文章并通过自制软件安装了一个新版本 - 但是 - 请务必按照自制程序中的所有说明进行操作!我错过了几个步骤,浪费了另一个小时来追踪这个问题 - 这是这些说明: https://stackoverflow.com/a/11061487/1241271

成功安装MySQL之后,我运行了sudo gem install mysql2,它就像一个魅力。

希望这可以帮助某人浪费几个小时,因为他们忽略了阅读文档(newb错误)。

答案 7 :(得分:0)

确保通过Homebrew在Mac上安装了openssl。

brew install openssl

Install mysql2 gem.

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

答案 8 :(得分:0)

我知道这个问题已经被回答了很多次,以下是对我有用的方法: 五月20,2019 OSX Mojave 10.14.4 使用EditLine包装器的osx10.14(x86_64)的MySQL Ver 14.14 Distrib 5.7.24 红宝石2.5.0p0(2017-12-25修订版61468)[x86_64-darwin17] Rails 5.2.1

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
gem install mysql2 -- --with-mysql-config=/usr/local/Cellar/mysql\@5.7/5.7.24/bin/mysql_config

将“ /usr/local/Cellar/mysql@5.7/5.7.24/bin/mysql_config”更改为您当前从“ / usr / local / Cellar / mysql”获得的有效路径

希望这会有所帮助!

答案 9 :(得分:0)

升级到Catalina(10.15.x)后,我遇到了同样的问题

我的Gemfile.lock定义了mysql2-0.3.20,无法安装。 但是,当我将其升级到mysql2-0.5.2

时,它起作用了
gem update mysql2 -- --srcdir=/usr/local/include/mysql/

请确保已将Brew与mysql一起安装。

brew install mysql
brew link mysql                                                                                                               

(链接/usr/local/Cellar/mysql/8.0.18 ...已创建95个符号链接)

但是,此后安装了gem mysql2,但捆绑更新mysql2无法正常工作。

但这另一个答案帮助了我 https://stackoverflow.com/a/39628463/110214

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

bundle update mysql2

答案 10 :(得分:0)

要修复手动安装gem:

brew install openssl
gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"

要修复所有捆绑包安装,请执行以下操作:

brew install openssl
bundle config --global build.mysql2 --with-opt-dir="$(brew --prefix openssl)"
bundle install