使用Gem在Ruby on Rails上安装mysql2

时间:2014-11-07 10:40:36

标签: mysql ruby runtime-error

我尝试在带有gem的rails上的ruby上安装mysql2但是,我收到了这个错误:

我使用的是gem 2.4.2和ruby 2.1.4。有人知道如何解决它吗?

 gem.bat : ERROR:  Error installing mysql2:
Em linha:1 caractere:4
+ gem <<<<  install mysql2
+ CategoryInfo          : NotSpecified: (ERROR:  Error installing                            mysql2::String)         [],    RemoteException
+ FullyQualifiedErrorId : NativeCommandError

ERROR: Failed to build gem native extension.

C:/Ruby21/bin/ruby.exe -r ./siteconf20141107-7440-10fbirr.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... *** 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.

1 个答案:

答案 0 :(得分:0)

mysql2 gem's GitHub page暗示了这一点:

  

     

首先,确保安装了DevKit   (http://rubyinstaller.org/downloads/)及其变量由   运行devkit \ devktvars.bat。

     

接下来,您需要一个MySQL库来链接。如果你有MySQL   加载到您的开发机器上,您可以使用它。如果没有,你会的   需要从服务器复制MySQL目录,否则   获取MySQL C连接器的副本:   http://dev.mysql.com/downloads/connector/c/

     

如果您正在使用连接器,我建议您只获取.zip文件   并在方便的地方解压缩。

     

现在你可以安装mysql2了。您必须使用--with-mysql-dir选项   告诉gem你的MySQL库文件在哪里。例如,如果你   将连接器解压缩到c:\mysql-connector-c-6.1.1-win32   像这样安装宝石:

gem install mysql2 -- --with-mysql-dir=c:\mysql-connector-c-6.1.1-win32
     

最后,您必须从MySQL或MySQL的lib子目录中复制libmysql.dll   连接器目录到ruby \ bin目录中。在上面   例如,libmysql.dll将位于   c:\ mysql-connector-c-6.1.1-win32 \ lib。

但是,我会切换到基于Linux的虚拟机。这有几个原因:

  • 许多宝石只是不能在Windows上运行或需要相当复杂的设置(如上所述)
  • 您可能从不将您的应用部署到生产中的Windows,匹配环境有助于更轻松地追踪可能的问题
  • 您可以使用RVM并在发布时编译最新的Rubies