Rails 安装 mysql2 (mysql2-0.3.20) 时出现错误

20

我正在尝试在本地机器上启动一个Rails项目。当我运行bundle install命令时

Fetching mysql2 0.3.20
Installing mysql2 0.3.20 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-

0.3.20/ext/mysql2
/Users/mac/.rvm/rubies/ruby-2.3.1/bin/ruby -r ./siteconf20180707-33936-1toblx7.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()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Setting rpath to /usr/local/Cellar/mysql/8.0.11/lib
-----
creating Makefile

current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling client.c
client.c:367:33: warning: implicit conversion loses integer precision: 'long' to 'unsigned int' [-Wshorten-64-to-32]
        elapsed_time = end_time - start_time;
                     ~ ~~~~~~~~~^~~~~~~~~~~~
client.c:439:3: error: use of undeclared identifier 'my_bool'
  my_bool res = mysql_read_query_result(client);
  ^
client.c:441:19: error: use of undeclared identifier 'res'
  return (void *)(res == 0 ? Qtrue : Qfalse);
                  ^
client.c:775:3: error: use of undeclared identifier 'my_bool'
  my_bool boolval;
  ^
client.c:806:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:807:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:810: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:811:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:812:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:843:38: error: use of undeclared identifier 'boolval'
        wrapper->reconnect_enabled = boolval;
                                     ^
client.c:1165:56: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'unsigned int' [-Wshorten-64-to-32]
  mysql2rb = mysql2_mysql_enc_name_to_rb(charset_name, charset_name_len);
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~               ^~~~~~~~~~~~~~~~
client.c:1198: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,
  ^
