Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hii i got error cannot load such file #1047

Closed
zokypesch opened this issue May 18, 2019 · 12 comments
Closed

hii i got error cannot load such file #1047

zokypesch opened this issue May 18, 2019 · 12 comments

Comments

@zokypesch
Copy link

my spec :
os: Mac os X mojave
mysql Ver 8.0.16 for osx10.14 on x86_64 Homebrew)
rvm 1.29.8 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
ruby version 2.6.1
rails version 5.2.3

i got error when try to run rake db:create  01:36:08
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.
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in block in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:89:in register' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:40:in rescue in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:26:in require'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in block in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in load_dependency'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:191:in spec'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:956:in establish_connection' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:60:in establish_connection'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/railtie.rb:136:in block (2 levels) in <class:Railtie>' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:71:in instance_eval'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:71:in block in execute_hook' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:62:in with_execution_control'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:67:in execute_hook' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:52:in block in run_load_hooks'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:51:in each' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:51:in run_load_hooks'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/base.rb:328:in <module:ActiveRecord>' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/base.rb:27:in

'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in block in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in register' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in block in require'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in load_dependency' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in require'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:17:in `block (2 levels) in '

Caused by:
LoadError: cannot load such file -- active_record/connection_adapters/mysql_adapter
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in block in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:89:in register' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:40:in rescue in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:26:in require'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in block in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in load_dependency'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/connection_specification.rb:191:in spec'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:956:in establish_connection' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/connection_handling.rb:60:in establish_connection'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/railtie.rb:136:in block (2 levels) in <class:Railtie>' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:71:in instance_eval'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:71:in block in execute_hook' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:62:in with_execution_control'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:67:in execute_hook' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:52:in block in run_load_hooks'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:51:in each' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/lazy_load_hooks.rb:51:in run_load_hooks'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/base.rb:328:in <module:ActiveRecord>' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/base.rb:27:in

