Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2833615
  • 博文数量: 200
  • 博客积分: 2413
  • 博客等级: 大尉
  • 技术积分: 3067
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-01 22:07
文章分类

全部博文(200)

文章存档

2018年(2)

2017年(8)

2016年(35)

2015年(14)

2014年(20)

2013年(24)

2012年(53)

2011年(44)

分类: Mysql/postgreSQL

2016-04-15 09:25:12



Mysql用户登陆验证过程 & 案例


Mysql用户登陆验证过程:
1、Mysql server将user表读入到内存中,然后排序,排序原则下面会讲
2、客户端尝试连接Mysql服务器,服务器扫描内存中排序过后的user表的条目
3、Mysql服务器采纳第一行匹配的客户端名和用户名,然后验证通过

Mysql服务器内存中user表排序原则:
1、最具象(most-specific)的条目放在最前面。
2、user表的host列,文本主机名(Literal host names)和IP地址是最具象的。子网掩码的方式,如192.168.1.0/255.255.255.0的具象程度等同于192.168.1.13,都是最具象的;子网掩码192.168.1.0/255.255.255.0的具象程度高于192.168.1.%。'%'是最不具象的,优先级排在后面;空字符串''意思是any host,具象级别排在'%'之后。
3、host值相同,最具象的值排在最前面;user值为空,以为着any user,是最不具象的。

总结:
1、user表中的host和user列按照具象程度排序加载到内存中,最具象的排在最前面。
2、host具象程度相同时,再去按照user的具象程度从高到底匹配;若没有匹配上,那么转移到次具象的host继续匹配,直至匹配上为止;若没有匹配上,那么登录验证宣告失败。



案例:
测试环境中,三台主机组成Master slave集群,gp-s2是master,gp-s1和gp-s3是slave,并且这三台服务器都在同一个网段。

gp-s2 Master上的user表如下,rep1用户允许 10.9.15.% 这个网段都访问gp-s2。

+-------------------------+-------+

| host                    | user  |

+-------------------------+-------+

| %                       | root  |

| %                       | test3 |

| 10.9.15.%               | rep1  |

| 10.9.15.%               | test  |

| 10.9.15.%               | test2 |

| 10.9.15.0/255.255.255.0 | wang  |

| 10.9.15.18              | root  |

| 127.0.0.1               | root  |

| ::1                     | root  |

| gp-master               | root  |

| gp-s1                   |       |

| gp-s1                   | root  |

| gp-s1                   | test  |

| gp-s3                   | test  |

| localhost               |       |

| localhost               | root  |

+-------------------------+-------+

16 rows in set (0.00 sec)



使用rep1用户在gp-s1上无法登录gp-s2

[root@gp-s1 ~]# mysql -urep1 -p123456 -h gp-s2

ERROR 1045 (28000): Access denied for user 'rep1'@'gp-s1' (using password: YES)



使用rep1用户在gp-s2上能够登录gp-s2

[root@gp-s3 ~]#  mysql -urep1 -p123456 -h gp-s2

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 17

Server version: 5.5.48-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2016, 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> 



这是一个很奇怪的问题,相同用户、在相同网段,gp-s1可以登录,gp-s3不可以登录。

试着在master上给rep1用户加上新权限

mysql> grant replication slave on *.* to 'rep1'@'10.9.15.19' identified by "123456";

 

这次在gp-s1主机上登录master可以顺利登录,但是走的条目是'10.9.15.19',而不是'10.9.15.%'

[root@gp-s1 ~]# mysql -urep1 -p123456 -h gp-s2      

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 34

Server version: 5.5.48-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2016, 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> 

mysql> select user(),current_user();

+------------+-----------------+

| user()     | current_user()  |

+------------+-----------------+

| rep1@gp-s1 | rep1@10.9.15.19 |

+------------+-----------------+

1 row in set (0.00 sec)

gp-s3登录则是走的正常条目'10.9.15.%'

[root@gp-s3 ~]# mysql -urep1 -p123456 -h gp-s2   

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 35

Server version: 5.5.48-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2016, 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 user(),current_user();

+----------------+----------------+

| user()         | current_user() |

+----------------+----------------+

| rep1@10.9.15.2 | rep1@10.9.15.% |

+----------------+----------------+

1 row in set (0.00 sec)  



这说明gp-s1到gp-s2网络是没有问题的。


采用排错法,gp-s3正常,那么比对gp-s1和gp-s3在user表中的区别,最后找到了根源。

+-------------------------+----------+-------------------------------------------+

| host                    | user     | password                                  |

+-------------------------+----------+-------------------------------------------+

| %                       | root     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| %                       | test3    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 10.9.15.%               | rep1     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 10.9.15.%               | test     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 10.9.15.%               | test2    | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 10.9.15.%               | wchbsync | *55D02D8B1D286B078AC8DC9CD05E2D5D80906F3F |

| 10.9.15.0/255.255.255.0 | wang     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 10.9.15.18              | root     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 10.9.15.19              | rep1     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 10.9.15.19              | rep2     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| 127.0.0.1               | root     |                                           |

| ::1                     | root     |                                           |

| gp-master               | root     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| gp-s1                   |          |                                           |    <---- 问题出在这里

| gp-s1                   | root     |                                           |

| gp-s1                   | test     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| gp-s3                   | test     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| localhost               |          |                                           |

| localhost               | root     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

+-------------------------+----------+-------------------------------------------+


将这一行删掉,问题解决。

那么为什么会出现这种问题呢??
因为gp-s1登陆gp-s2,首先去匹配host,按照具象程度,这3个条目匹配到。

| gp-s1                   |          |                                           |    <---- 问题出在这里

| gp-s1                   | root     |                                           |

| gp-s1                   | test     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |


匹配到host gp-s1之后,再按照具象程度匹配user,user列没有rep1用户,最后只能匹配空值'',空值意味着any user。对于用户空值并且密码同时为空的情况,意味着无法远程登陆。

**特别说明
如果用户名为空,但是密码不为空的情况。远程用户登录,只要密码正确,无论用户怎么写,都能登录。

这里也要特别注意,对于用户空值的情况,一定要及时删除,避免出现不必要的麻烦!


转载请注明:
十字螺丝钉
http://blog.chinaunix.net/uid/23284114.html

QQ:463725310
E-MAIL:houora#gmail.com(#请自行替换为@)
阅读(2596) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~