2 warnings and 10 errors generated.
make: *** [client.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20 for inspection.
Results logged to /Users/mac/.rvm/gems/ruby-2.3.1/extensions/x86_64-darwin-17/2.3.0/mysql2-0.3.20/gem_make.out

An error occurred while installing mysql2 (0.3.20), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.20' --source 'https://rubygems.org/'` succeeds before bundling.

In Gemfile:
  mysql2

我随后按照指示{{执行操作}}

gem install mysql2 -v '0.3.20' --source 'https://rubygems.org/'
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
/Users/mac/.rvm/rubies/ruby-2.3.1/bin/ruby -r ./siteconf20180707-34132-p3fohi.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()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Setting rpath to /usr/local/Cellar/mysql/8.0.11/lib
-----
creating Makefile

current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling client.c
client.c:367:33: warning: implicit conversion loses integer precision: 'long' to 'unsigned int' [-Wshorten-64-to-32]
        elapsed_time = end_time - start_time;
                     ~ ~~~~~~~~~^~~~~~~~~~~~
client.c:439:3: error: use of undeclared identifier 'my_bool'
  my_bool res = mysql_read_query_result(client);
  ^
client.c:441:19: error: use of undeclared identifier 'res'
  return (void *)(res == 0 ? Qtrue : Qfalse);
                  ^
client.c:775:3: error: use of undeclared identifier 'my_bool'
  my_bool boolval;
  ^
client.c:806:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:807:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:810: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:811:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:812:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:843:38: error: use of undeclared identifier 'boolval'
        wrapper->reconnect_enabled = boolval;
                                     ^
client.c:1165:56: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'unsigned int' [-Wshorten-64-to-32]
  mysql2rb = mysql2_mysql_enc_name_to_rb(charset_name, charset_name_len);
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~               ^~~~~~~~~~~~~~~~
client.c:1198: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,
  ^
2 warnings and 10 errors generated.
make: *** [client.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20 for inspection.
Results logged to /Users/mac/.rvm/gems/ruby-2.3.1/extensions/x86_64-darwin-17/2.3.0/mysql2-0.3.20/gem_make.out

我尝试了这里提到的解决方案

Ruby gem mysql2安装失败

brew install mysql
Warning: mysql 8.0.11 is already installed and up-to-date
To reinstall 8.0.11, run `brew reinstall mysql`

并且{{然后}}。
gem install mysql2
Building native extensions.  This could take a while...
Successfully installed mysql2-0.5.2
Parsing documentation for mysql2-0.5.2
Done installing documentation for mysql2 after 0 seconds
1 gem installed

但是当我再次运行bundle install时,仍然会出现所有这些错误

奇怪的是,如果我运行

 brew reinstall mysql
==> Reinstalling mysql 
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.11.high_sierra.bottle.tar.gz
Already downloaded: /Users/mac/Library/Caches/Homebrew/mysql-8.0.11.high_sierra.bottle.tar.gz
==> Pouring mysql-8.0.11.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/8.0.11/bin/mysqld --initialize-insecure --user=mac --basedir=/usr/local/Cellar/mysql/8.0.11 --datadir=/usr/local/var/mysql --tmpdir=/tmp
Last 15 lines from /Users/mac/Library/Logs/Homebrew/mysql/post_install.01.mysqld:
2018-07-07 23:56:01 -0500

/usr/local/Cellar/mysql/8.0.11/bin/mysqld
--initialize-insecure
--user=mac
--basedir=/usr/local/Cellar/mysql/8.0.11
--datadir=/usr/local/var/mysql
--tmpdir=/tmp

2018-07-08T04:56:01.743929Z 0 [System] [MY-013169] [Server] /usr/local/Cellar/mysql/8.0.11/bin/mysqld (mysqld 8.0.11) initializing of server in progress as process 35410
2018-07-08T04:56:01.746039Z 0 [ERROR] [MY-010457] [Server] --initialize specified but the data directory has files in it. Aborting.
2018-07-08T04:56:01.746086Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-07-08T04:56:01.746293Z 0 [System] [MY-010910] [Server] /usr/local/Cellar/mysql/8.0.11/bin/mysqld: Shutdown complete (mysqld 8.0.11)  Homebrew.
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall mysql`
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
  /usr/local/Cellar/mysql/8.0.11: 254 files, 232.6MB

这很令人困惑,说实话我并不完全理解,可能是问题的根源,但我不确定。

我已经更新到最新版本的XCode并安装了开发工具

这是我的gemfile.lock文件的样子

GEM
  remote: https://rubygems.org/
  specs:
    actionmailer (4.2.0)
      actionpack (= 4.2.0)
      actionview (= 4.2.0)
      activejob (= 4.2.0)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 1.0, >= 1.0.5)
    actionpack (4.2.0)
      actionview (= 4.2.0)
      activesupport (= 4.2.0)
      rack (~> 1.6.0)
      rack-test (~> 0.6.2)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.1)
    actionview (4.2.0)
      activesupport (= 4.2.0)
      builder (~> 3.1)
      erubis (~> 2.7.0)
      rails-dom-testing (~> 1.0, >= 1.0.5)
      rails-html-sanitizer (~> 1.0, >= 1.0.1)
    activejob (4.2.0)
      activesupport (= 4.2.0)
      globalid (>= 0.3.0)
    activemodel (4.2.0)
      activesupport (= 4.2.0)
      builder (~> 3.1)
    activerecord (4.2.0)
      activemodel (= 4.2.0)
      activesupport (= 4.2.0)
      arel (~> 6.0)
    activesupport (4.2.0)
      i18n (~> 0.7)
      json (~> 1.7, >= 1.7.7)
      minitest (~> 5.1)
      thread_safe (~> 0.3, >= 0.3.4)
      tzinfo (~> 1.1)
    addressable (2.4.0)
    arel (6.0.3)
    authority (3.1.0)
      activesupport (>= 3.0.0)
      rake (>= 0.8.7)
    autoprefixer-rails (6.3.6.2)
      execjs
    aws_cf_signer (0.1.3)
    bcrypt (3.1.11)
    better_errors (2.1.1)
      coderay (>= 1.0.0)
      erubis (>= 2.6.6)
      rack (>= 0.9.0)
    binding_of_caller (0.7.2)
      debug_inspector (>= 0.0.1)
    bootstrap-sass (3.3.6)
      autoprefixer-rails (>= 5.2.1)
      sass (>= 3.3.4)
    builder (3.2.2)
    byebug (9.0.4)
    carrierwave (0.11.2)
      activemodel (>= 3.2.0)
      activesupport (>= 3.2.0)
      json (>= 1.7)
      mime-types (>= 1.16)
      mimemagic (>= 0.3.0)
    celluloid (0.16.0)
      timers (~> 4.0.0)
    chartkick (2.0.0)
    ckeditor (4.1.6)
      cocaine
      orm_adapter (~> 0.5.0)
    climate_control (0.0.3)
      activesupport (>= 3.0)
    cloudinary (1.1.7)
      aws_cf_signer
      rest-client
    cocaine (0.5.8)
      climate_control (>= 0.0.3, < 1.0)
    coderay (1.1.1)
    coffee-rails (4.1.1)
      coffee-script (>= 2.2.0)
      railties (>= 4.0.0, < 5.1.x)
    coffee-script (2.4.1)
      coffee-script-source
      execjs
    coffee-script-source (1.10.0)
    concurrent-ruby (1.0.2)
    connection_pool (2.2.0)
    debug_inspector (0.0.2)
    devise (4.1.1)
      bcrypt (~> 3.0)
      orm_adapter (~> 0.1)
      railties (>= 4.1.0, < 5.1)
      responders
      warden (~> 1.2.3)
    dotenv (2.1.1)
    dotenv-rails (2.1.1)
      dotenv (= 2.1.1)
      railties (>= 4.0, < 5.1)
    erubis (2.7.0)
    execjs (2.7.0)
    faraday (0.11.0)
      multipart-post (>= 1.2, < 3)
    font-awesome-rails (4.6.3.0)
      railties (>= 3.2, < 5.1)
    friendly_id (5.1.0)
      activerecord (>= 4.0.0)
    globalid (0.3.6)
      activesupport (>= 4.1.0)
    groupdate (3.0.0)
      activesupport (>= 3)
    hashie (3.5.5)
    hitimes (1.2.4)
    i18n (0.7.0)
    ice_cube (0.11.1)
    jbuilder (2.4.1)
      activesupport (>= 3.0.0, < 5.1)
      multi_json (~> 1.2)
    jquery-rails (4.1.1)
      rails-dom-testing (>= 1, < 3)
      railties (>= 4.2.0)
      thor (>= 0.14, < 2.0)
    jquery-ui-rails (4.2.1)
      railties (>= 3.2.16)
    json (1.8.3)
    jwt (1.5.6)
    kaminari (0.16.3)
      actionpack (>= 3.0.0)
      activesupport (>= 3.0.0)
    libv8 (3.16.14.15)
    loofah (2.0.3)
      nokogiri (>= 1.5.9)
    mail (2.6.4)
      mime-types (>= 1.16, < 4)
    meta-tags (2.1.0)
      actionpack (>= 3.0.0)
    mime-types (3.1)
      mime-types-data (~> 3.2015)
    mime-types-data (3.2016.0521)
    mimemagic (0.3.1)
    mini_magick (4.5.1)
    mini_portile2 (2.0.0)
    minitest (5.9.0)
    multi_json (1.12.1)
    multi_xml (0.6.0)
    multipart-post (2.0.0)
    mysql2 (0.3.20)
    newrelic_rpm (3.15.2.317)
    nokogiri (1.6.7.2)
      mini_portile2 (~> 2.0.0.rc2)
    oauth2 (1.3.1)
      faraday (>= 0.8, < 0.12)
      jwt (~> 1.0)
      multi_json (~> 1.3)
      multi_xml (~> 0.5)
      rack (>= 1.2, < 3)
    omniauth (1.6.1)
      hashie (>= 3.4.6, < 3.6.0)
      rack (>= 1.6.2, < 3)
    omniauth-facebook (4.0.0)
      omniauth-oauth2 (~> 1.2)
    omniauth-oauth2 (1.4.0)
      oauth2 (~> 1.0)
      omniauth (~> 1.2)
    orm_adapter (0.5.0)
    polyamorous (1.3.0)
      activerecord (>= 3.0)
    quiet_assets (1.1.0)
      railties (>= 3.1, < 5.0)
    rack (1.6.5)
    rack-canonical-host (0.2.2)
      addressable (> 0, < 3)
      rack (>= 1.0.0, < 3)
    rack-protection (1.5.3)
      rack
    rack-test (0.6.3)
      rack (>= 1.0)
    rails (4.2.0)
      actionmailer (= 4.2.0)
      actionpack (= 4.2.0)
      actionview (= 4.2.0)
      activejob (= 4.2.0)
      activemodel (= 4.2.0)
      activerecord (= 4.2.0)
      activesupport (= 4.2.0)
      bundler (>= 1.3.0, < 2.0)
      railties (= 4.2.0)
      sprockets-rails
    rails-deprecated_sanitizer (1.0.3)
      activesupport (>= 4.2.0.alpha)
    rails-dom-testing (1.0.7)
      activesupport (>= 4.2.0.beta, < 5.0)
      nokogiri (~> 1.6.0)
      rails-deprecated_sanitizer (>= 1.0.1)
    rails-html-sanitizer (1.0.3)
      loofah (~> 2.0)
    rails-i18n (4.0.8)
      i18n (~> 0.7)
      railties (~> 4.0)
    rails_12factor (0.0.3)
      rails_serve_static_assets
      rails_stdout_logging
    rails_serve_static_assets (0.0.5)
    rails_stdout_logging (0.0.5)
    railties (4.2.0)
      actionpack (= 4.2.0)
      activesupport (= 4.2.0)
      rake (>= 0.8.7)
      thor (>= 0.18.1, < 2.0)
    rake (11.1.2)
    ranked-model (0.4.0)
      activerecord (>= 3.1.12)
    ransack (1.7.0)
      actionpack (>= 3.0)
      activerecord (>= 3.0)
      activesupport (>= 3.0)
      i18n
      polyamorous (~> 1.2)
    rdoc (4.2.2)
      json (~> 1.4)
    redis (3.3.0)
    redis-namespace (1.5.2)
      redis (~> 3.0, >= 3.0.4)
    ref (2.0.0)
    responders (2.2.0)
      railties (>= 4.2.0, < 5.1)
    rest-client (1.6.7)
      mime-types (>= 1.16)
    sass (3.4.22)
    sass-rails (5.0.4)
      railties (>= 4.0.0, < 5.0)
      sass (~> 3.1)
      sprockets (>= 2.8, < 4.0)
      sprockets-rails (>= 2.0, < 4.0)
      tilt (>= 1.1, < 3)
    sdoc (0.4.1)
      json (~> 1.7, >= 1.7.7)
      rdoc (~> 4.0)
    sidekiq (3.4.2)
      celluloid (~> 0.16.0)
      connection_pool (~> 2.2, >= 2.2.0)
      json (~> 1.0)
      redis (~> 3.2, >= 3.2.1)
      redis-namespace (~> 1.5, >= 1.5.2)
    sidetiq (0.6.3)
      celluloid (>= 0.14.1)
      ice_cube (= 0.11.1)
      sidekiq (>= 3.0.0)
    sinatra (1.4.7)
      rack (~> 1.5)
      rack-protection (~> 1.4)
      tilt (>= 1.3, < 3)
    slim (3.0.7)
      temple (~> 0.7.6)
      tilt (>= 1.3.3, < 2.1)
    slim-rails (3.0.1)
      actionmailer (>= 3.1, < 5.0)
      actionpack (>= 3.1, < 5.0)
      activesupport (>= 3.1, < 5.0)
      railties (>= 3.1, < 5.0)
      slim (~> 3.0)
    spring (1.7.1)
    sprockets (3.6.0)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.0.4)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    temple (0.7.7)
    therubyracer (0.12.2)
      libv8 (~> 3.16.14.0)
      ref
    thor (0.19.1)
    thread_safe (0.3.5)
    tilt (2.0.4)
    timers (4.0.4)
      hitimes
    tzinfo (1.2.2)
      thread_safe (~> 0.1)
    uglifier (3.0.0)
      execjs (>= 0.3.0, < 3)
    warden (1.2.6)
      rack (>= 1.0)
    web-console (2.3.0)
      activemodel (>= 4.0)
      binding_of_caller (>= 0.7.2)
      railties (>= 4.0)
      sprockets-rails (>= 2.0, < 4.0)

PLATFORMS
  ruby

DEPENDENCIES
  authority (~> 3.0)
  bcrypt (~> 3.1.7)
  better_errors
  bootstrap-sass (~> 3.3.6)
  byebug
  carrierwave
  chartkick
  ckeditor
  cloudinary
  coffee-rails (~> 4.1.0)
  devise
  dotenv-rails
  font-awesome-rails
  friendly_id
  groupdate
  jbuilder (~> 2.0)
  jquery-rails
  jquery-ui-rails (~> 4.2.1)
  kaminari (~> 0.15)
  meta-tags
  mini_magick
  mysql2 (~> 0.3.20)
  newrelic_rpm
  omniauth-facebook
  quiet_assets
  rack-canonical-host
  rails (= 4.2.0)
  rails-i18n
  rails_12factor
  ranked-model
  ransack
  sass-rails (~> 5.0)
  sdoc (~> 0.4.0)
  sidekiq (~> 3.4.2)
  sidetiq (~> 0.6)
  sinatra (>= 1.3.0)
  slim-rails
  spring
  therubyracer
  uglifier (>= 1.3.0)
  web-console (~> 2.0)

RUBY VERSION
   ruby 2.3.1p112

BUNDLED WITH
   1.12.5

我还尝试了像这个答案建议的更具体的命令

https://dev59.com/L3A65IYBdhLWcg3w1SVC#32869742

但我仍然得到相同的错误。

gem install mysql2 -v '0.3.20' -- --with-mysql-config=/usr/local/Cellar/mysql/8.0.11/bin/mysql_config
Building native extensions with: '--with-mysql-config=/usr/local/Cellar/mysql/8.0.11/bin/mysql_config'
This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
/Users/mac/.rvm/rubies/ruby-2.3.1/bin/ruby -r ./siteconf20180708-38248-vd681p.rb extconf.rb --with-mysql-config=/usr/local/Cellar/mysql/8.0.11/bin/mysql_config
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/Cellar/mysql/8.0.11/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Setting rpath to /usr/local/Cellar/mysql/8.0.11/lib
-----
creating Makefile

current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20/ext/mysql2
make "DESTDIR="
compiling infile.c
compiling client.c
client.c:367:33: warning: implicit conversion loses integer precision: 'long' to 'unsigned int' [-Wshorten-64-to-32]
        elapsed_time = end_time - start_time;
                     ~ ~~~~~~~~~^~~~~~~~~~~~
client.c:439:3: error: use of undeclared identifier 'my_bool'
  my_bool res = mysql_read_query_result(client);
  ^
client.c:441:19: error: use of undeclared identifier 'res'
  return (void *)(res == 0 ? Qtrue : Qfalse);
                  ^
client.c:775:3: error: use of undeclared identifier 'my_bool'
  my_bool boolval;
  ^
client.c:806:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:807:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:810: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:811:7: error: use of undeclared identifier 'boolval'
      boolval = (value == Qfalse ? 0 : 1);
      ^
client.c:812:17: error: use of undeclared identifier 'boolval'
      retval = &boolval;
                ^
client.c:843:38: error: use of undeclared identifier 'boolval'
        wrapper->reconnect_enabled = boolval;
                                     ^
client.c:1165:56: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'unsigned int' [-Wshorten-64-to-32]
  mysql2rb = mysql2_mysql_enc_name_to_rb(charset_name, charset_name_len);
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~               ^~~~~~~~~~~~~~~~
client.c:1198: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,
  ^
2 warnings and 10 errors generated.
make: *** [client.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/mac/.rvm/gems/ruby-2.3.1/gems/mysql2-0.3.20 for inspection.
Results logged to /Users/mac/.rvm/gems/ruby-2.3.1/extensions/x86_64-darwin-17/2.3.0/mysql2-0.3.20/gem_make.out

这是我的mysql_config文件的日志

mysql_config
Usage: /usr/local/bin/mysql_config [OPTIONS]
Compiler: Clang 9.1.0.9020039
Options:
        --cflags         [-I/usr/local/Cellar/mysql/8.0.11/include/mysql ]
        --cxxflags       [-I/usr/local/Cellar/mysql/8.0.11/include/mysql ]
        --include        [-I/usr/local/Cellar/mysql/8.0.11/include/mysql]
        --libs           [-L/usr/local/Cellar/mysql/8.0.11/lib -lmysqlclient -lssl -lcrypto]
        --libs_r         [-L/usr/local/Cellar/mysql/8.0.11/lib -lmysqlclient -lssl -lcrypto]
        --plugindir      [/usr/local/Cellar/mysql/8.0.11/lib/plugin]
        --socket         [/tmp/mysql.sock]
        --port           [0]
        --version        [8.0.11]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/local/Cellar/mysql/8.0.11/include/mysql]
                pkglibdir     [/usr/local/Cellar/mysql/8.0.11/lib]
                plugindir     [/usr/local/Cellar/mysql/8.0.11/lib/plugin]

我正在使用Mac High Sierra 10.13.4操作系统。

我希望能得到关于它的任何建议,因为它让我感到非常疯狂。请告诉我我可以提供哪些其他信息以更好地提出问题。

提前感谢您的帮助。


或许是同样的问题:https://dev59.com/L3A65IYBdhLWcg3w1SVC - Emu
你好Emu,非常感谢你的帮助。我尝试了这篇文章中提出的所有解决方案,但它们都对我无效。你可以在我的帖子中看到我在尝试执行"brew install mysql"时收到的错误信息。如果你能从这些错误信息中发现任何提示,指出可能导致此问题的其他问题,我会非常感激。再次感谢你的帮助。 - James Weber
6个回答

48

在我的 Mac Pro 上安装 mysql2-0.3.21 时遇到了同样的问题。

以下解决方案适用于我(在终端指向您的项目文件夹下运行以下 3条命令):

brew install mysql

gem install mysql2 -v '0.3.21' -- --srcdir=/usr/local/mysql/include

bundle install

我不得不将其指向我的Homebrew安装的mysql,但除此之外,它对我很有效。 - Andy Stewart
1
在我的情况下,我使用的srcdir是/usr/local/Cellar/mysql@5.7/5.7.29/include(macOS Catalina 10.15.1)。 - Barney
祝福你。:joy: - jeffdill2
1
Ubuntu 20+,gem install mysql2 -v '0.3.21' -- --srcdir=/usr/include/mysql;使用mysql_config --include命令查找包含文件夹。 - Akash Martin

37

今天我遇到了这个问题。最近mysql进行了更新,对我有用的解决方法是:

brew install mysql@5.6
brew link mysql@5.6 --force
bundle install

另外,我没有尝试过这个解决方案,但似乎对其他人有效。

安装Mysql 2 gem失败


3
你好Samantha,非常感谢你的帮助。这使我成功运行了bundle install!!! 但是现在我又遇到了一个新错误 :(ERROR 2059 (HY000): 无法加载身份验证插件'caching_sha2_password':dlopen(/usr/local/Cellar/mysql@5.6/5.6.40/lib/plugin/caching_sha2_password.so, 2):找不到图像 - James Weber
5
由于系统上存在另一个版本,我不得不执行“brew link --overwrite --force mysql@5.6 ”命令,但这个方法奏效了! - CTS_AE

11

这对我有用 - macOS Catalina

brew uninstall mysql
gem uninstall mysql2

brew install mysql@5.7
brew link mysql@5.7 --force
gem install mysql2 -v '0.3.21'

# Gemfile
gem 'mysql2', '0.3.21'

bundle update mysql2

7

我的MacBook Pro在升级到MacOS Mojave(非常不美观!)期间遭遇了磁盘崩溃,因此我不得不在之后重新建立所有的代码项目。

其中一个特定的项目非常难以启动和运行,今天我花费了几个小时寻找一种方法来让mysql2-0.3.21在旧的RoR项目中运行;使用Ruby版本2.1.1p76和Rails 4.1.1。

最终,我通过遵循这个建议成功了:https://github.com/brianmario/mysql2/issues/1010#issuecomment-460257986

我执行了以下步骤:

  1. Start by removing all installed versions of mysql2 from your gemset:

    gem uninstall mysql2
    
  2. Then install MySQL 5.6, I already have MySQL 8.0.17 installed, so 5.6 is installed as a keg only which is fine:

    brew install mysql@5.6
    
  3. Then install mysql2 version 0.3.21 with the following command:

    gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/Cellar/mysql@5.6/5.6.42/bin/mysql_config --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
    

OpenSSL部分可能可以省略,并且您可以根据项目运行的Rails版本更改mysql2版本号。

完成上述步骤后,我能够再次启动Rails控制台并查询数据库:

Loading development environment (Rails 4.1.1)
2.1.1 :001 > User.count
   (18.7ms)  SELECT COUNT(*) FROM `users`  WHERE `users`.`deleted_at` IS NULL
 => 1222
2.1.1 :002 >

1
请注意,在--with-mysql-config选项中,您可能需要使用brew install mysql@5.6安装的版本替换5.6.42 mysql版本(我的版本是5.6.47)。 - Egor Iskrenkov

0

从今天开始,Homebrew 默认使用 MySQL 8.0、OpenSSL 1.1 和 mysql2 0.5,它们可以很好地一起使用。然而,为了将我的遗留应用程序从 Rails 3.2 升级到更近期的版本,我不得不在我的 MacOS BigSur 11.1 上设置本地环境。

我的目标是安装 mysql2 gem 版本 0.3.x(这是唯一一个允许通过版本 4.0 和 4.1 平稳迁移从 Rails 3.2 到 Rails 4.2 的版本),以及 mySQL 5.x。它们都依赖于 OpenSSL 1.0。

以下是对我有效的操作:

// Clean-up all your faulty attempts you did before
// Homebrew does not store openssl 1.0 anymore, so I had to find alternative  openssl1.0.2t 
wget https://github.com/tebelorg/Tump/releases/download/v1.0.0/openssl.rb
brew install openssl.rb

brew install mysql@5.7
brew link mysql@5.7 --force
brew install mysql-client@5.7
// NB! Pay close attention to path where you have mysql_config and openssl 1.0 installed
gem install mysql2 -v 0.3.21 -- --with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include

看这里:

Building native extensions with: '--with-mysql-config=/usr/local/bin/mysql_config --with-ldflags=-L/usr/local/Cellar/openssl/1.0.2t/lib --with-cppflags=-I/usr/local/Cellar/openssl/1.0.2t/include'
This could take a while...
Successfully installed mysql2-0.3.21
Parsing documentation for mysql2-0.3.21
Installing ri documentation for mysql2-0.3.21
Done installing documentation for mysql2 after 0 seconds
1 gem installed

0
在 Debian 上执行 gem install mysql2 命令时,我遇到了类似的错误。解决方法是安装 libmariadbclient-dev 包。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接