Rails和Mysql2访问被拒绝用户'root'@'localhost'(使用密码:NO)

时间:2022-04-15 21:01:08

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.

我对Rails有些新意,我的大部分经验都让我感觉如何解决问题,所以如果我错过了明显的解决方案,我会提前道歉。我运行Windows机器。

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.

我正在使用mysql2创建登录和注册。 MySQL安装程序已经用于安装服务器,工作台等(我将root密码配置为密码),我已经安装了mysql2 gem。

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

rails已成功捆绑,但是当我输入rake db:create时,发生了用户'root'@'localhost'(使用密码:NO)的错误访问被拒绝。

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.

然后Rails提示我输入密码,我输入密码但错误再次发生。在第二次输入我的密码之后,似乎它工作正常,直到我尝试执行rails db: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.

这让我感到困惑,因为在MySQL查询中,我的密码设置与我输入的密码相同。我尝试给'root'用户所有的架构权限,但这没有任何区别。请告诉我如何解决这个问题,谢谢。

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

如果您对我的问题有任何疑问,请询问。

5 个解决方案

#1


18  

for me helped

对我有帮助

$ mysql -u root -p

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

and in databse.yml

在databse.yml

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

thanks to @spongezh22

感谢@ spongezh22

#2


8  

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

看起来您的用户无法连接到mysql DB,请在您的控制台中尝试此命令

mysql -u root -p

and when prompted give the password as 'admin'

并在提示时输入密码为“admin”

if this is possible then you should be good to go

如果这是可能的那么你应该好好去

And you database yml file should be like

而你的数据库yml文件应该是这样的

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

#3


1  

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

我只是复制并粘贴上面(下面)到我的rails yml文件,它工作了!!

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

#4


0  

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)

Rake尝试实际创建您的测试数据库,这就是为什么如果没有正确设置测试数据库凭据(例如(database.yml)),您会收到错误消息

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


development:
  <<: *default
  database: atheneum

test:
  <<: *default
  database: atheneum_test

#5


0  

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.

这是我在更改database.yml文件后不得不重新启动rails服务器的少数几次之一。在文件中设置密码后,我不得不重新启动rails服务器。之后,错误就消失了。

#1


18  

for me helped

对我有帮助

$ mysql -u root -p

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

and in databse.yml

在databse.yml

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

thanks to @spongezh22

感谢@ spongezh22

#2


8  

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

看起来您的用户无法连接到mysql DB,请在您的控制台中尝试此命令

mysql -u root -p

and when prompted give the password as 'admin'

并在提示时输入密码为“admin”

if this is possible then you should be good to go

如果这是可能的那么你应该好好去

And you database yml file should be like

而你的数据库yml文件应该是这样的

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

#3


1  

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

我只是复制并粘贴上面(下面)到我的rails yml文件,它工作了!!

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

#4


0  

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)

Rake尝试实际创建您的测试数据库,这就是为什么如果没有正确设置测试数据库凭据(例如(database.yml)),您会收到错误消息

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


development:
  <<: *default
  database: atheneum

test:
  <<: *default
  database: atheneum_test

#5


0  

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.

这是我在更改database.yml文件后不得不重新启动rails服务器的少数几次之一。在文件中设置密码后,我不得不重新启动rails服务器。之后,错误就消失了。