在Mac OSX 10.6上安装Ruby on Rails的mysql2 gem

35

我在安装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.
13个回答

63

看起来无法找到MySQL库。 你安装了MySQL吗?
尝试通过homebrew安装:

brew install mysql

或者通过MacPorts或Fink或您喜欢的其他方式进行安装。

然后再尝试使用以下命令:

gem install mysql2

谢谢!这个解决了!现在当我启动Rails服务器并单击“关于您的应用程序环境…”时,会出现以下消息:Mysql2 :: Error无法通过套接字'/tmp/mysql.sock'(2)连接到本地MySQL服务器 Rails.root:/Users/useruser/example 你知道这是什么意思吗? - Maze
你已经配置好了MySQL服务器了吗?执行"mysql.server start"命令即可完成,不要忘记为root用户配置访问权限。 - ALoR
这是出现了:. 错误!pid 文件的管理器退出,而没有更新文件。 - Maze
在进行任何操作之前,您必须执行“mysql_install_db”命令并按照说明进行操作。 - ALoR
1
嗨,我遇到了这个问题:http://imageshack.us/photo/my-images/857/screenshot20110610at230.png/ 有人可以帮我解决一下吗?谢谢! :) - Maze
我遇到了同样的问题。1) ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go) 2)brew install mysql 3)gem install mysql2 - alexndm

60

这个命令对我有用:

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

2
如果您已经使用从MySQL网站提供的二进制文件在Mac上安装了它,则此方法可行。 - gmaliar
谢谢,正如Guy上面所说的,如果您使用mysql网站进行安装,则可以运行此操作。 - Brenden
这对我也起作用了,但我没有使用MySQL网站进行安装。 - Siddharth
2
这对我有用,但我使用了不同的目录。我使用了 locate mysql | grep include 并找到了另一个目录。整个命令:gem install mysql2 -v '0.3.21' -- --srcdir=/usr/local/Cellar/mysql/8.0.12/include - mario199
我爱你如此深沉。 - cody
适用于苹果M1,谢谢! - bragboy

28

以下命令对我来说成功了。

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

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

谢谢,我在macOS 10.12.5上安装了MariaDB 10.2(通过brew),使用Ruby 2.3.4和Rails 5.0.3后,它对我有效。 - smile2day
2
针对OSX 10.13.5和通过Homebrew安装的MySQL的快速提示: --srcdir是:/usr/local/include/mysql - fmquaglia

20

我不需要在 Mac OS X 上安装 MySQL,因为我已经在 Vagrant box 上安装了 MySQL。因此,我只安装了 mysql-connector-c。

brew install mysql-connector-c
gem install mysql2

7

修复手动安装 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

只有这个对我起作用了! - SahilW
今天的救命稻草! - Hegwin

4
如果您使用 Homebrew 安装了 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 = 您已安装的mysql版本,请使用ls /usr/local/Cellar/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

我爱你!'u救了我的命:D - Leo Gasparrini

4
以下对我有帮助:

以下对我有帮助

brew install mysql 
brew install mysql-connector-c
gem install mysql2

3
您需要在Mac OSX 10.6 的 64 位架构上安装此 gem 时指定一些额外的环境变量:
env ARCHFLAGS="-arch x86_64" gem install mysql2

如果您使用rvm,您可以将以下内容添加到 ~/.rvmrc 文件中作为默认选项:
rvm_archflags="-arch x86_64"

没成功 :( 可能是我做错了,你能详细解释一下怎么做吗? - Maze
你是否遇到了相同的错误或不同的错误?如果你的mysql安装在默认位置以外,你可能还需要指定其他标志,比如--with-opt-dir。 - Pan Thomakos
我遇到了相同的错误。它说:“Gem文件将保留在 /Users/useruser/.rvm/gems/ruby-1.9.2-p136/gems/mysql-2.8.1 以供检查。”也许这可以帮助解决问题?是的,我正在使用 rvm。 - Maze
我在你和ALoR的帮助下解决了问题。感谢您格式化我的问题! :) - Maze

1

这是对我有效的方法:

gem install mysql2 -v 'x.x.x' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
  • 将x.x.x替换为您想要安装的mysql2 gem版本

1

请确保在Mac上通过Homebrew安装了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

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