Rails error installing mysql2 (mysql2-0.3.20)

2020-05-23 08:52发布

I am trying to get a rails project up and running on my local machine. When I do 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

I then follow the instructions and

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

I have tried the solution mentioned here

Ruby gem mysql2 install failing

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

and then

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

but when I do bundle install again I still get all of these errors

strangely if I run

 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
                

4条回答
该账号已被封号
2楼-- · 2020-05-23 09:30

My MacBook Pro had a disk crashed during upgrade to MacOS Mojave (not pretty!), so I had to reestablish all my code projects afterwards.

One particular project proved extremely hard to get up and running and I've spent several hours today looking for a way to get mysql2-0.3.21 running for an older RoR project; Ruby version 2.1.1p76 with Rails 4.1.1.

Finally, I succeeded by following this advice: https://github.com/brianmario/mysql2/issues/1010#issuecomment-460257986

I did the following steps:

  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
    

The OpenSSL part can probably be left out and you can change the mysql2 version number depending on what version of Rails your project is running.

After the above steps I was able to start the Rails console again and query the database:

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 >
查看更多
SAY GOODBYE
3楼-- · 2020-05-23 09:42

I just ran into this problem today. mysql got updated recently, so what worked for me was:

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

Alternatively, I did not try this solution, but it seems to have worked for other people.

Installing Mysql 2 gem fails

查看更多
混吃等死
4楼-- · 2020-05-23 09:45

I faced the same issue when installing mysql2-0.3.21 on Mac pro.

Below mentioned solution worked for me (run the below mentioned 3 commands in the terminal pointing to your project folder):

brew install mysql

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

bundle install
查看更多
该账号已被封号
5楼-- · 2020-05-23 09:53

This worked for me - 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
查看更多
登录 后发表回答