1. 程式人生 > 資料庫 >關於MySQL繞過授予information_schema中物件時報ERROR 1044(4200)錯誤

關於MySQL繞過授予information_schema中物件時報ERROR 1044(4200)錯誤

這個問題是微信群中網友關於MySQL許可權的討論,有這麼一個業務需求(下面是他的原話):

因為MySQL的很多功能都依賴主鍵,我想用zabbix使用者,來監控業務資料庫的所有表,是否都建立了主鍵。

監控的語句是:

FROM  information_schema.tables t1 
    LEFT OUTER JOIN information_schema.table_constraints t2 
          ON t1.table_schema = t2.table_schema 
            AND t1.table_name = t2.table_name 
            AND t2.constraint_name IN ( 'PRIMARY' ) 
WHERE t2.table_name IS NULL 
    AND t1.table_schema NOT IN ( 'information_schema','myawr','mysql','performance_schema','slowlog','sys','test' ) 
    AND t1.table_type = 'BASE TABLE' 

但是我不希望zabbix使用者,能讀取業務庫的資料。一旦不給zabbix使用者讀取業務庫資料的許可權,那麼information_schema.TABLES 和 information_schema.TABLE_CONSTRAINTS 就不包含業務庫的表資訊了,也就統計不出來業務庫的表是否有建主鍵。有沒有什麼辦法,即讓zabbix不能讀取業務庫資料,又能監控是否業務庫的表沒有建立主鍵

首先,我們要知道一個事實:information_schema下的檢視沒法授權給某個使用者。如下所示

mysql> GRANT SELECT ON information_schema.TABLES TO test@'%';
ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'

關於這個問題,可以參考mos上這篇文章:Why Setting Privileges on INFORMATION_SCHEMA does not Work (文件 ID 1941558.1)

APPLIES TO:

MySQL Server - Version 5.6 and later

Information in this document applies to any platform.

GOAL

To determine how MySQL privileges work for INFORMATION_SCHEMA.

SOLUTION

A simple GRANT statement would be something like:

mysql> grant select,execute on information_schema.* to 'dbadm'@'localhost';

ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'

The error indicates that the super user does not have the privileges to change the information_schema access privileges.

Which seems to go against what is normally the case for the root account which has SUPER privileges.

The reason for this error is that the information_schema database is actually a virtual database that is built when the service is started.

It is made up of tables and views designed to keep track of the server meta-data,that is,details of all the tables,procedures etc. in the database server.

So looking specifically at the above command,there is an attempt to add SELECT and EXECUTE privileges to this specialised database.

The SELECT option is not required however,because all users have the ability to read the tables in the information_schema database,so this is redundant.

The EXECUTE option does not make sense,because you are not allowed to create procedures in this special database.

There is also no capability to modify the tables in terms of INSERT,UPDATE,DELETE etc.,so privileges are hard coded instead of managed per user.

那麼怎麼解決這個授權問題呢? 直接授權不行,那麼我們只能繞過這個問題,間接實現授權。思路如下:首先建立一個儲存過程(使用者資料庫),此儲存過程找出沒有主鍵的表的數量,然後將其授予test使用者。

DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `moitor_without_primarykey`()
BEGIN
   SELECT COUNT(*) 
FROM  information_schema.tables t1 
    LEFT OUTER JOIN information_schema.table_constraints t2 
          ON t1.table_schema = t2.table_schema 
            AND t1.table_name = t2.table_name 
            AND t2.constraint_name IN ( 'PRIMARY' ) 
WHERE t2.table_name IS NULL 
    AND t1.table_schema NOT IN ( 'information_schema','test' ) 
    AND t1.table_type = 'BASE TABLE';
END //
DELIMITER ;
 
 
mysql> GRANT EXECUTE ON PROCEDURE moitor_without_primarykey TO 'test'@'%';
Query OK,0 rows affected (0.02 sec)

此時test就能間接的去查詢information_schema下的物件了。

mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| test@%     |
+----------------+
1 row in set (0.00 sec)
 
mysql> call moitor_without_primarykey;
+----------+
| COUNT(*) |
+----------+
|    6 |
+----------+
1 row in set (0.02 sec)
 
Query OK,0 rows affected (0.02 sec)

檢視test使用者的許可權。

mysql> show grants for test@'%';
+-------------------------------------------------------------------------------+
| Grants for test@%                               |
+-------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `test`@`%`                       |
| GRANT EXECUTE ON PROCEDURE `zabbix`.`moitor_without_primarykey` TO `test`@`%` |
+-------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

到此這篇關於關於MySQL繞過授予information_schema中物件時報ERROR 1044(4200)錯誤的文章就介紹到這了,更多相關mysql ERROR 1044(4200)內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!