Ruby-on-rails Mysql2::Error(用户'root'@'localhost'的访问被拒绝(使用密码:NO)):

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8675489/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:32:53  来源:igfitidea点击:

Mysql2::Error (Access denied for user 'root'@'localhost' (using password: NO)):

ruby-on-railsmysql2

提问by webmaster_sean

Just setup a new rails 3.1.3 app using mysql (mysql2 gem) on CentOS 5 server / apache / passenger... I have correctly setup a database and a user for that database and I have added the login and info into my database.yml file... I can generate stuff, and rake db:migrate ok but the "We're sorry, but something went wrong." message is being rendered in the browser and this message is showing up in my production.log file!

只需在 CentOS 5 服务器/apache/passenger 上使用 mysql(mysql2 gem)设置一个新的 rails 3.1.3 应用程序......我已经正确设置了一个数据库和该数据库的用户,并且我已将登录名和信息添加到我的数据库中。 yml 文件...我可以生成东西,并且 rake db:migrate ok 但是“我们很抱歉,但是出了点问题。” 消息正在浏览器中呈现,此消息显示在我的 production.log 文件中!

Started GET "/" for xx.xxx.xx.xxx at 2011-12-29 19:52:35 -0600

在 2011-12-29 19:52:35 -0600 开始为 xx.xxx.xx.xxx 获取“/”

Mysql2::Error (Access denied for user 'root'@'localhost' (using password: NO)):

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

weird, I am not using "root" as the login info in database.yml... Any suggestions?

奇怪,我没有使用“root”作为 database.yml 中的登录信息......有什么建议吗?

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: the_db_I_made
  pool: 5
  username: the_user_I_made
  password: the_password
  socket: /var/lib/mysql/mysql.sock

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: the_db_I_made
  pool: 5
  username: the_user_I_made
  password: the_password
  socket: /var/lib/mysql/mysql.sock

采纳答案by webmaster_sean

Ok, I guess this is all part of my learning curve :)

好的,我想这是我学习曲线的一部分:)

What it ended up being was this: The gem for passenger that I installed originally was version 3.0.9, I did a gem update --system and that installed version 3.0.11. So the server thought it was using version 3.0.11 and I was still pointing to libraries for 3.0.9 in my rails_server.conf file. I found how to upgrade on http://blog.phusion.nl/

结果是这样的:我最初安装的乘客 gem 是 3.0.9 版,我做了一个 gem 更新 --system 并安装了 3.0.11 版。所以服务器认为它使用的是 3.0.11 版本,而我仍然在我的 rails_server.conf 文件中指向 3.0.9 的库。我在http://blog.phusion.nl/上找到了如何升级

回答by vvohra87

Bunch of questions / suggestions:

一堆问题/建议:

  1. Can you connect to the database using the terminal?

    mysql -u root -p
    
  2. Also, have you tried this on development mode? If so, please share the results.

  3. Try removing

    socket: /var/lib/mysql/mysql.sock
    
  4. Is the gem installation correct?

    gem check mysql2
    
  1. 您可以使用终端连接到数据库吗?

    mysql -u root -p
    
  2. 另外,您是否在开发模式下尝试过这个?如果是这样,请分享结果。

  3. 尝试删除

    socket: /var/lib/mysql/mysql.sock
    
  4. gem安装正确吗?

    gem check mysql2
    

EDIT:

编辑:

There is a lot of difference in development and production mode ( including but not limited to, environment variables like the database connection string, asset pre-compilation, different level of logging, custom debug info on error pages )

开发和生产模式有很多不同(包括但不限于环境变量,如数据库连接字符串、资产预编译、不同级别的日志记录、错误页面上的自定义调试信息)

** Embarrassing!!**

** 尴尬!!**

You have not included the "host" property in your config!

您尚未在配置中包含“host”属性!

Try this:

尝试这个:

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  host: your_host #<----- normally localhost
  database: the_db_I_made
  pool: 5
  username: the_user_I_made
  password: the_password
  socket: /var/lib/mysql/mysql.sock

回答by user1629670

  1. In Ubuntu 1)/usr/bin/mysql_secure_installation 2) Run in Terminal follow the steps carefully change the mysql password here.Everything done .then go to database.yml change the password previously what u have created. Run rails server and check the application .. it will works
  1. 在 Ubuntu 1)/usr/bin/mysql_secure_installation 2) 在终端中运行,按照这里的步骤仔细更改 mysql 密码。一切都完成了。然后去 database.yml 更改您之前创建的密码。运行 rails server 并检查应用程序..它会工作

回答by NameNotFoundException

Did you create the database "the_db_I_made"?

您是否创建了数据库“the_db_I_made”

First create it, then only run it, else it will show this error continuously.

首先创建它,然后才运行它,否则它会不断显示此错误。

Now try to tun it.

现在尝试调整它。

回答by Pratik Naik

For Me It was giving error because of no database. So i created one Using :

对我来说,由于没有数据库,它给出了错误。所以我创建了一个使用:

$ rake db:create

$耙数据库:创建

And the problem was solved :D

问题解决了:D