1

I get the following error when I run 'rails db:schema:dump' after initially configuring my Ruby on Rails project and checking the schema set-up for my Mysql database, which should be empty in terms of data so far the tables I've created:

Mysql2::Error::ConnectionError: Access denied for user 'rails_user'@'localhost' (using password: YES)

I enter mysql -u root -p in the command line and create the following databases:

CREATE DATABASE simple_ex_development;

CREATE DATABASE simple_ex_test;

Now instead of using root user to connect to my newly created databases (which isn't an optimal security practice), I define a new user that the rails application can use to connect to them called 'rails_user' as follows:

GRANT ALL PRIVILEGES simple_ex_development.* TO 'rails_user'@'localhost' IDENTIFIED BY 'blahpassword'

GRANT ALL PRIVILEGES simple_ex_test.* TO 'rails_user'@'localhost' IDENTIFIED BY 'blahpassword'

Then I exit mysql and go to my rails application config/database.yml file and update the username and password, respectively:

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

Then when I go back to the terminal and run 'rails db:schema:dump' I get the following error above, when I'm not expecting to get anything at all. I've read other stackoverflow posts here touching on similar issues and hinting that it might be anonymous user.

Why am I getting this error message and how can I resolve it?

Any pointers here would be great, thank you!

P.S. When I do run rails server I can see the web app on localhost but again, I'm not sure if it's being configured properly to the new user and password versus root and my root password.

1 Answer 1

2

Try creating the user before granting privileges:

CREATE USER 'rails_user'@'localhost' IDENTIFIED BY 'blahblahpassword';

And then grant the privileges the same way:

GRANT ALL PRIVILEGES ON simple_ex_development.* TO 'rails_user'@'localhost' IDENTIFIED BY 'blahpassword';

GRANT ALL PRIVILEGES ON simple_ex_test.* TO 'rails_user'@'localhost' IDENTIFIED BY 'blahpassword';
Sign up to request clarification or add additional context in comments.

3 Comments

This looks promising. Although I do get the following error message when starting grant privileges: "ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'simple_ex_development.* TO 'rails_user'@'localhost' IDENTIFIED BY 'blahpassword' at line 1" Why do I get this error and how can I resolve it?
my bad, commands should be GRANT ALL PRIVILEGES ON simple_ex_development.* ....
For some reason, I'm still getting the exact same error here. Hmm, not sure why, even though the syntax appears correct. Any other thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.