通过命令行客户端以“root”身份连接到MySQL,我试图用以下命令创build一个MySQL用户:
mysql> create user 'myuser'@'%' identified by 'mypass'; Query OK, 0 rows affected (0.00 sec) mysql> grant all privileges on MY_DATABASE.* to 'myuser'@'%' identified by 'mypass'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
用户被创build,但其密码和特权不是“粘住”的。 当我尝试使用密码login时,出现错误:
~$ mysql -u myuser -p Enter password: ERROR 1045 (28000): Access denied for user 'myuser'@'localhost' (using password: YES)
但是,我可以通过简单地按下[enter]无需密码login:
~$ mysql -u myuser -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 95 Server version: 5.5.28-0ubuntu0.12.04.3 (Ubuntu) Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
但是,用户仍然看不到任何表…所以不仅密码不能保存,而且我设置的权限也丢失了。
有谁知道我在这里错过了什么? 谢谢!
更新:按照注释中的要求从mysql.user表中发布内容:
mysql> select User, Host, Password from mysql.user; +------------------+---------------------+-------------------------------------------+ | User | Host | Password | +------------------+---------------------+-------------------------------------------+ | root | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | | root | 127.0.0.1 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | | root | ::1 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | | | localhost | | | debian-sys-maint | localhost | *817684763DD0B095B4703EC55053DAB57A2D9F4F | | phpmyadmin | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | | myuser | % | *C5250F20FB991AA969917726DE6547D425DB3234 | +------------------+---------------------+-------------------------------------------+
你有一个localhost用户,匿名用户没有任何密码。
有必要为您的
myuser同时拥有(%和localhost)帐户,以便能够以myuser身份从任何位置进行连接。 如果没有本地主机帐户,当myuser从localhost连接时,由mysql_install_db创build的localhost的anonymous user帐户将优先。 因此,myuser将被视为anonymous user。 原因是匿名用户帐户具有比myuser'@'%'帐户更具体的主机列值,因此在用户表sorting顺序中更早。
关于sorting顺序:
服务器使用sorting规则,首先排列具有最特定主机值的行。 文字主机名和IP地址是最具体的。 (文字IP地址的特殊性不受它是否具有networking掩码的影响,例如
192.168.1.13和192.168.1.0/255.255.255.0被认为是同样具体的。)模式'%'表示"any host",是最不具体的。 空string''也意味着"any host"但在'%'之后sorting。 具有相同主机值的行首先与最具体的用户值sorting(空白用户值意味着"any user"并且是最不具体的)。
所以mysql正在考虑你的myuser作为一个anonymous user ,因为localhost的anonymous user没有任何密码,所以它可以不用密码login。
要解决你的问题,你只需要为你的myuser用密码创build一个localhost用户