2
Access denied for user 'root'@'localhost' (using password: YES)

我设置了一个新环境,其中 mysql 没有密码以及我是如何得到这个错误的。我在哪里改变

'root'@'localhost' (using password: YES)

我的 database.yml 显示在下面我的数据库是 sample_app

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
development:
  adapter: mysql2
  database: db/development.mysql2
  pool: 5
  timeout: 5000
  database: sample_app
  username: root
  socket: /tmp/mysql.sock

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  database: db/test.mysql2
  pool: 5
  timeout: 5000
  database: sample_app
  username: root
  socket: /tmp/mysql.sock

 production:
  adapter: mysql2
  database: db/production.mysql2
  pool: 5
  timeout: 5000
  database: sample_app
  username: root
  socket: /var/run/mysqld/mysqld.sock
4

2 回答 2

1

您不应该使用 root 连接到带有应用程序的数据库。创建一个具有名称的新用户/密码,以识别它正在用于特定的应用程序\任务,然后使用它而不是 root。

为空密码添加这个password:

production:
  adapter: mysql2
  database: db/production.mysql2
  pool: 5
  timeout: 5000
  database: sample_app
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

如果您仍然拒绝访问,则数据库表示用户\密码无效

于 2012-05-14T18:10:26.813 回答
1

如果这,

 $ mysql -u root 

带你到 mysql 然后这将工作,

production:
  adapter: mysql2
  database: db/production.mysql2
  pool: 5
  timeout: 5000
  database: sample_app
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

如果您为 root 用户设置了一些密码,请尝试

$ mysql -u root -p
<<It will prompt for a password, key it in>>

如果这将您带到mysql,那么这将起作用,

production:
  adapter: mysql2
  database: db/production.mysql2
  pool: 5
  timeout: 5000
  database: sample_app
  username: root
  password: <<password>>
  socket: /var/run/mysqld/mysqld.sock

故事的道德启示:

首先,尝试从命令行使用 mysql 命令登录 mysql 并查看它是否有效。然后,在 database.yml 中使用这些凭据并从 rails 应用程序登录到 mysql。

于 2012-05-14T19:02:25.133 回答