浅谈Mysql连接数据库时host和user的匹配规则

 更新时间:2021年01月05日 14:54:44   作者:翔之天空  
这篇文章主要介绍了浅谈Mysql连接数据库时host和user的匹配规则,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

--连接数据库时,host和user的匹配规则

官方文档:https://dev.mysql.com/doc/refman/5.7/en/connection-access.html

--host和user的匹配规则如下:

--是host为明确的最先匹配,host带%模糊的时候最后匹配,但host为''(空)位于%之后才匹配

--相同的host时候,比较user为明确的最先匹配,user为''(空)最后匹配

--相同的host和user时,排序是不确定的

When multiple matches are possible, the server must determine which of them to use. It resolves this issue as follows: 
Whenever the server reads the user table into memory, it sorts the rows. 
When a client attempts to connect, the server looks through the rows in sorted order. 
The server uses the first row that matches the client host name and user name. 
The server uses sorting rules that order rows with the most-specific Host values first. Literal host names and IP addresses are the most specific. (The specificity of a literal IP address is not affected by whether it has a netmask, so 198.51.100.13 and 198.51.100.0/255.255.255.0 are considered equally specific.) The pattern '%' means “any host” and is least specific. The empty string '' also means “any host” but sorts after '%'. Rows with the same Host value are ordered with the most-specific User values first (a blank User value means “any user” and is least specific). For rows with equally-specific Host and User values, the order is nondeterministic.

--查看当前的host及用户信息匹配顺序,先host顺序匹配、后user顺序匹配

mysql> SELECT authentication_string, host, user,account_locked FROM mysql.USER ORDER BY host desc ,user desc;
+-------------------------------------------+--------------+---------------+----------------+
| authentication_string      | host   | user   | account_locked |
+-------------------------------------------+--------------+---------------+----------------+
| *511C0A408C5065XXEC90D60YYA1AB9437281AF28 | localhost | root   | N    |
| *THISISNOTAVALIXXASSWORDYYATCANBEUSEDHERE | localhost | mysql.sys  | Y    |
| *THISISNOTAVALIXXASSWORDYYATCANBEUSEDHERE | localhost | mysql.session | Y    |
| *485CE31BA547A4XXC047659YY10DF200F361CD4E | localhost | bkpuser  | N    |
| *7B502777D8FF69XX4B56BC2YY2867F4B47321BA8 | 192.168.56.% | repl   | N    |
| *AECCE73463829AXX3968838YYF6F85E43C3F169C | %   | flyremote  | N    |
| *566AC8467DAAAEXXE247AE7YY0A770E9B97D9FB0 |    | flylocal  | N    |
+-------------------------------------------+--------------+---------------+----------------+
8 rows in set (0.00 sec)
 

--举个特殊例子

--建立两个特殊用户如下,一个用户名为''(空)、一个用户名和host都为''(空)

mysql> create user ''@'localhost' identified by "Kong123$";
Query OK, 0 rows affected (0.00 sec) 
mysql> create user ''@'' identified by "doubleKong123$";   
Query OK, 0 rows affected (0.00 sec)

--查看当前的host及用户信息匹配顺序,先host顺序匹配、后user顺序匹配

mysql> SELECT authentication_string, host, user,account_locked FROM mysql.USER ORDER BY host desc ,user desc;
+-------------------------------------------+--------------+---------------+----------------+
| authentication_string      | host   | user   | account_locked |
+-------------------------------------------+--------------+---------------+----------------+
| *511C0VVV8C5065CBEC90D6TTTT1AB9437281AF28 | localhost | root   | N    |
| *THISIVVVTAVALIDPASSWORTTTTTCANBEUSEDHERE | localhost | mysql.sys  | Y    |
| *THISIVVVTAVALIDPASSWORTTTTTCANBEUSEDHERE | localhost | mysql.session | Y    |
| *485CEVVVA547A48CC04765TTTT0DF200F361CD4E | localhost | bkpuser  | N    |
| *256D7VVV91F7363EBDADEFTTTTB74B2B318746FC | localhost |    | N    |
| *7B502VVVD8FF69164B56BCTTTT867F4B47321BA8 | 192.168.56.% | repl   | N    |
| *AECCEVVV63829A5F396883TTTT6F85E43C3F169C | %   | flyremote  | N    |
| *566ACVVV7DAAAE79E247AETTTTA770E9B97D9FB0 |    | flylocal  | N    |
| *AE162VVV68403D1D98A4C9TTTT50A508B8C56F3F |    |    | N    |
+-------------------------------------------+--------------+---------------+----------------+
9 rows in set (0.00 sec)

