Codestudio
Codestudio

Reputation: 525

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

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.

Upvotes: 1

Views: 1616

Answers (1)

Furkan E Apaydın
Furkan E Apaydın

Reputation: 324

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';

Upvotes: 2

Related Questions