mysql2
mysql2 copied to clipboard
Rails app on Heroku + ClearDB- “cannot load such file — active_record/connection_adapters/mysql_adapter”
TL;DR
I'm seeing the following error when running heroku run rake db:setup
:
LoadError: cannot load such file -- active_record/connection_adapters/mysql_adapter
It appears that Heroku is attempting to use the wrong _adapter
file (i.e. mysql_adapter.rb
instead of mysql2_adapter.rb
), but this is only a guess and could be a red herring.
Background:
I created a "Hello World" Rails app as per the instructions here, with the exception that my Rails version is 5.2.4.4 and my database is MySQL:
~/Workspace/project_time/mysql/foobar (master) $ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
~/Workspace/project_time/mysql/foobar (master) $ rails -v
Rails 5.2.4.4
~/Workspace/project_time/mysql/foobar (master) $ rails new foobar -d mysql
The Ruby version in my Gemfile matches the Ruby version used by my system:
ruby '2.7.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.4', '>= 5.2.4.4'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
After a successful git push heroku master
, I subsequently ran heroku addons:create cleardb:ignite
and did not see any errors.
I also ran the provisioning instructions which I found on Heroku, including setting up the DATABASE_URL
env var. I confirmed that both this env var and the CLEARDB_
-prefixed version of same appear as follows:
mysql://<cleardb_username>:<cleardb_pwd>@us-cdbr-east-03.cleardb.com/<heroku_path>?reconnect=true
I added the following to my database.yml file:
production:
url: <%= ENV['DATABASE_URL'] %>
Also in database.yml
, I verified that the default adapter is set up as follows, and that my production settings don't perform an override:
default: &default
adapter: mysql2
Now, I'm trying to run heroku run rake db:setup
, however I'm seeing the following error:
rake aborted!
LoadError: 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.
/app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:34:in `require'
......
Caused by:
LoadError: cannot load such file -- active_record/connection_adapters/mysql_adapter
This Heroku link specifies that "If you’re using Ruby on Rails and the mysql2 gem, you will need to change the mysql:// scheme in the CLEARDB_DATABASE_URL to mysql2://". I did this but it had no apparent effect, and I continued to observe the same cannot load such file
error.
The rake db:setup
command works on my local machine. Coincidentally, I see mysql2_adapter.rb
among the list of files inside my ActiveRecord gem's lib/active_record/connection_adapters
directory. My best guess is that the reason the setup works locally but not on Heroku is that Heroku is looking for the wrong connection adapter file.
Any suggestions for how to proceed setting up my Heroku environment?
Update
FWIW, I also tried updating the regular DATABASE_URL
env var to mysql2://
, and that resulted in a new error:
rails aborted!
Mysql2::Error::ConnectionError: SSL connection error: error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol
This might imply that Heroku is continuing to use the old env var, not the new CLEARDB_DATABASE_URL
var. Not sure why this would be, given that I ran heroku addons:create cleardb:ignite
successfully. However, when I deleted DATABASE_URL
entirely (leaving only CLEARDB_DATABASE_URL
intact), and re-ran the setup command, I saw the following error:
rails aborted!
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
The dburl environment variable overrides the database.yml. Change mysql:// to mysql2://.
I must have been typing my update at the same time you were typing your reply. I updated DATABASE_URL
to mysql2://
, but that resulted in the following error:
Mysql2::Error::ConnectionError: SSL connection error: error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol
I also tried removing DATABASE_URL
entirely while leaving CLEARDB_DATABASE_URL
intact, but that resulted in the following:
Mysql2::Error::ConnectionError: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
FWIW, I also ensured that I updated the URL in the production section of database.yml
from DATABASE_URL
to CLEARDB_DATABASE_URL
, so that it points to the correct mysql2://
version of the URL. That had no effect.
I'm having the same issue. I went so far as to manually connect:
ActiveRecord::Base.establish_connection( :adapter => 'mysql2', :database => ENV.fetch("MYSQL_DATABASE"), :username => ENV.fetch("MYSQL_USERNAME"), :password => ENV.fetch("MYSQL_PASSWORD"), :host => ENV.fetch("MYSQL_HOST")).connection
Using that statement I was able to query the production database from my local console, but I can't connect in production. I get back:
Mysql2::Error::ConnectionError (SSL connection error: error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol)
@richiethomas I switched to JawsDB on heroku and it worked out. You may want to try the same.
@heflinao thanks! I'll check it out.
@heflinao I can confirm that switching to JawsDB did indeed work for me as well! I still have to read up on both ClearDB and JawsDB to understand what the trade-offs are, but I would have had to do that anyway lol.
For future devs who read this thread, just like with ClearDB, you'll still have to update your JAWSDB_URL
env var from the mysql://
prefix to the mysql2://
prefix. Also, make sure your database.yml
file includes url: <%= ENV['JAWSDB_URL'] %>
under the production
section.