筆者前幾日在作數據庫遷移的時候,發現了一個挺有意思的小東西:數據庫訪問限制(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,上一張咱們平時用的最多的可視化界面的圖: 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.」
同時,你也能夠爲這個用戶設置例如一小時內最大鏈接次數的之類的基本屬性:
在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的初步瞭解就先記錄到這!