關(guān)于MySQL繞過(guò)授予information_schema中對(duì)象時(shí)報(bào)ERROR 1044(4200)錯(cuò)誤
這個(gè)問(wèn)題是微信群中網(wǎng)友關(guān)于MySQL權(quán)限的討論,有這么一個(gè)業(yè)務(wù)需求(下面是他的原話(huà)):
因?yàn)镸ySQL的很多功能都依賴(lài)主鍵,我想用zabbix用戶(hù),來(lái)監(jiān)控業(yè)務(wù)數(shù)據(jù)庫(kù)的所有表,是否都建立了主鍵。
監(jiān)控的語(yǔ)句是:
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用戶(hù),能讀取業(yè)務(wù)庫(kù)的數(shù)據(jù)。一旦不給zabbix用戶(hù)讀取業(yè)務(wù)庫(kù)數(shù)據(jù)的權(quán)限,那么information_schema.TABLES 和 information_schema.TABLE_CONSTRAINTS 就不包含業(yè)務(wù)庫(kù)的表信息了,也就統(tǒng)計(jì)不出來(lái)業(yè)務(wù)庫(kù)的表是否有建主鍵。有沒(méi)有什么辦法,即讓zabbix不能讀取業(yè)務(wù)庫(kù)數(shù)據(jù),又能監(jiān)控是否業(yè)務(wù)庫(kù)的表沒(méi)有建立主鍵?
首先,我們要知道一個(gè)事實(shí):information_schema下的視圖沒(méi)法授權(quán)給某個(gè)用戶(hù)。如下所示
mysql> GRANT SELECT ON information_schema.TABLES TO test@'%'; ERROR 1044 (42000): Access denied for user 'root'@'localhost' to database 'information_schema'
關(guān)于這個(gè)問(wèn)題,可以參考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.
那么怎么解決這個(gè)授權(quán)問(wèn)題呢? 直接授權(quán)不行,那么我們只能繞過(guò)這個(gè)問(wèn)題,間接實(shí)現(xiàn)授權(quán)。思路如下:首先創(chuàng)建一個(gè)存儲(chǔ)過(guò)程(用戶(hù)數(shù)據(jù)庫(kù)),此存儲(chǔ)過(guò)程找出沒(méi)有主鍵的表的數(shù)量,然后將其授予test用戶(hù)。
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', 'myawr', 'mysql', 'performance_schema', 'slowlog', 'sys', '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)
此時(shí)test就能間接的去查詢(xún)information_schema下的對(duì)象了。
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用戶(hù)的權(quán)限。
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)
到此這篇關(guān)于關(guān)于MySQL繞過(guò)授予information_schema中對(duì)象時(shí)報(bào)ERROR 1044(4200)錯(cuò)誤的文章就介紹到這了,更多相關(guān)mysql ERROR 1044(4200)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Navicat Premium 連接 MySQL 8.0 報(bào)錯(cuò)"1251"的問(wèn)題分析
這篇文章主要介紹了解決Navicat Premium 連接 MySQL 8.0 報(bào)錯(cuò)"1251"的問(wèn)題分析,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11MySQL密碼策略管理插件validate_password用法詳解
自MySQL5.6起,引入validate_password插件,用于密碼長(zhǎng)度和強(qiáng)度管理,在MySQL8.0中,該插件通過(guò)服務(wù)器組件重新實(shí)現(xiàn),插件默認(rèn)不允許密碼為用戶(hù)名,可設(shè)定最小長(zhǎng)度和強(qiáng)度等級(jí),還可要求密碼包含數(shù)字、大小寫(xiě)字母和特殊字符2024-11-11(MariaDB)MySQL數(shù)據(jù)類(lèi)型和存儲(chǔ)機(jī)制全面講解
下面小編就為大家分享一篇(MariaDB)MySQL數(shù)據(jù)類(lèi)型和存儲(chǔ)機(jī)制全面講解,具有很的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01MySQL中SELECT+UPDATE處理并發(fā)更新問(wèn)題解決方案分享
這篇文章主要介紹了MySQL中SELECT+UPDATE處理并發(fā)更新問(wèn)題解決方案分享,需要的朋友可以參考下2014-05-05mysql如何在線(xiàn)修改主從復(fù)制選項(xiàng)
這篇文章主要介紹了mysql如何在線(xiàn)修改主從復(fù)制選項(xiàng),幫助大家更好的理解和學(xué)習(xí)mysql,感興趣的朋友可以了解下2020-08-08mysql數(shù)據(jù)庫(kù)查詢(xún)優(yōu)化 mysql效率
MySQL由于它本身的小巧和操作的高效, 在數(shù)據(jù)庫(kù)應(yīng)用中越來(lái)越多的被采用.我在開(kāi)發(fā)一個(gè)P2P應(yīng)用的時(shí)候曾經(jīng)使用MySQL來(lái)保存P2P節(jié)點(diǎn),由于P2P的應(yīng)用中,結(jié)點(diǎn)數(shù)動(dòng)輒上萬(wàn)個(gè),而且節(jié)點(diǎn)變化頻繁,因此一定要保持查詢(xún)和插入的高效.以下是我在使用過(guò)程中做的提高效率的三個(gè)有效的嘗試. 1. 使用statement進(jìn)行綁定查詢(xún) 2. 隨機(jī)的獲取記錄 3. 使用連接池管理連接.2008-01-01一文弄懂MySQL中redo?log與binlog的區(qū)別
在學(xué)習(xí)mysql數(shù)據(jù)庫(kù)時(shí),不可避免要去接觸到redo log和binlog,好多人對(duì)這兩者的概念分不太清,下面這篇文章主要給大家介紹了關(guān)于MySQL中redo?log與binlog區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-02-02MySQL實(shí)時(shí)監(jiān)控工具orztop的使用介紹
這篇文章主要給大家介紹了MySQL實(shí)時(shí)監(jiān)控工具orztop的使用,文中給出了詳細(xì)的介紹,相信對(duì)大家的學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-01-01mysql中update按照多重條件進(jìn)行更新處理的方案
更新數(shù)據(jù)是使用數(shù)據(jù)庫(kù)時(shí)最重要的任務(wù)之一,下面這篇文章主要給大家介紹了關(guān)于mysql中update按照多重條件進(jìn)行更新處理的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09