'rajivratn'@'%' 和 rajivratn@localhost 指的是 MySQL 中的同一个用户吗?

Are 'rajivratn'@'%' and rajivratn@localhost refer to same user in MySQL?

我想将数据加载到数据库的 table(数据集)中。 我正在使用标准命令

 LOAD DATA INFILE '/home/rajivratn/single_output.tsv' IGNORE INTO TABLE dataset ...

我收到以下权限错误:

ERROR 1045 (28000): Access denied for user 'rajivratn'@'localhost' (using password: YES)

大多数 post 认为此问题是由于 MySQL 上的 FILE 特权引起的,可以通过以下 GRANT 命令修复:

GRANT FILE ON *.* to 'rajivratn'@'%';

我检查了权限,发现如下:

mysql> show grants for 'rajivratn'@'%'
    -> ;
+--------------------------------------+
| Grants for rajivratn@%               |
+--------------------------------------+
| GRANT FILE ON *.* TO 'rajivratn'@'%' |
+--------------------------------------+
1 row in set (0.00 sec)

mysql> show grants;
+------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for rajivratn@localhost                                                                                                                 |
+------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'rajivratn'@'localhost' IDENTIFIED BY PASSWORD 'somepassword'                               |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON `yahoo`.* TO 'rajivratn'@'localhost' |
+------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

但是,我仍然收到错误 1045 (28000):用户 'rajivratn'@'localhost' 的访问被拒绝(使用密码:YES)

此外,为什么我在 rajivratn@localhost 的 Grants 中看不到 FILE 权限,为什么它与 'rajivratn'@'%'

的 grants 不同

有什么解决这个问题的建议吗?

谢谢

按以下方式授予权限-

GRANT USAGE ON *.* TO 'rajivratn'@'localhost' IDENTIFIED BY PASSWORD 'somepassword'; 
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, FILE ON `yahoo`.* TO 'rajivratn'@'localhost'

注意:添加了一个 FILE 权限。

MySQL account names consist of a user name and a host name. This enables creation of accounts for users with the same name who can connect from different hosts.

https://dev.mysql.com/doc/refman/5.6/en/account-names.html

所以,不,这两个用户确实是两个不同的用户,拥有两组独立的权限。

此外,

An account name consisting only of a user name is equivalent to 'user_name'@'%'. For example, 'me' is equivalent to 'me'@'%'.

当您连接时,服务器会使用 mysql.user table 中的条目,以内部排序的顺序来确定您是哪个用户。

The server uses sorting rules that order rows with the most-specific Host values first. Literal host names and IP addresses are the most specific.

The pattern '%' means “any host” and is least specific.

https://dev.mysql.com/doc/refman/5.6/en/connection-access.html

因此,当您从 localhost 连接时,您会与该用户匹配,否则,您会与通配符用户匹配。

您需要删除本地主机上的用户,或者特别是授予它权限。

mysql> GRANT ... TO rajivratn@localhost ...;

最后,我使用 .csv 文件通过以下命令将数据集加载到 'yahoo' 数据集:

mysqlimport -u rajivratn -p --local yahoo dataset.csv

感谢其他两个答案,因为他们也阐明了与 mysql 相关的许多其他重要概念。