这个问题是微信群中网友关于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' 

首先,我们要知道一个事实:information_schema下的视图没法授权给某个用户。如下所示

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

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)
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)
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)

更多相关文章

  1. 类和 Json对象
  2. Android中文API(144) —— JsonWriter
  3. Android之Handler用法总结
  4. android通过ksoap2对webservice的解析
  5. Android(安卓)View的介绍和使用
  6. Android中,把XML文件转换成Object对象的方法
  7. Android中使用Gson解析JSON数据
  8. Android中使用Gson解析JSON数据
  9. Android(安卓)基础UI编程2

随机推荐

  1. Android属性动画上手实现各种动画效果,自
  2. 2013年01月06日
  3. android Activity类的使用
  4. Intellij IDEA 导入 Android(安卓)SDK
  5. Android(安卓)App接入微信开放平台注意事
  6. Android(安卓)ART模式简介
  7. Android中moveTo、lineTo、quadTo、cubic
  8. [置顶] Android事件总线还能怎么玩?
  9. Android账户同步备份机制
  10. Android(安卓)Studio SVN版本控制