MySQL Mysql2::Error: 用户 'test'@'localhost' 对数据库 'depot_test' 的访问被拒绝

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

Mysql2::Error: Access denied for user 'test'@'localhost' to database 'depot_test'

mysqlruby-on-rails

提问by DynastySS

I am a bit stumped here. I have created a database and have had no issues using the depot_production database. However, of late whenever I run rake test, I get a bunch of errors like

我有点难住这里。我已经创建了一个数据库并且使用 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'

The odd thing is I think my database.yml file is fine. And everytime I run db:migrate I just get an empty line returned to me. I also added a user test, but I think that only added it to my development database. I sorta think my test and production and databases don't exist...

奇怪的是我认为我的 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

Any advice would be appreciated, thanks.

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

Thanks for sticking with me here. I feel like I am close but something is odd. Here is what I did.

谢谢你一直陪着我。我觉得我很接近,但有些事情很奇怪。这是我所做的。

 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

回答by 1337holiday

So first login as rootor whatever your root user is called from terminal.

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

mysql -u root -p

CREATE DATABASE depot_test

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

USE depot_test

Once you are logged in to mysql, grant privileges to the user test(remember to change password)

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

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

FLUSH PRIVILEGES;

You will need to change your pass to "mypass123" in your database.yml

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