您好,欢迎访问一九零五行业门户网

MySQLStudy之--MySQL普通用户无法本地登陆_MySQL

mysql study之--mysql普通用户无法本地登陆
在安装完成mysql后,我们通常添加拥有相应权限的普通用户用来访问数据库。在使用用户本地登录数据库的时候,经常会出现怎么登录也无法登录的情况,但是从其它的mysql客户端却可以登录。
故障现象:
[root@mysrv ~]# mysql -u root -poracle
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 10
server version: 5.6.25-enterprise-commercial-advanced-log mysql enterprise server - advanced edition (commercial)
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> select version()\g
+-------------------------------------------+
| version() |
+-------------------------------------------+
| 5.6.25-enterprise-commercial-advanced-log |
+-------------------------------------------+
1 row in set (0.00 sec)
创建用户并授权:
mysql> grant all on prod.* to 'rose'@'%' identified by 'rose';
query ok, 0 rows affected (0.01 sec)
mysql> show grants for rose;
+-----------------------------------------------------------------------------------------------------+
| grants for rose@% |
+-----------------------------------------------------------------------------------------------------+
| grant usage on *.* to 'rose'@'%' identified by password '*86f57026c60b8ce1038efb3b9383ec573979a7bd' |
| grant all privileges on `prod`.* to 'rose'@'%' |
+-----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> select user,host from user;
+-------+-----------+| user | host |+-------+-----------+| jerry | % || rose | % || tom | % || tom1 | % || tom2 | % || root | 127.0.0.1 || root | ::1 || | localhost || jerry | localhost || root | localhost || scott | localhost || tom | localhost || | mysrv || root | mysrv |+-------+-----------+14 rows in set (0.00 sec)
用户登陆:
[root@mysrv ~]# mysql -u rose -prose
error 1045 (28000): access denied for user 'rose'@'localhost' (using password: yes) ---登陆失败!
[root@mysrv ~]# mysql -u rose -p
enter password:
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 22
server version: 5.6.25-enterprise-commercial-advanced-log mysql enterprise server - advanced edition (commercial)
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> use prod;
error 1044 (42000): access denied for user ''@'localhost' to database 'prod'--
---在不用密码的情况下可以登陆,但没有权限访问,应该是匿名用户的身份 !
远程登陆:
---远程登陆成功!
一、登录后查看mysql.user表的情况
可以看到,我的数据库中有rose用户和匿名用户localhost;
mysql> show grants for rose;
+-----------------------------------------------------------------------------------------------------+
| grants for rose@% |
+-----------------------------------------------------------------------------------------------------+
| grant usage on *.* to 'rose'@'%' identified by password '*86f57026c60b8ce1038efb3b9383ec573979a7bd' |
| grant all privileges on `prod`.* to 'rose'@'%' |
+-----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
二、在本机用rose用户登录,发现不用密码可以登录;
[root@mysql01 ~]# mysql -urose -p
enter password:
mysql> select user(),current_user();
+----------------+----------------+
| user() | current_user() |
+----------------+----------------+
| rose@localhost | @localhost |
+----------------+----------------+
1 row in set (0.00 sec)
登录成功了,使用user()和current_user()两个函数查看所使用的用户。
user()函数返回你在客户端登陆时指定的用户名和主机名。
current_user()函数返回的是mysql使用授权表中的哪个用户来认证你的登录请求。
这里发现,我使用'rose'@'localhost'这个账户登录数据库(因为在本地登陆时没指定主机,默认是以localhost登录),但是数据库使用的是''@'localhost'这个账户来进行登录认证,而''@'localhost'这个匿名用户是没有密码的,因此我输入空密码登录成功了。但是登录后,所对应的用户的匿名用户。
一般在mysql在安装完毕后,我们使用mysql_install_db这个脚本生成授权表,会默认创建''@'localhost'这个匿名用户。正是因为这个匿名用户,影响了其他用户从本地登录的认证。
那么mysql是如何进行用户身份认证呢?
一、当用户从客户端请求登陆时,mysql将授权表中的条目与客户端所提供的条目进行比较,包括用户的用户名,密码和主机。授权表中的host字段是可以使用通配符作为模式进行匹配的,如test.example.com, %.example.com, %.com和%都可以匹配test.example.com这个主机。授权表中的user字段不允许使用模式匹配,但是可以有一个空字符的用户名代表匿名用户,并且空字符串可以匹配所有的用户名,就像通配符一样。 当user表中的host和user有多个值可以匹配客户端提供的主机和用户名时,mysql将user表读入内存,并且按照一定规则排序,按照排序规则读取到的第一个匹配客户端用户名和主机名的条目对客户端进行身份验证。
二、排序规则:对于host字段,按照匹配的精确程度进行排序,越精确的排序越前,例如当匹配test.example.com这个主机时, %.example.com比%.com更精确,而test.example.com比%.example.com更精确。对于user字段,非空的字符串用户名比空字符串匹配的用户名排序更靠前。 user和host字段都有多个匹配值,mysql使用主机名排序最前的条目,在主机名字段相同时再选取用户名排序更前的条目。因此,如果user和host字段都有多个匹配值,主机名最精确匹配的条目被用户对用户进行认证。
了解了这个认证流程,就知道为什么server登录失败了。
使用game在本机登录数据时,不指定-h参数默认为localhost主机登录,而在mysql中有两个匹配的条目:'rose'@'%' 和 ''@'localhost'
匿名用户能够匹配的原因上面说过,空字符串可以匹配所有的用户名,就像通配符一样。
根据mysql认证时的排序规则,第一个条目的用户名排序更前,第二个条目的主机名更精确,排序更前。
而mysql会优先使用主机名排序第一的条目进行身份认证,因此''@'localhost'被用户对客户端进行认证。因此,只有使用匿名用户的空密码才能登录进数据库。就会出现下面的情况了。
解决的方法:删除匿名用户(仅仅为了安全也有这个必要)
为什么root用户不会受影响,而只有普通用户不能从本地登录?
因为mysql_install_db脚本会在授权表中生成'root'@'localhost'这个账户。同样的,使用root登录mysql 时,'root'@'localhost'和''@'localhost'都能匹配登录的账户,但是根据排序规则,主机名相同,而用户名非空字符串优先,因此'root'@'localhost'这个条目的排序更靠前。使用root本地登录是不会被匿名用户遮盖。
解决方法:
授权rose用户本地登陆: mysql> grant all on prod.* to 'rose'@'localhost' identified by 'rose';
query ok, 0 rows affected (0.01 sec)
从本地登陆: [root@mysrv ~]# mysql -u rose -prose
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 26
server version: 5.6.25-enterprise-commercial-advanced-log mysql enterprise server - advanced edition (commercial)
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> use prod;
database changed
mysql> show tables;
+----------------+
| tables_in_prod |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t1;
+------+-------+
| id | name |
+------+-------+
| 10 | tom |
| 20 | jerry |
| 30 | rose |
+------+-------+
3 rows in set (0.00 sec)
---登陆成功!
其它类似信息

推荐信息