Rails y Mysql2 Acceso denegado para el usuario 'root'@'localhost' (con contraseña: NO)

I am somewhat new to Rails, and much of my experience involves me feeling out how to work out the problem so I apologize in advance if I have missed and obvious solution. I run a Windows machine.

I am creating a log-in and registration using mysql2. MySQL installer has already been used to install the server, workbench, etc. (I configured the root password as password) and I have already installed the mysql2 gem.

The rails was bundled successfully but when I entered rake db:create, el error Access denied for user 'root'@'localhost' (using password: NO) ocurrido.

Rails then prompted me for my password, I entered it but the error occurred again. After entering my password the second time, it seemed as if it worked fine until I tried to do a rails db:migrate wherein the error appeared again making it not possible to migrate.

This confuses me because in the MySQL query, I have my password set as the same one I entered. I tried giving the 'root' user all the schema privileges, but that made no difference. Please tell me how to solve this problem and thank you.

If you have any questions about my question, please ask.

preguntado el 12 de febrero de 14 a las 05:02

12 Respuestas

para mí ayudó

$ mysql -u root -p

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mypassword')

and in databse.yml

adapter: mysql2
database: mydb
host: localhost
username: root
password: "mypassword" #I had an empty string here before

thanks to @spongezh22

respondido 24 nov., 15:17

Same problem if your password starts with the ! character. - Franck Boudraa

@zombie_ghast It doesn't work for me. It still asks a password when I run mysql -u root -p password is same as I set during installation. please help me to resolve this issue. - elvinspro

Don't forget your semicolon for: SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mypassword'); - quimturión

seems like your user cannot connect to the mysql DB, try this commands in your console

mysql -u root -p

and when prompted give the password as 'admin'

if this is possible then you should be good to go

And you database yml file should be like

 adapter: mysql2
 database: mydb
 host: localhost
 username: root
 password: mypass

Respondido 12 Feb 14, 05:02

Thanks, that seemed to of fixed the migration but now I changed the routes file to Login::Application.routes.draw do resources :users get '/register' root 'users#index' y cuando lo hice rails s , a large error occured - spongezh22

The error is too large for the comment size but I tried reinstalling everything and when I created a fresh program with mysql and did rails s , the browser shows Mysql2::Error Access denied for user 'root'@'localhost' (using password: NO) Rails.root: C:/Sites/rails_projects/practice . I would assume I have missed doing something in the Mysql workbench but I am not sure what it could be. @Bharatsoni - spongezh22

My Mysql workbench has the command SET PASSWORD FOR 'root'@'localhost' = PASSWORD('mypassword') if this information helps - spongezh22

I just copied and pasted the above (below) to my rails yml file and it worked!!

adapter: mysql2
database: mydb
host: localhost
username: root
password: mypass

Respondido el 12 de diciembre de 14 a las 16:12

Rake try in fact to create your test database as well, that's why you get the error message if you do not set correctly your test database credentials like (database.yml)

default: &default
    adapter: mysql2
    encoding: utf8
    database: atheneum
    pool: 5
    username: ******
    password: "******"
    host: localhost

  <<: *default
  database: atheneum

  <<: *default
  database: atheneum_test

respondido 28 mar '17, 08:03

Correr ./bin/spring stop fixed for me. I found it in a Github, but can't remember where.

Respondido el 19 de Septiembre de 18 a las 20:09

My problem was due to the password starting with "!". Changing my password in mysql and database.yml solved it for me.

Respondido 19 Oct 18, 12:10

In my case, it was just an incorrect database password that was in the database.yml file. I changed the password and it worked perfectly.

Respondido el 17 de enero de 20 a las 15:01

This was one of the few occasions where I had to restart the rails server after I made changes to the database.yml file. After I set the password in the file, I had to restart the rails server. After that the error went away.

Respondido 05 Feb 18, 14:02

Not a rails answer, but for others showing up here:

If you are passing in your own credentials somehow, perhaps an ENV file to a standalone script, be sure that you don't have any invisible characters like a '\n' at the end of your password. That'll really mess with you.

Respondido 06 Feb 19, 18:02

Error was saying that Access denied for user root. (I had no password). My problem was for the reason that i've changed the default 3306 port to 8111. In database.yml ive added port: 8111.

Respondido 28 ago 19, 07:08

If you don't remember setting your password, do this:

sudo mysql -u root

use mysql;

UPDATE user set authentication_string='MyPassword' where user='root';

This will update your password. Then, change the database/config.yml file to match your password.

(Depending on the version of MySQL you're running, you might have to surround the 'password' with a PASSWORD() function or a sha2() function)

Respondido el 22 de enero de 21 a las 04:01

On advise of Bhrat above I created my linux user as a user in mysql. ensured that user can connect from $user changed user from root in database yml to user rake db:create worked

Respondido 05 Abr '19, 07:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.