Ruby gem mysql2安装失败

83
当我尝试安装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
11个回答

156

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

3
很遗憾,在OSX上没有apt可用。但是你的方法确实适用于Debian/Ubuntu。 - Holger Just
6
我不明白为什么这个回答得到了这么多赞,因为它与问题并不相符。 - WattsInABox
19
它获得了很多赞,因为其他人(包括我自己)发现它很有用。它解决了我的问题。 - John
2
问题是相同的,只不过发生在另一个操作系统上。 - Abe Petrillo
7
请注意,在Ubuntu 13上,至少不需要指定with-mysql-dir。只需安装libmysqlclient-dev,然后运行bundle install即可正常工作。 - Josh M.
显示剩余5条评论

42

为了正确编译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编译器。


The brew解决方案对我来说效果最好。如果你安装了Mac OS X的MySQL二进制文件,请确保使用64位版本。 - Jason
谢谢,--with-mysql-dir= 选项正是我需要解决问题的。 - Tobias Cohen
对我来说,--with-mysql-dir 实际上引起了一个问题。我只是将其忽略不计,并让它安装到默认目录。我只需使用 gem install mysql2 -v '0.3.18' 直接安装特定版本即可。 - user2490003
谢谢:这对我有用:==> sudo gem install mysql2 -- --with-mysql-dir=/usr/local/mysql - Arunabh Das

31

您的机器上必须安装64位的MySQL,以及在安装xcode时附带的编译工具。


3
解决了。通过下面的链接在我的电脑上安装了64位MySQL,一切都正常工作了。谢谢! - Tanner
21
没错 - brew install mysql 然后 gem install mysql 没有问题 - 谢谢! - froderik

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


1
如果您正在使用brew版本安装旧版本,例如brew install mysql51,则此方法也适用,gem install mysql2 -v'0.3.13' -- --with-mysql-config=/usr/local/Cellar/mysql51/5.1.71/bin/mysql_config(在Mountain Lion上)。 - nruth
在MacOS中,这个奇怪问题的唯一解决方案! - kevinluo201

8
在Mac OSX Moutain Lion上,以下命令适用于我:
gem install mysql2 -- --srcdir=/usr/local/mysql/include

1
Ubuntu 14.01同样适用! - Peter Kazazes
在macOS Mojave上也可以! - evgpisarchik

7

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

3

2
在CentOS 6.x(7也适用),使用SCL(软件集合)rh-mysql:
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

1
我解决了这个问题,方法是明确指定目录位置,不需要重新安装任何东西,使用brew或macports或其他方式,只需指定物品的位置(仅有一件事:我的Mac上安装了rvm的ruby,我没有使用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


1
我在互联网和Stack Overflow上搜索了两天,直到我找到这个链接并开始处理mysql2的这张票据,我才真正解决了这个问题。根据我的设置(如票据中所述),编译器开关-Wno-null-conversion -Wno-unused-private-field会导致错误,并且给出的错误信息不完全正确:
mysql.h is missing. please check your installation of mysql and try again

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