安装 Mysql 2 gem 失败

Installing Mysql 2 gem fails

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

我已经使用自制软件(版本 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

我怎样才能完成工作?。

您正在安装旧版本的 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 之前)(参见 https://github.com/brianmario/mysql2/issues/950)。

另一种对我有用的方法是安装 MariaDB,特别是版本 10。0.x

$ brew install mariadb@10.0
$ brew link mariadb@10.0 --force

要自动启动 MariaDB 服务器,请使用 Homebrew 的服务功能,它与 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 使其工作 https://dev.mysql.com/downloads/mysql/5.7.html#downloads

根据 MySQL 5.7 文档 MYSQL_SECURE_AUTH 在 5.7 中默认启用,因此 mysql2 无法在版本 0.4.10 以下编译到 MySQL => 5.7

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