Ruby on rails 安装Mysql 2 gem失败

Ruby on rails 安装Mysql 2 gem失败,ruby-on-rails,rubygems,mysql2,Ruby On Rails,Rubygems,Mysql2,我正在尝试安装mysql2(版本0.4.5),我正在使用Rails 5.0.2、Ruby 2.3.1 我已经使用homebrew(版本8.0.11)安装了mysql服务器,并以以下方式启动了mysql: brew install mysql brew services start mysql Gemfile.rb gem 'mysql2' 当我尝试安装gem mysql2时,我得到了以下错误 Gem::Ext::BuildError: ERROR: Failed to build gem n

我正在尝试安装mysql2(版本0.4.5),我正在使用Rails 5.0.2、Ruby 2.3.1

我已经使用homebrew(版本8.0.11)安装了mysql服务器,并以以下方式启动了mysql:

brew install mysql
brew services start mysql
Gemfile.rb

gem 'mysql2'
当我尝试安装gem mysql2时,我得到了以下错误

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/gomathi/.rvm/gems/ruby-2.3.1@connector/gems/mysql2-0.4.5/ext/mysql2

creating Makefile

current directory: /Users/gomathi/.rvm/gems/ruby-2.3.1@connector/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/gomathi/.rvm/gems/ruby-2.3.1@connector/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR="
compiling client.c
In file included from client.c:1:
In file included from ./mysql2_ext.h:41:
./result.h:24:3: error: unknown type name 'my_bool'
  my_bool *is_null;
  ^
./result.h:25:3: error: unknown type name 'my_bool'
  my_bool *error;
  ^
client.c:507:3: error: use of undeclared identifier 'my_bool'
  my_bool res = mysql_read_query_result(client);
  ^
client.c:509:19: error: use of undeclared identifier 'res'
  return (void *)(res == 0 ? Qtrue : Qfalse);
                  ^
client.c:828:3: error: use of undeclared identifier 'my_bool'
  my_bool boolval;
  ^
client.c:859:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:860:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:863:10: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
    case MYSQL_SECURE_AUTH:
         ^~~~~~~~~~~~~~~~~
         MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.11/include/mysql/mysql.h:188:3: note: 'MYSQL_DEFAULT_AUTH' declared here
  MYSQL_DEFAULT_AUTH,
  ^
client.c:864:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:865:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:896:38: error: use of undeclared identifier 'boolval'
        wrapper->reconnect_enabled = boolval;
                                     ^
client.c:1285:38: error: use of undeclared identifier 'MYSQL_SECURE_AUTH'; did you mean 'MYSQL_DEFAULT_AUTH'?
  return _mysql_client_options(self, MYSQL_SECURE_AUTH, value);
                                     ^~~~~~~~~~~~~~~~~
                                     MYSQL_DEFAULT_AUTH
/usr/local/Cellar/mysql/8.0.11/include/mysql/mysql.h:188:3: note: 'MYSQL_DEFAULT_AUTH' declared here
  MYSQL_DEFAULT_AUTH,
  ^
12 errors generated.
make: *** [client.o] Error 1

make failed, exit code 2

我怎样才能完成这项工作

尝试取消链接并重新安装mysql:

brew unlink mysql
brew cleanup
brew install mysql
gem install mysql2

您正在安装较旧版本的gem(0.4.5)。我也有同样的问题(使用0.4.3)。安装版本0.4.10为我解决了这个问题

试试这个:

gem install mysql2 -v 0.4.10
如果安装干净,您必须更新您的
Gemfile
以要求此版本:

gem 'mysql2', '~> 0.4.10'

不要安装较新版本的gem(即0.5.x),它们不能与Rails 4或更旧版本的Rails 5(5.0.7/5.1.6之前)一起使用(请参阅)。

另一个对我有用的替代方法是安装MariaDB,也就是10.0.x版本

$ brew install mariadb@10.0
$ brew link mariadb@10.0 --force
要自动启动MariaDB服务器,请使用与macOS launchctl集成的自制服务功能:

brew services start mariadb@10.0
现在,mysql2的安装将非常有效

$ gem install mysql2 -v '0.4.6'
Building native extensions. This could take a while...
Successfully installed mysql2-0.4.6

我通过从dmg包中安装MySQL使其工作

根据MySQL 5.7文档,5.7中默认启用了
MySQL\u SECURE\u AUTH
,因为mysql2没有编译到低于版本
0.4.10
的MySQL=>5.7


我认为您可以删除Gemfile.lock中的所有内容。再次尝试
捆绑安装后,您的项目将正常运行。

我今天也遇到了同样的问题(MacOS 10.13)。这以前是有用的。不确定发生了什么变化。事实上,我看到您使用的是较旧版本的gem(0.4.5–我使用的是0.4.3)。你试过更新的版本吗?我能够成功地安装当前版本(0.5.1)(使用
gem install mysql2
),但0.4.3版在我的系统上失败。事实上,我刚刚了解到,0.5.x发行版不能与任何Rails 4应用程序和某些Rails 5应用程序一起使用(取决于发布点)。在0.4.x发行版中尝试更新版本的gem,例如0.4.10。谢谢你的回答。是的,它在0.4.5版时失败。但安装在最新版本中。@Gomathi如果解决了您的问题,请将此答案标记为正确。天哪!是2k19。。。谢谢,你节省了我的时间!看起来有点像核武器?(不是我的-1,只是评论)