码迷,mamicode.com
首页 > 数据库 > 详细

MySQL权限及删除MySQL的匿名账户

时间:2016-12-06 00:00:08      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:mysql 权限 匿名账户

MySQL权限系统的工作原理

MySQL权限系统通过下面两个阶段进行认证:

1)对连接的用户进行身份认证,合法的用户通过认证,不合法的用户拒绝连接;

2)对通过认证的合法用户赋予相应的权限,用户可以在这些权限范围内对数据库做相应的操作。

对于身份的认证,MySQL是通过IP地址和用户名联合进行确认的,例如MySQL安装后默认创建的用户root@localhost表示用户root只能从本地(localhost)进行连接才可以通过认证,此用户从其他任何主机对数据库进行的连接都将被拒绝。也就是说,同样的一个用户名,如果来自不同的IP地址,则MySQL将其视为不同的用户。

MySQL的权限表在数据库启动的时候就载入内存,当用户通过身份认证后,就在内存中进行相应权限的存取,这样,此用户就可以在数据库中做权限范围内的各种操作了。所以在对用户做了修改操作后flush privileges;后才会生效。

在权限存取的两个过程中,系统会用到“mysql”数据库(安装MySQL时被创建,数据库名称叫“mysql”)中user、host和db这3个最重要的权限表。在这3个表中,最重要的表是user表,其次是db表,host表在大多数情况下并不使用。user中的列主要分为4个部分:用户列、权限列、安全列和资源控制列。

当用户进行连接的时候,权限表的存取过程有以下两个阶段。

  • 先从user表中的host、user和password这3个字段中判断连接的IP、用户名和密码是否存在于表中,如果存在,则通过身份验证,否则拒绝连接。

  • 如果通过身份验证,则按照以下权限表的顺序得到数据库权限:user->db->tables_priv->columns_priv。

在这几个权限表中,权限范围依次递减,全局权限覆盖局部权限。

  •  Host值可以是主机名或IP号,或“localhost”指出本地主机。

  • 可以在Host值使用通配符字符“%和“_

  • Host值“%”匹配任何主机名,空Host值等价于“%”。它们的含义与LIKE操作符的模式匹配操作相同。例如,“%”的Host值与所有主机名匹配,而“%.mysql.com”匹配mysql.com域的所有主机。

如果权限表中的host既有“thomas.loc.gov”,又有“%”,而此时,连接从主机thomas.loc.gov过来。显然,user里面这两条记录都符合匹配条件,那系统会选择哪一个呢?

如果有多个匹配,服务器必须选择使用哪个条目。按照下述原则来解决:

l 服务器在启动时读入user表后进行排序;

l 然后当用户试图连接时,以排序的顺序浏览条目;

l 服务器使用与客户端和用户名匹配的第一行。

当服务器读取表时,它首先以最具体的Host值排序。主机名和IP号是最具体的。“%”意味着“任何主机”并且是最不特定的。有相同Host值的条目首先以最具体的User值排序(空User值意味着“任何用户”并且是最不特定的)。

排序前:
 +-----------+----------+-
| Host      | User    | …
+-----------+----------+-
|%         | root     | …
|%         | jeffrey  | …
|localhost | root     | …
|localhost |          | …
+-----------+----------+-
排序后:
 
+-----------+----------+-
|Host      | User     | …
+-----------+----------+-
|localhost | root     | … ...
|localhost |          | …...
|%         |jeffrey  | … ...
|%         |root     | … ...
+-----------+----------+-


删除匿名用户:

mysql版本5.6.18

查看用户

mysql> select user,host,plugin,password,authentication_string,password_expired from mysql.user;
+------+-----------+-----------------------+-------------------------------------------+-----------------------+------------------+
| user | host      | plugin                | password                                  | authentication_string | password_expired |
+------+-----------+-----------------------+-------------------------------------------+-----------------------+------------------+
| root | localhost |                       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |                       | N                |
| root | rhel7     |                       |                                           |                       | N                |
| root | 127.0.0.1 |                       |                                           |                       | N                |
| root | ::1       |                       |                                           |                       | N                |
|      | localhost |                       |                                           | NULL                  | N                |
|      | rhel7     |                       |                                           | NULL                  | N                |
| zx   | %         | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |                       | N                |
+------+-----------+-----------------------+-------------------------------------------+-----------------------+------------------+

使用不存在的用户也可以登录MySQL

[root@rhel7 mysql5.6.18]# ./bin/mysql -ua
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.6.18-enterprise-commercial-advanced MySQL Enterprise Server - Advanced Edition (Commercial)

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

删除user为空的用户

mysql> delete from mysql.user where user=‘‘;
Query OK, 2 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,plugin,password,authentication_string,password_expired from mysql.user;
+------+-----------+-----------------------+-------------------------------------------+-----------------------+------------------+
| user | host      | plugin                | password                                  | authentication_string | password_expired |
+------+-----------+-----------------------+-------------------------------------------+-----------------------+------------------+
| root | localhost |                       | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |                       | N                |
| root | rhel7     |                       |                                           |                       | N                |
| root | 127.0.0.1 |                       |                                           |                       | N                |
| root | ::1       |                       |                                           |                       | N                |
| zx   | %         | mysql_native_password | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |                       | N                |
+------+-----------+-----------------------+-------------------------------------------+-----------------------+------------------+
5 rows in set (0.00 sec)

使用不存在的用户不能再登录MySQL

[root@rhel7 mysql5.6.18]# ./bin/mysql -ua
ERROR 1045 (28000): Access denied for user ‘a‘@‘localhost‘ (using password: NO)

参考:《深入浅出MySQL》

本文出自 “DBA Fighting!” 博客,请务必保留此出处http://hbxztc.blog.51cto.com/1587495/1879606

MySQL权限及删除MySQL的匿名账户

标签:mysql 权限 匿名账户

原文地址:http://hbxztc.blog.51cto.com/1587495/1879606

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!