Search code examples
mysqlrubymysql2

`mysql2` gem- how to run benchmark files?


OSx v12.4
Ruby v2.6.0
XCode v13.4.1
`mysql2` gem, SHA e9c662912dc3bd3707e6c7f0c75e591294cffe12

I cloned the mysql2 repo and I'm trying to run ruby benchmark/active_record.rb from the root directory. I'm getting the following error:

/Users/richiethomas/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/activesupport-
6.1.6/lib/active_support/dependencies.rb:332:in `require': Could not load the 'mysql' 
Active Record adapter. Ensure that the adapter is spelled correctly in 
config/database.yml and that you've added the necessary adapter gem to your Gemfile. 
(LoadError)

I see the following in the repo's Gemfile:

group :benchmarks, optional: true do
  gem 'activerecord', '>= 3.0'
  gem 'benchmark-ips'
  gem 'do_mysql'
  gem 'faker'
  # The installation of the mysql latest version 2.9.1 fails on Ruby >= 2.4.
  gem 'mysql' if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.4')
  gem 'sequel'
end

I tried running bundle install --with=benchmarks, and that completes successfully, however it doesn't install the mysql ActiveRecord adapter because my Ruby version is 2.6.0.

Although I know this shouldn't be necessary, I tried installing the mysql gem via the following command, as per the gem's README.md file:

gem install mysql2 -- --with-mysql-dir=-/usr/local/opt/[email protected]

However, here I get:

library not found for -lssl
clang: error: linker command failed with exit code 1

I also tried installing mysql using the following command, as per this StackOverflow answer:

gem install mysql -- --with-ldflags=-L/usr/local/opt/openssl/lib

However, I get an error related to rb_cFixnum, which I surmise is because the Fixnum class doesn't exist in my Ruby version (it appears to have been eliminated in 2.4.0, and I'm on 2.6.0). Between these two errors, I feel like I can eliminate the possibility that the error is caused by a lack of the mysql gem.

On the chance that the root issue is an out-of-date linker file, I have updated both my OS and XCode to the latest versions available as of today's date. The above errors occurred after I completed this update process.

My question is two-fold:

  1. I notice that the error message references config/database.yml, which I'm accustomed to seeing in my Rails projects but which I don't see anywhere in the mysql2 repo. Does this mean I can only run the benchmarks if there's an associated database.yml file? And if so, how would I run these benchmarks from (for example) a "Hello World"-type Rails project?

  2. If I'm unable to install the mysql gem in conjunction with Ruby versions greater than 2.4.0, why does the benchmarks/active_record.rb file appear to rely on its presence in this line of code? I would expect to be able to run benchmark files regardless of my Ruby version.


Solution

  • I think you should not assume the benchmark tests can be run without some manual editing and corrections of the benchmark code. For example the file setup_db.rb contains hardcoded credentials which you will obviously need to edit.

    Note also that you will have to run the setup file first, before you run anything else. For example the active_record.rb file seems to be using data from the mysql2_test table created with setup_db.rb.

    The reference you get to database.yml is just a standard error message from the Rails codebase. It does not mean you need this file for the test. In fact you will most likely need to edit the opts line in the active_record.rb file instead, to point to a DB that can connect to something that is compatible with what you initialize in setup_rb.rb.

    Also I would skip the mysql gem and just try to run it with mysql2, if you have it installed already. For this you have to remove any reference to mysql from the test code.

    If you look at the mysql gem on Rubygems, the latest version is from 2013. It is highly unlikely you will get it to run on any modern Ruby version. This also gives a hint of how outdated the benchmark code most likely is.

    Further hints of out-of-date can be seen by looking at the git history of the files in the benchmark folder. They have not been updated since 2017.

    In summary:

    You will have to edit the benchmark code to make it work. The code does not look like it is set up in a generalized fire and forget manner, and is most likely outdated.

    Do not install mysql, but instead mysql2. For questions related to mysql2 installation issues, it's best to post a new question.