MySQL修改存储过程和函数、事件、触发器、视图的DEFINER

MySQL修改存储过程和函数、事件、触发器、视图的DEFINER

新建存储过程、视图、函数等这些功能模块时,用了用户A,由于项目不停的迭代和人员的流动,结果用户A不知道什么时候被删除了,只有用户B,这个时候使用用户B运行之前创建的存储过程等功能,就会出现 DEFINER对应不上的错误。

例如:

ERROR 1142 (42000): SELECT command denied to user 'user1'@'localhost' for table 'user'

查看、修改存储过程和函数的DEFINER

查看:

mysql> select db,name,type,definer from mysql.proc where db='caimei' limit 0,1 \G;
*************************** 1. row ***************************
     db: caimei
   name: addNewUserForDiTui
   type: PROCEDURE
definer: developer@%
1 row in set (0.00 sec)

修改:

mysql> update mysql.proc set definer='user@localhost' where db = 'caimei' and name = 'extract_schema_from_file_name';

查看、修改事件的DEFINER

查看:

mysql> select definer from mysql.event;

修改:

mysql> update mysql.event set definer='user@localhost' name = 'xxxxxx';

查看、修改触发器的DEFINER

没有更方便的方法,只能删除重建,或者手动修改。改前最好锁表,改的过程中如果有其它表改变而触发,会造成数据不一致。

查看:

mysql> select * from information_schema.triggers where TRIGGER_SCHEMA = 'caimei';

修改:

mysql> flush tables with readlock
mysql> drop trigger trigger_name;
mysql> delimiter ;;
>create definer=`user`@`%` trigger trigger_name before 
>delete on table_name for each row 
>begin
>insert ……;
>end;;
>delimiter ;
mysql> unlock tables

查看、修改视图的DEFINER

修改视图有些复杂,不过还是有办法的。

查看:

select * from information_schema.views where table_schema = 'xxxxxx';

修改:

select concat("alter definer=`developer`@`%` sql security definer view ",table_schema,".",table_name," as ",view_definition,";") as 'view' from information_schema.views where definer<>'developer@%' and table_schema = 'xxxxxx'; 

运行后,会得到一堆通过concat拼装好的sql语句,稍作编辑,放在客户端执行即可。

你可能感兴趣的:(Mysql)