brianmario / mysql2

A modern, simple and very fast Mysql library for Ruby - binding to libmysql
http://github.com/brianmario/mysql2
MIT License
2.24k stars 550 forks source link

`require': cannot load such file -- mysql2/mysql2 (LoadError) #1354

Closed sreenak closed 7 months ago

sreenak commented 7 months ago

I am trying to install gem mysql2 in Mac M3 Pro, Ventura, ruby(3.2.2) latest version rails(7.1.2) latest and mysql latest version(8.3.0). I am getting this error when I tryrails s. I searched almost all solutions, but I couldn't find a way to resolve this. Please help

First I got the below error when I trybundle install, this got resolved by executing thearch -x86_64 gem install mysql2 -v 0.5.5 -- --srcdir=/usr/local/mysql/include command, but when I run rails s its not still working

me@me-MacBook-Pro Project % bundle install
Fetching gem metadata from https://rubygems.org/........
Installing mysql2 0.5.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/.rvm/gems/ruby-3.2.2/gems/mysql2-0.5.5/ext/mysql2
/Users/.rvm/rubies/ruby-3.2.2/bin/ruby extconf.rb --with-ldflags\=-L/usr/local/opt/openssl/lib --with-cppflags\=-I/usr/local/opt/openssl/include
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_gc_mark_movable()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enc_interned_str() in ruby.h... yes
-----
Using --with-openssl-dir=/usr/local/opt/openssl@1.1
-----
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_DEFAULT_AUTH in mysql.h... yes
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... no
-----
Setting rpath to /usr/local/Cellar/mysql/8.3.0/lib
-----
creating Makefile

current directory: /Users/.rvm/gems/ruby-3.2.2/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20240208-27892-82jjme sitelibdir\=./.gem.20240208-27892-82jjme clean

current directory: /Users/.rvm/gems/ruby-3.2.2/gems/mysql2-0.5.5/ext/mysql2
make DESTDIR\= sitearchdir\=./.gem.20240208-27892-82jjme sitelibdir\=./.gem.20240208-27892-82jjme
compiling client.c
In file included from client.c:15:
./mysql_enc_name_to_ruby.h:43:1: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
mysql2_mysql_enc_name_to_rb_hash (str, len)
^
./mysql_enc_name_to_ruby.h:86:1: warning: a function definition without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
mysql2_mysql_enc_name_to_rb (str, len)
^
client.c:1438:3: error: call to undeclared function 'mysql_ssl_set'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
  mysql_ssl_set(wrapper->client,
  ^
client.c:1438:3: note: did you mean 'mysql_close'?
/usr/local/Cellar/mysql/8.3.0/include/mysql/mysql.h:797:14: note: 'mysql_close' declared here
void STDCALL mysql_close(MYSQL *sock);
             ^
2 warnings and 1 error generated.
make: *** [client.o] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/.rvm/gems/ruby-3.2.2/gems/mysql2-0.5.5 for inspection.
Results logged to /Users/.rvm/gems/ruby-3.2.2/extensions/x86_64-darwin-22/3.2.0/mysql2-0.5.5/gem_make.out

  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:119:in `run'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:51:in `block in make'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:43:in `each'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:43:in `make'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/ext_conf_builder.rb:41:in `build'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:187:in `build_extension'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:221:in `block in build_extensions'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:218:in `each'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/ext/builder.rb:218:in `build_extensions'
  /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/installer.rb:843:in `build_extensions'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/rubygems_gem_installer.rb:76:in `build_extensions'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/rubygems_gem_installer.rb:28:in `install'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/source/rubygems.rb:203:in `install'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/installer/gem_installer.rb:54:in `install'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/installer/gem_installer.rb:16:in `install_from_spec'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/installer/parallel_installer.rb:130:in `do_install'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/installer/parallel_installer.rb:121:in `block in worker_pool'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/worker.rb:62:in `apply_func'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/worker.rb:57:in `block in process_queue'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/worker.rb:54:in `loop'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/worker.rb:54:in `process_queue'
  /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/worker.rb:90:in `block (2 levels) in create_threads'

An error occurred while installing mysql2 (0.5.5), and Bundler cannot continue.

In Gemfile:
  mysql2

And someway I got it resolved after doing this command

