关于MySQL绕过授予information_schema中对象时报ERROR 1044(4200)错误
作者:潇湘隐者 发布时间:2024-01-24 07:49:27
这个问题是微信群中网友关于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', '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)
此时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)
来源:https://www.cnblogs.com/kerrycode/archive/2020/10/16/13826210.html


猜你喜欢
- 本文实例讲述了Python装饰器用法。分享给大家供大家参考,具体如下:一、装饰器是什么python的装饰器本质上是一个Python函数,它可
- 书 名:细节决定交互设计的成败国际书号:ISBN 978-7-121-08232-0作 &nb
- 如下所示:# -*- coding: utf-8 -*-import threadingimport threadimport timecl
- DataFrame 是一个表格型的数据结构,它含有一组有序的列,每列可以是不同的值类型(数值、字符串、布尔型值)。DataFrame 既有行
- 当我们用javascript写ajax程序写得很“开心”的时候,突然有人告诉你有一种东西叫jquery,它会告诉你不直接和HttpReque
- 以Python 3.x版本为主一、条件语句条件语句基本结构0或null为false,其余则为trueif 判定条件:语句块...可多行els
- 先上效果,视频敬上:字符舞:代码舞源代码:video_2_code_video.pyimport argparseimport osimpo
- 关于用鼠标滚动到某个位置我们就去加载数据,这样的场景与业务需求现在越来越常见,现在来分析下《vue.js 实战》中作者的一个解决策略:1.
- python的三种输出格式环境:pycharm + python3.81. % (不推荐使用)格式: 格式字符串% (输出项1,输出项2,&
- Python 相对路径报错:"No such file or directory"'原因及解决方法如果你取相对路
- 本文实例讲述了windows下Python实现将pdf文件转化为png格式图片的方法。分享给大家供大家参考,具体如下:最近工作中需要把pdf
- 现像如下:站点无法打开,或者打开很慢.HTML可以打开.重新启动或者回收应用程序池可恢复.但过一段时间又会出现日志里会有:ISAPI
- 浏览网页的时候经常会碰到一些不认识的英文单词,或者想知道一些中文单词的翻译,这时候再去找翻译软件或者翻译网站就有些麻烦了。因此我做了一个“中
- Centos7的yum源中默认是没有mysql,因为现在已经用mariaDB代替mysql了。首先我们下载mysql的repo源,我们可以去
- 今天我们来使用Python实现递归算法求指定位数的斐波那契数列首先我们得知道斐波那契数列是什么?斐波那契数列又叫兔子数列斐波那契数列就是一个
- Vue 3.2 引入了语法,这是一种稍微不那么冗长的声明组件的方式。您可以通过向 SFC 的元素添加属性来启用它,然后可以删除组件中的一些样
- 磁盘突然报错使用率过大,排查原因,发现mysql的binlog文件占用过大命令ls -l -hmysql-binlog是MySQL数据库的二
- 说说最近的一个案例吧,线上阿里云RDS上的一个游戏日志库最近出现了一点问题,随着游戏人数的增加,在线日
- 1. MyISAM底层存储(非聚集索引方式)与InnoDB底层存储(聚集索引方式)1.1 MyISAM底层存储(非聚集索引方式)Myisam
- 将有安全问题的SQL过程删除.比较全面.一切为了安全!删除了调用shell,注册表,COM组件的破坏权限MS SQL SERVER2000使