'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in block in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in register' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in require_with_bootsnap_lfi'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in require' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in block in require'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:257:in load_dependency' /Users/maulana/.rvm/gems/ruby-2.6.1/gems/activesupport-5.2.3/lib/active_support/dependencies.rb:291:in require'
/Users/maulana/.rvm/gems/ruby-2.6.1/gems/activerecord-5.2.3/lib/active_record/railties/databases.rake:17:in `block (2 levels) in '

Caused by:
Bootsnap::LoadPathCache::FallbackScan:

any body can help ???

@zokypesch
Copy link
Author

config/database.yml

default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: root
password: root
host: localhost

@sodabrew
Copy link
Collaborator

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.

This suggests your configuration is for adapter: mysql and not mysql2. Please triple check your database config is being used. Try setting different adapter values and make sure it fails with your dummy values. If it always fails with the same error no matter what you put in your config file, then go find out what config file is actually in use, since that would indicate not the one you're editing.

@zokypesch
Copy link
Author

zokypesch commented May 21, 2019

Yes i tired change configuration, the result is same so i think is not about a config, cause with the another adapter like sqllite is running well. I try too with different language using mysql and works fine... what i made misatakes?

By the way, two months ago everything works fine.. in my case its “rails new my_project -d mysql && cd my_project && rake db:create” and then no errors found while gem installed

@sodabrew
Copy link
Collaborator

Yes i tired change configuration, the result is same so i think is not about a config, cause with the another adapter like sqllite is running well.

But this is exactly what I suggested to try. If different adapter name produce the error "cannot find mysql" then you are editing a different config file than your Rails instance is using.

@sodabrew
Copy link
Collaborator

Have you got this working yet? I want to close out this ticket because it does not represent a bug in the mysql2 gem, but I want to confirm that you got the help you need.

@sci-phi
Copy link

sci-phi commented Jun 5, 2019

I'm actually seeing the same problem : The app refers to 'mysql' adapter when I have been verified there is no configuration other than for a 'mysql2' adapter.

/vendor/bundle/ruby/2.5.0/gems/bootsnap-1.4.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22: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 am fairly certain this is some bad mojo in Rails (5.2.3, MRI 2.5.1, macOS X) but just in case it is a mysql2 adapter issue, I thought I would chime in.

@sci-phi
Copy link

sci-phi commented Jun 5, 2019

OK - in my case, it looks like Rails is resolving the config to pull from ENV["DATABASE_URL"] and then preferring to infer 'mysql' from that versus the 'mysql2' (or 'sqlite3' ...) configured in the database.yml

line 78 ActiveRecord::ConnectionHandling::MergeAndResolveDefaultUrlConfig#config:

76: def config
77:   @raw_config.dup.tap do |cfg|
78:     if url = ENV["DATABASE_URL"]
79:       cfg[@env] ||= {}
80:       cfg[@env]["url"] ||= url
81:     end
82:   end
83: end

and also line 163 ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver:

152: def resolve_all
153:   config = configurations.dup
154: 
155:   if env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
156:     env_config = config[env] if config[env].is_a?(Hash) && !(config[env].key?("adapter") || config[env].key?("url"))
157:   end
158: 
159:   config.reject! { |k, v| v.is_a?(Hash) && !(v.key?("adapter") || v.key?("url")) }
160:   config.merge! env_config if env_config
161: 
162:   config.each do |key, value|
163:     config[key] = resolve(value) if value
164:   end
165: 

I would expect it to merge these two (as it seems to do for some non-adapter values) as I will be deploying using an Ops established ENV-based secrets mechanism to get that URL, so... it is definitely ActiveRecord 'resolving' the config wrong, and ignoring the mysql2 adapter setting ✅

@sodabrew
Copy link
Collaborator

sodabrew commented Jun 5, 2019

Great find!! Do you think this is a Rails big, or at least documentation issue?

@sci-phi
Copy link

sci-phi commented Jun 6, 2019

The docs for ActiveRecord::Base#establish_connection 🔗 state that you can pass a hash "Or a URL"

It does not say you can merge the two techniques. ☹️ I guess that makes it is a surprisingly limited feature, rather than an bug.

Furthermore, ActiveRecord encodes assumptions about the ENV["DATABASE_URL"] being definitive. It will actually populate the base configuration when ActiveRecord is first loaded, slurping the URL from the ENV variable before it even reads the YAML file.

When AR resolves the URL, it basically overwrites any conflicting info you had specified in the YAML

ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver#resolve_hash_connection

   270: def resolve_hash_connection(spec)
   271:   if spec["url"] && spec["url"] !~ /^jdbc:/
   272:     connection_hash = resolve_url_connection(spec.delete("url"))
=> 273:     spec.merge!(connection_hash)
   274:   end
   275:   spec
   276: end

Any of the YAML settings OTHER THAN these uri-parsed fields will be merged OK.

  "adapter"
  "username"
  "password"
  "port"
  "database"
  "host"

So, that's the basics of why it does not work as I expected.

The GOOD NEWS is that there is a simple hack to make DB URLs work with MySQL2:

  • Specify "mysql2" as the schema in your URL. That's it.
 "mysql2://sql_user:sql_pass@sql_host_name/sql_db_name"

Since ActiveRecord infers the URI scheme from the parsed URL as the adapter name,
you end up with a connection spec like so:

    => {
     "adapter"=>"mysql2",
     "username"=>"sql_user",
     "password"=>"sql_pass",
     "database"=>"sql_db_name",
     "host"=>"sql_host_name"
     }

If you CAN NOT change it (and I was laboring under that impression...) then :

  1. Avoid the Black Magic of using ENV["DATABASE_URL"], rename your variable!

    On my local machine, using dotenv, I renamed the env var ["SQL_SERVICE_URL"]
    Later, I also remapped the deployed variable name via Kubernetes app config.

    If present, the ENV variable is used for base connection BEFORE you modify it

  2. In the YAML config, take advantage of ERB pre-processing to MODIFY the URL,
    and replace the 'mysql://' scheme with 'mysl2://'

    Assuming an ENV var of (from Ops or dotenv):

     "mysql://sql_user:sql_pass@sql_host_name/sql_db_name"

Add this regex to the URL injection from ENV:

   deployed_env:
      <<: *mysql_defaults
      url: <%= ENV.fetch('SQL_SERVICE_URL').sub(/^(mysql)[^\d]/, 'mysql2') %>

Which transforms the URI scheme, resulting in :

     "mysql2://sql_user:sql_pass@sql_host_name/sql_db_name"
  • Anchored on scheme in case host name includes 'mysql' as in 'dev-mysql-box'
  • Avoids doubling the '2' in case the URL has been pre-deuced by Ops...

This works great, but it was a journey to get there, and as it turns out, SOME
of our Ops people already had a practice of simply specifying 'mysql2' in
the URL for Rails apps, but it was not widely distributed or documented.

@lugonthier
Copy link

Hello there,
I am trying to deploy my rails app and I have exactly the same error, I did not understand how you repaired it.
What do you replace with what? I am a novice.

@tushar2k19
Copy link

@sci-phi, great explaination. wasted so much time to fix this but couldn't really do it. but you spotted it bang on. Thanks a ton.

mine thing was working fine on local but while deploying, it was breaking

@kali-kb
Copy link

kali-kb commented Jul 14, 2024

@sci-phi thanks so much, i manage to fix this after days of debugging, this should be mentioned in the documentation clearly otherwise it will give an error pointing the developer to something that isn't wrong, which was a big problem in my experience

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants