Ruby gem MySQL2安装失败


75

当我尝试安装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

Answers:


29

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


3
那解决了。通过下面的链接在我的计算机上安装了64位MySQL,一切正常。谢谢!
Tanner

18
是的-brew install mysql然后gem install mysql 没有问题-谢谢!
froderik

146

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
不幸的是,apt在OSX上尚不可用。但是您的方法确实适用于Debian / Ubuntu。
Holger

6
我不明白当它与问题不符时,它会有如此多的否决。
WattsInABox 2011年

19
它的投票如此之多,因为其他人(例如我自己)发现这很有用。它解决了我的问题。
约翰,

2
问题是相同的问题,但是在不同的操作系统上
Abe Petrillo

6
请注意,至少在Ubuntu 13上,不必指定with-mysql-dir。只需安装libmysqlclient-dev然后运行bundle install,它应该可以工作。
Josh M.

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编译器。


冲泡解决方案对我来说效果最好。如果安装Mac OS X MySQL二进制文件,请确保使用64位版本。
杰森

谢谢,这--with-mysql-dir=正是我解决问题所需要的。
Tobias Cohen

对我来说,这--with-mysql-dir实际上是一个问题。我只是将其保留,然后将其安装到默认目录。我只是直接用gem install mysql2 -v '0.3.18'
user2490003

谢谢:这对我
有用

25

安装了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(在山狮上)
nruth

解决MacOS中这个奇怪问题的唯一方法!
kevinluo201


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


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

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


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

0

我猜想,如果您输入rvm use ruby-2.1.0@rails4.0 --create目录,那么bundle install它将起作用。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.