me@me-MacBook-Pro Project % arch -x86_64 gem install mysql2 -v 0.5.5 -- --srcdir=/usr/local/mysql/include 
Building native extensions with: '--srcdir=/usr/local/mysql/include'
This could take a while...
Successfully installed mysql2-0.5.5
Parsing documentation for mysql2-0.5.5
Installing ri documentation for mysql2-0.5.5
Done installing documentation for mysql2 after 0 seconds
1 gem installed

but after running rails s Igot this error, and I didn't get any solution working to resolve this.

me@me-MacBook-Pro Project % rails s                                                                       
<internal:/Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require': cannot load such file -- mysql2/mysql2 (LoadError)
    from <internal:/Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in `require'
    from /Users/.rvm/gems/ruby-3.2.2/gems/mysql2-0.5.5/lib/mysql2.rb:36:in `<top (required)>'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in `require'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in `require'
    from /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
    from /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/runtime.rb:55:in `each'
    from /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/runtime.rb:55:in `block in require'
    from /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/runtime.rb:44:in `each'
    from /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler/runtime.rb:44:in `require'
    from /Users/.rvm/gems/ruby-3.2.2/gems/bundler-2.4.22/lib/bundler.rb:187:in `require'
    from /Users/Documents/Project/config/application.rb:7:in `<top (required)>'
    from <internal:/Users/sreena/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/Users/sreena/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /Users/sreena/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:139:in `block in perform'
    from <internal:kernel>:90:in `tap'
    from /Users/sreena/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/commands/server/server_command.rb:136:in `perform'
    from /Users/sreena/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
    from /Users/sreena/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /Users/sreena/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/command/base.rb:178:in `invoke_command'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/command/base.rb:73:in `perform'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/command.rb:71:in `block in invoke'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/command.rb:149:in `with_argv'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/command.rb:69:in `invoke'
    from /Users/.rvm/rubies/ruby-3.2.2/lib/ruby/gems/3.2.0/gems/railties-7.1.3/lib/rails/commands.rb:18:in `<top (required)>'
    from <internal:/Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /Users/sreena/Documents/techaffinity/InspectionsProject/bin/rails:9:in `<top (required)>'
    from /Users/.rvm/gems/ruby-3.2.2/gems/spring-4.1.3/lib/spring/client/rails.rb:28:in `load'
    from /Users/.rvm/gems/ruby-3.2.2/gems/spring-4.1.3/lib/spring/client/rails.rb:28:in `call'
    from /Users/.rvm/gems/ruby-3.2.2/gems/spring-4.1.3/lib/spring/client/command.rb:7:in `call'
    from /Users/.rvm/gems/ruby-3.2.2/gems/spring-4.1.3/lib/spring/client.rb:30:in `run'
    from /Users/.rvm/gems/ruby-3.2.2/gems/spring-4.1.3/bin/spring:49:in `<top (required)>'
    from /Users/.rvm/gems/ruby-3.2.2/gems/spring-4.1.3/lib/spring/binstub.rb:11:in `load'
    from /Users/.rvm/gems/ruby-3.2.2/gems/spring-4.1.3/lib/spring/binstub.rb:11:in `<top (required)>'
    from <internal:/Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/Users/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /Users/Documents/techaffinity/InspectionsProject/bin/spring:15:in `<top (required)>'
    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'
januszm commented 7 months ago

oh no, brew upgrade 💥 , we need to add support for MySQL 8.3

sreenak commented 7 months ago

januszm Should I proceed with downgrading MySQL to an older version? Could you please specify the recommended version for compatibility?

januszm commented 7 months ago

januszm Should I proceed with downgrading MySQL to an older version? Could you please specify the recommended version for compatibility?

@sreenak Stick to 8.0 or use the mysql2 gem straight from Github (support for 8.2 is not released yet).

I need to do the same thing, Rails does not work with 8.3, at least not on M1 platform on Macs, getting other errors too.

sreenak commented 7 months ago

@januszm Great Thanks for your reply, Let me try installing with 8.0 version

sreenak commented 7 months ago

@januszm Very very thanks and cheers it worked for me, using mysql version 8.0. Also followed the below commands after that, if anyone stuck with same issue, hope it will be helpful for them

gem install mysql2 -- --with-openssl-dir=$(brew --prefix openssl@1.1)
bundle config --local build.mysql2 -- --with-openssl-dir=$(brew --prefix openssl@1.1)
bundle config --local build.mysql2 -- $(ruby -r rbconfig -e 'puts RbConfig::CONFIG["configure_args"]' | xargs -n1 | grep with-openssl-dir)