【问题标题】:Mysql2::Error: Access denied for user 'test'@'localhost' to database 'depot_test'Mysql2::Error: 用户'test'@'localhost'拒绝访问数据库'depot_test'
【发布时间】:2013-04-14 19:27:25
【问题描述】:

我在这里有点难过。我创建了一个数据库,使用 depot_production 数据库没有任何问题。但是,最近每当我运行 rake 测试时,我都会收到一堆错误,例如

# Running tests:

EEEEEEEE

Finished tests in 0.031499s, 253.9763 tests/s, 0.0000 assertions/s.

1) Error:
test_should_create_product(ProductsControllerTest):
Mysql2::Error: Access denied for user 'test'@'localhost' to database 'depot_test'

奇怪的是我认为我的 database.yml 文件很好。每次我运行 db:migrate 时,我都会得到一个空行返回给我。我还添加了一个用户测试,但我认为这只是将它添加到我的开发数据库中。我有点认为我的测试、生产和数据库不存在......

development:
adapter: mysql2
encoding: utf8
reconnect: false
database: depot_development
pool: 5
username: root
password: admin
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
encoding: utf8
reconnect: false
database: depot_test
pool: 5
username: test
password: testy
socket: /tmp/mysql.sock

production:
adapter: mysql2
encoding: utf8
reconnect: false
database: depot_production
pool: 5
username: prod
password: mypassword
socket: /tmp/mysql.sock

任何建议将不胜感激,谢谢。

感谢您在此陪伴我。我觉得我很接近,但有些奇怪。这就是我所做的。

 mysql> use depot_test;
ERROR 1049 (42000): Unknown database 'depot_test'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| depot_development  |
| development        |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.01 sec)

mysql> use depot_test
ERROR 1049 (42000): Unknown database 'depot_test'
mysql> use test
Database changed
mysql> GRANT SELECT, INSERT, DELETE ON `test` TO test@'localhost' IDENTIFIED BY 'testy';
ERROR 1146 (42S02): Table 'test.test' doesn't exist
mysql> GRANT SELECT, INSERT, DELETE ON `depot_test` TO test@'localhost' IDENTIFIED BY       'testy';
ERROR 1146 (42S02): Table 'test.depot_test' doesn't exist

【问题讨论】:

  • 您需要授予用户“test”权限以更改“depot_test”

标签: mysql ruby-on-rails


【解决方案1】:

所以首先以 root 或从终端调用的任何 root 用户身份登录。

mysql -u root -p

CREATE DATABASE depot_test

CREATE USER 'test'@'localhost' IDENTIFIED BY 'mypass123';

USE depot_test

登录mysql后,授予用户test权限(记得改密码)

GRANT ALL privileges on depot_test.* to test@localhost identified by 'mypass123';

FLUSH PRIVILEGES;

您需要在 database.yml 中将您的通行证更改为“mypass123”

【讨论】:

  • 谢谢!我尝试并得到了以下内容。 ERROR 1046 (3D000): 未选择数据库
  • 哦,是的,您必须选择 depot_test 数据库,查看编辑
  • 抱歉,消息过长。我已经编辑了我的原始帖子并添加了它。谢谢!
  • 您似乎没有名为 depot_test 的数据库,但您有一个名为 test 的数据库,因此我们将创建 depot_test
  • 很高兴听到!安装 Rails 总是很痛苦,幸运的是你只有 mysql 问题:)
猜你喜欢
  • 1970-01-01
  • 2012-01-30
  • 2015-05-12
  • 1970-01-01
  • 2014-06-14
  • 2015-05-28
  • 2018-06-24
  • 2012-01-27
  • 1970-01-01
相关资源
最近更新 更多