81

当我尝试安装 mysql2 gem 时,它失败了,没有明显的错误。有谁知道如何解决这个问题以便安装 mysql2?

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

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
checking for rb_thread_blocking_region()... no
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=/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/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 /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/mysql2-0.2.6/ext/mysql2/gem_make.out
4

11 回答 11

151

Ubuntu:

sudo apt-get install libmysqlclient-dev  #(mysql development headers)
sudo gem install mysql2 -- --with-mysql-dir=/etc/mysql/

而已!

结果:

Building native extensions. This could take a while...
Successfully installed mysql2-0.2.6
1 gem installed
Installing ri documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Result not known
Enclosing class/module 'mMysql2' for class Client not known
Installing RDoc documentation for mysql2-0.2.6...
Enclosing class/module 'mMysql2' for class Result not known
Enclosing class/module 'mMysql2' for class Client not known
于 2010-12-22T15:44:41.337 回答
41

您需要 mysql2 的 MySQL 开发头文件才能正确编译。这是必需的,因为大部分功能是用 C 语言编写的,随后与 MySQL 相关联。

一种解决方案是将 gem 安装程序明确指向您的 mysql 安装文件夹,如下所示

gem install mysql2 -- --with-mysql-dir=/usr/local/mysql
# or where ever you installed your mysql server to

或者通过将 mysql 服务器安装到已知位置(例如使用homebrew。)然后安装 gem

# install the mysql server locally
brew install mysql
# install the gem
gem install mysql2

这两种方法都要求您安装 XCode 才能拥有所需的 GCC 编译器。

于 2010-11-06T21:10:33.117 回答
30

您必须在机器上安装 64 位 MySQL,以及安装 xcode 时获得的构建工具。

于 2010-11-06T21:03:50.937 回答
27

安装 Brew 和 MySQL 后,我使用以下命令安装 mysql2 gem

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

如果您使用的是 Bundler,您可以使用以下命令告诉 bundler:

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

如此处所述:http: //gembundler.com/man/bundle-config.1.html

于 2012-02-19T12:16:00.723 回答
8

在 Mac OSX Moutain Lion 上,以下命令对我有用:

gem install mysql2 -- --srcdir=/usr/local/mysql/include
于 2012-12-10T10:32:18.413 回答
6

Ubuntu 15.04:

sudo apt-get install libmysqlclient-dev
sudo gem install mysql2

Ubuntu-16.04:

sudo apt-get install ruby-mysql2
sudo gem install mysql2

输出:

Building native extensions.  This could take a while...
Successfully installed mysql2-0.3.19
Parsing documentation for mysql2-0.3.19
Installing ri documentation for mysql2-0.3.19
Done installing documentation for mysql2 after 0 seconds
1 gem installed
于 2015-08-23T10:53:22.913 回答
2

您可能想在 MySQL 网站上查看此线程: http: //forums.mysql.com/read.php?116,178217,178217,尤其是 Scott Derrick 的回答:http ://forums.mysql.com/ read.php?116,178217,189357#msg-189357

更具体地说,尝试使用

sudo gem install mysql2 -- --with-mysql-dir=/usr/local/mysql

希望有帮助。

于 2010-11-06T21:04:31.853 回答
1

我挖了 2 天的互联网和 Stack Overflow,直到我找到这个链接,然后开始处理mysql2 的这张票,我才真正解决了这个问题。

使用我的设置(如票证中所述), -Wno-null-conversion -Wno-unused-private-field 的编译器开关会中断并给我一个不完全正确的错误,即:

mysql.h is missing. please check your installation of mysql and try again
于 2013-06-25T17:04:22.490 回答
1

在带有 SCL(软件集合)rh-mysql 的 Centos 6.x(7 应该可以工作)中:

scl enable rh-mysql56 bash
gem install mysql2 -v '0.4.5' -- --with-mysql-include=/opt/rh/rh-mysql56/root/usr/include --with-mysql-lib=/opt/rh/rh-mysql56/root/usr/lib64
于 2016-11-26T04:24:29.647 回答
1

我通过具体说明目录在哪里解决了这个问题,无需重新安装任何东西,使用 brew 或 macports 或其他任何东西,只需指定东西在哪里(只有一件事:我的 mac 上的 ruby​​ 是用 rvm 安装的,我不使用 os x 附带的默认值):

添加到你gem install mysql2下面的标志

  • --srcdir="..."- 包括
  • --with-mysql-dir="..."- mysql目录
  • --with-mysql-config="..."- mysql_config 文件

像这样gem install mysql2 --srcdir=/usr/local/mysql/include/ --with-mysql-dir=/usr/local/mysql --with-mysql-config=/usr/local/mysql/bin/mysql_config

于 2016-10-21T17:00:01.290 回答
0

我想如果你输入rvm use ruby-2.1.0@rails4.0 --create你的目录,那么bundle install它就可以工作。

于 2014-01-30T16:35:08.597 回答