mysql:用户拒绝访问

时间:2017-02-28 11:20:59

标签: mysql ruby-on-rails

在我的rails应用程序中,我有数据库的配置

  adapter: mysql2
  host: *****
  username: *****
  password: <%= ENV['MYSQL_PW'] %>
  database: *****
  encoding: utf8
  timeout: 5000
  pool: 5

它在服务器中运行良好。但最近有一个错误,我试图访问rails控制台,但我收到此错误

 Access denied for user '****' (using password: NO) (Mysql2::Error).

我也试图运行迁移,我再次遇到同样的错误。我不明白这里有什么问题。我怎么解决这个问题? 另外,我如何检查在unix环境变量中是否设置了ENV['MYSQL_PW']

这是我的日志

$ rake db:migrate
DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly. (called from block in <top (required)> at )
DEPRECATION WARNING: You did not specify a `log_level` in `production.rb`. Currently, the default value for `log_level` is `:info` for the production environment and `:debug` in all other environments. In Rails 5 the default value will be unified to `:debug` across all environments. To preserve the current setting, add the following line to your `production.rb`:

   config.log_level = :info

. (called from block in tsort_each )
rake aborted!
Mysql2::Error: Access denied for user '****' (using password: NO)

2 个答案:

答案 0 :(得分:0)

在运行命令时,不要忘记指定RAILS_ENV。

默认情况下,rails假定环境是开发环境,但正如我在此处所见,您希望在生产环境中运行这些环境。

简单地做

 RAILS_ENV=production rake db:migrate
 RAILS_ENV=production rails c
 # or alternatively
 rails c -e production

http://guides.rubyonrails.org/command_line.html

答案 1 :(得分:0)

尝试以下方法:

  1. database.yml密码编辑为字符串而不是ENV变量,以识别问题。
  2. 要测试 ENV变量,请使用rails c登录rails控制台并输入ENV['YourVariable']以查看是否已设置
  3. 如果您在终端输入source ~/.bash_profile
  4. 中安装了Unix系统(linux / ubuntu / mac),则可以通过获取bash_profile文件来解决此问题
  5. 如果你有Rails 4,你应该运行以下终端命令spring stop
  6. 在database.yml中,您应该使用以下语法包含ENV变量,因为yml需要.erb语法。 <%= ENV['YOUR VARIABLE'] %> Failing to access environment variables within `database.yml` file
  7. ENV变量区分大小写
  8. 我有这个问题,我能够解决它,但我一直有它们。出于这个原因,我读了几个讨论: Rails 4.1 environment variables not reloading

    很抱歉,如果我无法提供更多帮助

    Fabrizio Bertoglio