首先让我提一下,我已经看了很多建议的问题,但没有找到相关的答案。这就是我正在做的。

我连接到Amazon EC2实例。我可以用这个命令登录MySQL根目录:

mysql -u root -p

然后我用host %创建了一个新的用户帐单

CREATE USER 'bill'@'%' IDENTIFIED BY 'passpass';

授予用户bill的所有权限:

grant all privileges on *.* to 'bill'@'%' with grant option;

然后我退出root用户,尝试用bill登录:

mysql -u bill -p

输入正确的密码并得到以下错误:

错误1045(28000):用户“账单”@“localhost”(使用密码:YES)的访问被拒绝


当前回答

您可能有一个匿名用户“@'localhost'或“@'127.0.0.1'”。

根据手册:

当可能有多个匹配时,服务器必须确定其中的哪一个 使用它们。它解决这个问题如下:(…) 当客户端尝试连接时,服务器会检查mysql表的[行。用户]。 服务器使用与客户端主机名和用户名匹配的第一行。 (…) 服务器使用排序规则首先对具有最特定的Host值的行进行排序。 字面主机名[如'localhost']和IP地址是最具体的。

因此,当从本地主机连接时,这样的匿名用户将“屏蔽”任何其他用户,如“[any_username]”@“%”。

'bill'@'localhost'不匹配'bill'@'%',但会匹配(例如)“@事先“localhost”。

推荐的解决方案是删除这个匿名用户(这通常是一件好事)。


下面的编辑大多与主要问题无关。这些只是为了回答本帖其他评论中提出的一些问题。

编辑1

通过套接字验证为'bill'@'%'。


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass --socket=/tmp/mysql-5.5.sock
    Welcome to the MySQL monitor (...)
    
    mysql> SELECT user, host FROM mysql.user;
    +------+-----------+
    | user | host      |
    +------+-----------+
    | bill | %         |
    | root | 127.0.0.1 |
    | root | ::1       |
    | root | localhost |
    +------+-----------+
    4 rows in set (0.00 sec)
    
    mysql> SELECT USER(), CURRENT_USER();
    +----------------+----------------+
    | USER()         | CURRENT_USER() |
    +----------------+----------------+
    | bill@localhost | bill@%         |
    +----------------+----------------+
    1 row in set (0.02 sec)
    
    mysql> SHOW VARIABLES LIKE 'skip_networking';
    +-----------------+-------+
    | Variable_name   | Value |
    +-----------------+-------+
    | skip_networking | ON    |
    +-----------------+-------+
    1 row in set (0.00 sec)
    

编辑2

完全相同的设置,除了我重新激活了网络,我现在创建了一个匿名用户“@'localhost'。


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql
    Welcome to the MySQL monitor (...)
    
    mysql> CREATE USER ''@'localhost' IDENTIFIED BY 'anotherpass';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> Bye

    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        --socket=/tmp/mysql-5.5.sock
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        -h127.0.0.1 --protocol=TCP
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)
    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -ppass \
        -hlocalhost --protocol=TCP
    ERROR 1045 (28000): Access denied for user 'bill'@'localhost' (using password: YES)

编辑3

与编辑2中的情况相同,现在提供匿名用户的密码。


    root@myhost:/home/mysql-5.5.16-linux2.6-x86_64# ./mysql -ubill -panotherpass -hlocalhost
    Welcome to the MySQL monitor (...)

    mysql> SELECT USER(), CURRENT_USER();
    +----------------+----------------+
    | USER()         | CURRENT_USER() |
    +----------------+----------------+
    | bill@localhost | @localhost     |
    +----------------+----------------+
    1 row in set (0.01 sec)

来自编辑1的结论1:可以通过套接字验证为'bill'@'%'。

结论2,来自编辑2:一个人是通过TCP连接还是通过套接字连接对身份验证过程没有影响(除非一个人不能通过套接字以'something'@'localhost'的身份连接,显然)。

来自编辑3的结论3:尽管我指定了-ubill,但我被授予了匿名用户的访问权限。这是因为上面建议的“排序规则”。注意,在大多数默认安装中,都存在一个无密码的匿名用户(应该保护/删除)。

其他回答

更新:在v8.0.15(可能是这个版本),PASSWORD()函数不起作用。

你必须:

确保你已经停止MySQL。 使用特权绕过安全模式运行服务器:sudo mysqld_safe——skip-grant-tables 登录:mysql -u root 更新mysql。SET authentication_string=null WHERE user ='root'; mysql> FLUSH PRIVILEGES; mysql >退出; 再次登录:mysql -u root mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd'

如果您使用旧版本的MySQL UI(如SQLYoug),生成的密码带有错误的散列,也会出现这个问题。

用SQL脚本创建用户可以解决这个问题。

我也遇到了类似的问题——当我第一次尝试以root用户登录MySQL时,它告诉我访问被拒绝。原来我忘了用sudo…

所以,如果你第一次尝试root失败了,试试:

sudo mysql -u root -p

然后输入密码,就可以了。

对我来说,root有一个默认密码

我用ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password '修改密码;这个方法奏效了

MySQL account names consist of a user name and a host name, The name 'localhost' in host name indicates the local host also You can use the wildcard characters “%” and “_” in host name or IP address values. These have the same meaning as for pattern-matching operations performed with the LIKE operator. For example, a host value of '%' matches any host name, whereas a value of '%.mysql.com' matches any host in the mysql.com domain. '192.168.1.%' matches any host in the 192.168.1 class C network.

以上只是介绍:

实际上,用户“bill”@“localhost”和“bill”@“%”是不同的MySQL帐户,因此两者都应该使用自己的身份验证细节,如密码。

欲了解更多信息,请参阅http://dev.mysql.com/doc/refman//5.5/en/account-names.html