3

I'm new to Rails and having this issue for which I can't find a solution. I've seen some related to Python but nothing Rails specific. This is the error when running docker-compose run web rails db:setup:

RSA Encryption not supported - caching_sha2_password plugin was built with GnuTLS support
Couldn't create 'mydb' database. Please check your configuration.
rails aborted!
Mysql2::Error: RSA Encryption not supported - caching_sha2_password plugin was built with GnuTLS support
/usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.2/lib/mysql2/client.rb:90:in `initialize'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `new'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/mysql2_adapter.rb:24:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:879:in `new_connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:923:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:902:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:863:in `acquire_connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:587:in `checkout'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:431:in `connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:1111:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_handling.rb:231:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/connection_handling.rb:199:in `connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/mysql_database_tasks.rb:8:in `connection'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:126:in `create'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:185:in `block in create_current'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:479:in `block (2 levels) in each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:476:in `each'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:476:in `block in each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:475:in `each'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:475:in `each_current_configuration'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/tasks/database_tasks.rb:184:in `create_current'
/usr/local/bundle/gems/activerecord-6.0.0/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/railties-6.0.0/lib/rails/commands/rake/rake_command.rb:23:in `block in perform'
/usr/local/bundle/gems/railties-6.0.0/lib/rails/commands/rake/rake_command.rb:20:in `perform'
/usr/local/bundle/gems/railties-6.0.0/lib/rails/command.rb:48:in `invoke'
/usr/local/bundle/gems/railties-6.0.0/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:setup => db:schema:load_if_ruby => db:create
(See full trace by running task with --trace)
ERROR: 1

Any ideas would be appreciated.

EDIT - adding database.yml

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>
  port: <%= ENV['DB_PORT'] %>
  host: <%= ENV['DB_HOST'] %>
  database: <%= ENV['DB_NAME'] %>
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default

Edit - for clarity the error is Mysql2::Error: RSA Encryption not supported - caching_sha2_password plugin was built with GnuTLS support and specifying mysql_native_password as the authentication plugin doesn't resolve the error.

Fo.
  • 3,752
  • 7
  • 28
  • 44
  • When you run `rails db:setup` it tries to create the database with the config (typically in `config/database.yml`), because you are configured to use Mysql, the `mysql2` gem/adapter tries to execute a statement like `CREATE DATABASE mydb;`, but that is failing for you here. Maybe your config is specifying an encryption mode that the database that docker connects to doesn't support? – Unixmonkey Mar 23 '22 at 20:10
  • I haven't specified an encryption mode (added config above) – Fo. Mar 23 '22 at 20:16
  • To be clear I understand what that step in my process is doing, I just don't know how to resolve the error in a Rails environment. – Fo. Mar 23 '22 at 20:24
  • 1
    Rails is trying to connect to your database but is unable to, likely due to the encryption mode for your Mysql user's password. [this answer](https://stackoverflow.com/a/52994785/23915) suggests changing the algorigthm mysql uses at the docker level, but you may also want to try upgrading the `mysql2` gem. – Unixmonkey Mar 23 '22 at 20:29
  • Thank you - I did see that question/answer earlier but the solution doesn't work in this case. The error is different, in that case the plugin is not found. I don't have that issue. – Fo. Mar 23 '22 at 20:41
  • In the end after blowing away everything and rebuilding, adding `command: --default-authentication-plugin=mysql_native_password` to the docker-compose file did solve the issue (thank you) though it would be great to understand how to enable the default mysql 8 authentication method. – Fo. Mar 24 '22 at 13:27
  • 2
    From what I understand from what I've read, the new MySQL database default is to use this `caching_sha2_password`, but most versions of `libmysqlclient` in the wild don't know about it yet, and need `mysql_native_password` style passwords to connect. The `mysql2` gem is built using the `libmysqlclient` libraries, so it isn't working. There seem to be some notes and work-arounds detailed here: https://github.com/brianmario/mysql2/issues/1023 – Unixmonkey Mar 24 '22 at 18:49

0 Answers0