MySQL 用户 'root'@'localhost' 拒绝 Rails 和 Mysql2 访问(使用密码:NO)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21719738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails and Mysql2 Access denied for user 'root'@'localhost' (using password: NO)
提问by spongezh22
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.
导轨已成功捆绑,但当我输入时rake db:create
,出现错误Access denied for user 'root'@'localhost' (using password: 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.
如果您对我的问题有任何疑问,请提问。
回答by zombie_ghast
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
回答by Bharat soni
seems like your user cannot connect to the mysql DB, try this commands in your console
似乎您的用户无法连接到 mysql 数据库,请在您的控制台中尝试此命令
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
回答by macjeans
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
回答by Igorzovisk
Running ./bin/spring stop
fixed for me. I found it in a Github, but can't remember where.
跑步./bin/spring stop
对我来说是固定的。我在 Github 上找到了它,但不记得在哪里。
回答by ltondeleir
My problem was due to the password starting with "!". Changing my password in mysql and database.yml solved it for me.
我的问题是由于密码以“!”开头。在 mysql 和 database.yml 中更改我的密码为我解决了这个问题。
回答by Josiah
Not a rails answer, but for others showing up here:
不是 rails 答案,但对于其他出现在这里的人:
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.
如果您以某种方式将自己的凭据(可能是 ENV 文件)传递给独立脚本,请确保您的密码末尾没有任何不可见的字符,例如“\n”。那真的会惹你生气。
回答by uberdave
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
根据上面 Bhrat 的建议,我将我的 linux 用户创建为 mysql 中的用户。确保用户可以从 $user 更改用户从数据库 yml 中的 root 连接到用户 rake db:create 工作
回答by A. Askarov
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
.
错误是说用户 root 的访问被拒绝。(我没有密码)。我的问题是因为我将默认的 3306 端口更改为 8111。在 database.yml 中我添加了
port: 8111
.
回答by Willian Araujo
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.
就我而言,它只是 database.yml 文件中不正确的数据库密码。我更改了密码,它运行得很好。
回答by Michael Chemani
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 实际上也尝试创建您的test
数据库,这就是为什么如果您没有正确设置您的test
数据库凭据(例如 (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