mysql 错误信息:拒绝用户'test'@'localhost'访问数据库'depot_test'

tvz2xvvm  于 2022-12-28  发布在  Mysql
关注(0)|答案(2)|浏览(58)

我在这里遇到了一点困难,我已经创建了一个数据库,并且在使用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
jc3wubiy

jc3wubiy1#

因此,首先以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;

您需要在数据库中将通行证更改为“mypass123”。yml

kpbpu008

kpbpu0082#

@1337holiday提供的解决方案可以工作,但我猜在大多数情况下,我们不希望对希望允许其他人连接的数据库授予GRANT ALL权限。例如,如果您像我一样,只希望给予对特定数据库的只读访问权限,则可以简单地向数据库或其对象授予特定权限(例如GRANT SELECT)-并分配此权限will grant a connect privilege to the particular mysql schema/database as well。我猜文档中没有那么清楚,但是在这个数据库或它的对象上具有任何被授予的特权(SELECT、CREATE、DELETE、UPDATE、DROP和许多其他特权)就足以连接到数据库。

因此,您可以发出如下语句(由具有足够权限的root或用户发出)

CREATE USER 'local' IDENTIFIED WITH 'caching_sha2_password' BY 'local';
CREATE DATABASE local;
GRANT SELECT ON local.* TO 'local';

并且此后local用户将具有使用以下各项连接到local数据库的能力:

USE local;

或者通过编程语言客户端的方式。我希望这对你有帮助:)

相关问题