电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

淺談Mysql連接數據庫時host和user的匹配規則

瀏覽:15日期:2023-10-07 08:43:07

--連接數據庫時,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 15Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners. 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 12Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners. 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.33mysql: [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 6Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners. 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 havena user ' 'account created for them. This is intended ' 'only forntesting, and to make the ' 'installation go a bit smoother.nYou should ' 'remove them before moving into a productionn' 'environment.nnRemove 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.nn'); } else fprintf(stdout, 'n ... skipping.nn');}

補充: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 數據庫
相關文章:
主站蜘蛛池模板: 铝镁锰板厂家_进口钛锌板_铝镁锰波浪板_铝镁锰墙面板_铝镁锰屋面-杭州军晟金属建筑材料 | 充气膜专家-气膜馆-PTFE膜结构-ETFE膜结构-商业街膜结构-奥克金鼎 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 盐水蒸发器,水洗盐设备,冷凝结晶切片机,转鼓切片机,絮凝剂加药系统-无锡瑞司恩机械有限公司 | 门禁卡_智能IC卡_滴胶卡制作_硅胶腕带-卡立方rfid定制厂家 | 综合管廊模具_生态,阶梯护坡模具_检查井模具制造-致宏模具厂家 | 紫外线老化试验箱_uv紫外线老化试验箱价格|型号|厂家-正航仪器设备 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 领先的大模型技术与应用公司-中关村科金 | 光泽度计_测量显微镜_苏州压力仪_苏州扭力板手维修-苏州日升精密仪器有限公司 | 日本SMC气缸接头-速度控制阀-日本三菱伺服电机-苏州禾力自动化科技有限公司 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 长沙发电机-湖南发电机-柴油发电机供应厂家-长沙明邦智能科技 | 撕碎机_轮胎破碎机_粉碎机_回收生产线厂家_东莞华达机械有限公司 | 咖啡加盟-咖啡店加盟-咖啡西餐厅加盟-塞纳左岸咖啡西餐厅官网 | 德国UST优斯特氢气检漏仪-德国舒赐乙烷检测仪-北京泽钏 | 深圳富泰鑫五金_五金冲压件加工_五金配件加工_精密零件加工厂 | 植筋胶-粘钢胶-碳纤维布-碳纤维板-环氧砂浆-加固材料生产厂家-上海巧力建筑科技有限公司 | 塑料造粒机「厂家直销」-莱州鑫瑞迪机械有限公司| 深圳货架厂家_金丽声精品货架_广东金丽声展示设备有限公司官网 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 自动记录数据电子台秤,记忆储存重量电子桌称,设定时间记录电子秤-昆山巨天 | 胀套-锁紧盘-风电锁紧盘-蛇形联轴器「厂家」-瑞安市宝德隆机械配件有限公司 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 北京晚会活动策划|北京节目录制后期剪辑|北京演播厅出租租赁-北京龙视星光文化传媒有限公司 | 安全光栅|射频导纳物位开关|音叉料位计|雷达液位计|两级跑偏开关|双向拉绳开关-山东卓信机械有限公司 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 交联度测试仪-湿漏电流测试仪-双85恒温恒湿试验箱-常州市科迈实验仪器有限公司 | 干洗店加盟_洗衣店加盟_干洗店设备-伊蔻干洗「武汉总部」 | 电机保护器-电动机综合保护器-上海硕吉电器有限公司 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 | 武汉创亿电气设备有限公司_电力检测设备生产厂家 | 柔性输送线|柔性链板|齿形链-上海赫勒输送设备有限公司首页[输送机] | 5L旋转蒸发器-20L-50L旋转蒸发器-上海越众仪器设备有限公司 | 玻璃钢格栅盖板|玻璃钢盖板|玻璃钢格栅板|树篦子-长沙川皖玻璃钢制品有限公司 | 密度电子天平-内校-外校电子天平-沈阳龙腾电子有限公司 | 筒瓦厂家-仿古瓦-寺庙-古建琉璃瓦-宜兴市古典园林建筑陶瓷厂有限公司 |