Ask A Question


You’re not receiving notifications from this thread.

rails test attempts to create multiple test databases and fails because it cannot connect

Ian Burry asked in Testing

rails 6.0
minitest 5.1
mysql2 gem

I'm having (what seems to me) a rather peculiar problem running rails test right from the get-go. All the correct databases exist, the rails user has what should be all the required privileges, and I've run db:migrate. Rather than using the app test database, it will try to create numbered versions of it and use those. The error I get is like:

Mysql2::Error::ConnectionError: Access denied for user 'rails_remote'@'%' to database 'sample_app_test-3'

Naturally, this fails, because the rails user does not have the CREATE privilege. Even with it, the rails user does not have any privileges on the newly created databases, so it fails again.

How do I get rails to use the existing test database rather than trying to create its own?

ETA: It seems to be associated with the following line in test_helper.rb. Comment it out and things seem to work as expected.

parallelize(workers: :number_of_processors)

Well, I learned about Parallel Testing today, a new "feature" of Rails 6. Kinda cool, but probably shouldn't be a default of newly created applications.

It also means that you need to give your mysql user privileges on databases that match a pattern:

grant all privileges on `sample_app_test-%`.* to 'my_user'@'%';
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 76,990+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.