Installing Mysql 2 gem fails

2019-02-21 13:40发布

问题:

I am trying to install mysql2 (version 0.4.5) and I am using Rails 5.0.2, Ruby 2.3.1

I've already installed mysql server using homebrew (version 8.0.11)and started mysql as:

brew install mysql
brew services start mysql

Gemfile.rb

gem 'mysql2'

When I try installing gem mysql2 I am getting the following error

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

How can I get the job done?.

回答1:

You're installing an older version of the gem (0.4.5). I had the same issue (using 0.4.3). Installing version 0.4.10 solved this for me.

Try this:

gem install mysql2 -v 0.4.10

If that installs cleanly, you'll have to update your Gemfile to require this version:

gem 'mysql2', '~> 0.4.10'

Don't install a newer version of the gem (i.e. 0.5.x), they won't work with Rails 4 or older releases of Rails 5 (before 5.0.7/5.1.6) (see https://github.com/brianmario/mysql2/issues/950).



回答2:

Another alternative that worked for me here was to install MariaDB, that too specifically version 10.0.x

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

To auto-start MariaDB Server, use Homebrew's services functionality, which integrates with macOS launchctl:

brew services start mariadb@10.0

Now, the installation of mysql2 will work like a charm.

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


回答3:

I made it work by installing MySQL from dmg package https://dev.mysql.com/downloads/mysql/5.7.html#downloads



回答4:

According to MySQL 5.7 document MYSQL_SECURE_AUTH has been enabled by default in 5.7, because of that the mysql2 not compile below the version 0.4.10 to the MySQL => 5.7

  • https://github.com/brianmario/mysql2/releases/tag/0.4.10
  • https://github.com/brianmario/mysql2/issues/891
  • https://dev.mysql.com/doc/refman/5.6/en/mysql-options.html


回答5:

I think you can remove everything in Gemfile.lock. After try bundle install again and your Project will be running ok.



回答6:

Try to unlink and reinstall mysql:

brew unlink mysql
brew cleanup
brew install mysql
gem install mysql2