--这样本地登录flyremote用户时 会报错,因为按以上的顺序 优先匹配到了host为localhost、user为''(空)的用户,而不是flyremote用户 (因为user为''(空)的用户可以匹配任意用户名)

[root@hostmysql-m mysql]# mysql -uflyremote -pFlyremote123$
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'flyremote'@'localhost' (using password: YES)

--那就是说本地登录flyremote用户时, 用匹配到的host为localhost、user为''(空)的密码 Kong123$ ,就可以正常登陆了

[root@hostmysql-m mysql]# mysql -uflyremote -pKong123$
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.23-log MySQL Community Server (GPL) 
Copyright (c) 2000, 2018, 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() |
+---------------------+----------------+
| flyremote@localhost | @localhost  |
+---------------------+----------------+
1 row in set (0.06 sec)

--用带入ip的方式登录flyremote用户时 无问题, ip匹配到了% ,user匹配到了flyremote

[root@hostmysql-m mysql]# mysql -uflyremote -pFlyremote123$ -h127.11.22.33 
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.23-log MySQL Community Server (GPL) 
Copyright (c) 2000, 2018, 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> select user(),CURRENT_USER();
+------------------------+----------------+
| user()     | CURRENT_USER() |
+------------------------+----------------+
| flyremote@127.11.22.33 | flyremote@% |
+------------------------+----------------+
1 row in set (0.00 sec)

--任意用户、任意host,只要密码和建立的第二个空用户空host的密码"doubleKong123$"匹配了, 就可以进入mysql

--测试一个不存在的用户hahaha

[root@hostmysql-m ~]# mysql -uhahaha -pdoubleKong123$ -h127.11.22.33
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.23-log MySQL Community Server (GPL) 
Copyright (c) 2000, 2018, 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> select user(),CURRENT_USER();
+---------------------+----------------+
| user()    | CURRENT_USER() |
+---------------------+----------------+
| hahaha@127.11.22.33 | @    |
+---------------------+----------------+
1 row in set (0.01 sec)

--解决方案:

1、手工删除空用户和空host用户确保安全

或者

2、使用 mysql_secure_installation 来进行安全配置

--安全配置如下,其中有删除匿名用户的操作

This program enables you to improve the security of your MySQL installation in the following ways:
 You can set a password for root accounts.
 You can remove root accounts that are accessible from outside the local host.
 You can remove anonymous-user accounts.
 You can remove the test database (which by default can be accessed by all users, even anonymous users), and privileges that permit anyone to access databases with names that start with test_.

--删除匿名用户的源码 mysql_secure_installation.cc 如下:

 //Remove anonymous users
 remove_anonymous_users(); 
/**
 Removes all the anonymous users for better security.
*/
void remove_anonymous_users()
{
 int reply;
 reply= get_response((const char *) "By default, a MySQL installation has an "
      "anonymous user,\nallowing anyone to log "
      "into MySQL without having to have\na user "
      "account created for them. This is intended "
      "only for\ntesting, and to make the "
      "installation go a bit smoother.\nYou should "
      "remove them before moving into a production\n"
      "environment.\n\nRemove anonymous users? "
      "(Press y|Y for Yes, any other key for No) : ", 'y');
 
 if (reply == (int) 'y' || reply == (int) 'Y')
 {
 const char *query;
 query= "SELECT USER, HOST FROM mysql.user WHERE USER=''";
 if (!execute_query(&query, strlen(query)))
  DBUG_PRINT("info", ("query success!"));
 MYSQL_RES *result= mysql_store_result(&mysql);
 if (result)
  drop_users(result);
 mysql_free_result(result);
 fprintf(stdout, "Success.\n\n");
 }
 else
 fprintf(stdout, "\n ... skipping.\n\n");
}

