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

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

MySQL 官方文献传送门 戳👇mysql

MySQL 5.7 : Access Control Stage 1 Connection-Accesssql

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.this

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

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

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

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

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

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的初步了解就先记录到这!

相关文章
相关标签/搜索