MySQL 用户访问限制 -- Host Match Limit

  笔者前几日在做数据库迁移的时候,发现了一个挺有意思的小东西:数据库访问限制(Host Match limit),简单地翻阅了下给官方资料,发现这个东西应用场景其实非常广泛,只是我们采用了其他可能没有原生数据库带的Access Limit 功能好地方式,特此摘记!

MySQL 官方文献传送门 戳?

MySQL 5.7 : Access Control Stage 1 Connection-Access

MySQL 5.7 : Access Control Stage 2 Request-Access

 The server checks credentials first, then account locking state. A failure for either step causes the server to deny access to you completely. Otherwise, the server accepts the connection, and then enters Stage 2 and waits for requests.

 上面那端话摘自文献第一端,大意就是MySQL校验访问者身份会先从访问者的有效认证(credentials)开始,这个有效认证实际上值得就是我们平时赋值一个新的Access User的登入名、密码以及访问时所用的主机名。

 OK,上一张我们平时用的最多的可视化界面的图: MySQL Workbench 用户权限

 图中标识红色的部分也就是我们常用来设置某用户访问权限的地方,其中笔者设置的是localhost,即笔者这个Root用户访问的时候,MySQL会除了将 MySQL.User表里的Username、Password与输入的进行比较外,还会将在表中的Hostname与访问的Hostname进行比较,倘若这三者中又一步失败了,则MySQL都会拒绝访问(Deny access !)

 好,那么问题来了,如果我是想把这个用户做成开放性接口使用的用户,那么我该怎么办?其实细心点观察图上的文字提示你就会发现,实际上想开放访问的Hostname,你只需要将红色标识的输入框内的localhost(或者其它)改成 ''(空字符串)或是%即可。因为上述两种符号均表示为此用户登入不做Hostname约束的意思!

摘一段关于赋予用户权限的官方陈述:

The db table grants database-specific privileges. Values in the scope columns of this table can take the following forms:

1. A blank User value matches the anonymous user. A nonblank value matches literally; there are no wildcards in user names.

2. The wildcard characters % and _ can be used in the Host and Db columns. These have the same meaning as for pattern-matching operations performed with the LIKE operator. If you want to use either character literally when granting privileges, you must escape it with a backslash. For example, to include the underscore character (_) as part of a database name, specify it as \_ in the GRANT statement.

3. A '%' or blank Host value means “any host.”

4. A '%' or blank Db value means “any database.”

 同时,你也可以为这个用户设置例如一小时内最大连接次数的之类的基本属性:

Account Limits

 在MySQL.User表中,遇到同名的用户,System会采用第一个匹配成功的用户作为首选项(即它的权限和相关信息都是采用第一个用户了)。

PS : 附上查找User表里用户的常用信息的语句

mysql> select user,host,authentication_string from mysql.user;

以及赋值用户的官方传送门:Grant Table 以及基本赋值命令

GRANT ALL ON db1.* TO 'jeffrey'@'localhost';
GRANT SELECT ON db2.invoice TO 'jeffrey'@'localhost';

OK,关于Access Control的初步了解就先记录到这!

转载于:https://my.oschina.net/u/3744313/blog/1790150

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值