补充:mysql 用户表中多个host时的匹配规则

mysql数据库中user表的host字段,是用来控制用户访问数据库“权限”的。

可以使用“%”,表示所有的网段;

也可以使用具体的ip地址,表示只有该ip的客户端才可以登录到mysql服务器;

也可以使用“_”进行模糊匹配,表示某个网段的客户端可以登录到mysql服务器。

如果在user表中存在一个用户两条不同host值的记录,那么mysql服务器该如何匹配该用户的权限呢?

mysql采用的策略是:当服务器读取user表时,它首先以最具体的Host值排序(主机名和IP号是最具体的) 。有相同Host值的条目首先以最具体的User匹配。

举例:

如下,有两条root用户,那么只有localhost的root客户端可以登录到mysql服务器。

| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | %   | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

相关文章

  • mysql表分区的方式和实现代码示例

    mysql表分区的方式和实现代码示例

    通俗地讲表分区是将一个大表,根据条件分割成若干个小表,下面这篇文章主要给大家介绍了关于mysql表分区的方式和实现代码,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • MySQL修改密码方法汇总

    MySQL修改密码方法汇总

    本文中小编给大家汇总介绍了MySQL修改密码的方法,分为MySQL5.7版本之前以及MySQL5.7版本之后的修改方法,有需要的小伙伴可以参考下
    2018-08-08
  • MySQL数据库索引及优化的示例详解

    MySQL数据库索引及优化的示例详解

    在日常的数据库使用过程中,我们经常需要对数据进行查询、插入、删除等操作,为了提高这些操作的效率,数据库的性能优化显得尤为重要,本文就来讲讲MySQL中是如何优化索引的吧
    2023-05-05
  • 使用MySQL Workbench构建ER图的详细教程

    使用MySQL Workbench构建ER图的详细教程

    ER图又称实体-联系图(Entity Relationship Diagram),提供了表示实体类型、属性和联系的方法,用来描述现实世界的概念模型,MySQL Workbench是一个强大的数据库设计工具,提供了便捷的数据导入导出功能,本文介绍了使用MySQL Workbench构建ER图的详细教程
    2024-06-06
  • MySQL数据库的一次死锁实例分析

    MySQL数据库的一次死锁实例分析

    本文主要给大家通过一个实例来具体介绍MySQL死锁问题的相关知识,接下来我们就来一一介绍这部分内容,希望能够对您有所帮助。
    2016-11-11
  • MySQL版oracle下scott用户建表语句实例

    MySQL版oracle下scott用户建表语句实例

    这篇文章主要给大家介绍了关于MySQL版oracle下scott用户建表语句的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • MySQL 5.7.43下载安装配置的超详细教程

    MySQL 5.7.43下载安装配置的超详细教程

    这篇文章主要介绍了MySQL 5.7.43下载安装配置的超详细教程,本文通过实例图文结合的形式给大家介绍的非常详细,对大家的学习或工作具有一定的帮助,需要的朋友可以参考下
    2023-09-09
  • MySQL会发生死锁的几种情况及处理方法

    MySQL会发生死锁的几种情况及处理方法

    数据库的死锁是指不同的事务在获取资源时相互等待,导致无法继续执行的一种情况,当发生死锁时,数据库系统会自动中断其中一个事务,以解除死锁,本文给大家介绍了MySQL什么情况下会死锁,发生了死锁怎么处理呢,需要的朋友可以参考下
    2023-09-09
  • Mysql中批量替换某个字段的部分数据(推荐)

    Mysql中批量替换某个字段的部分数据(推荐)

    这篇文章主要介绍了Mysql中批量替换某个字段的部分数据,通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02
  • MySQL limit子句用法及优化小结

    MySQL limit子句用法及优化小结

    limit在获取到满足条件的数据量时即会立刻终止SQL的执行,本文主要介绍了MySQL limit子句用法及优化小结,具有一定的参考价值,感兴趣的可以了解一下
    2024-09-09

最新评论