博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

mysql导出存储过程、函数、视图、触发器

Posted on 2015-09-11 09:26  DBA日志小记  阅读(3931)  评论(0编辑  收藏  举报
由于通过mysqldump导出的存储过程、函数、视图、触发器包含definer信息,导致很多时候无法正常导入。通过以下SQL可以自动生成创建语句。

--导出存储过程和函数
SELECT CONCAT("DROP ",TYPE," IF EXISTS `",db,"`.`", NAME,"`;\nDELIMITER ;;\nCREATE ",TYPE," `",db,"`.`",NAME,"`(", param_list,") ",IF ( TYPE = "FUNCTION", CONCAT ("RETURNS ", RETURNS, "\n"),"\n"), body_utf8,";;\nDELIMITER ;")  FROM   mysql.proc;

--导出视图
SELECT CONCAT("DROP VIEW IF EXISTS `",TABLE_SCHEMA,"`.`",TABLE_NAME,"`;\nCREATE VIEW `", TABLE_SCHEMA,"`.`",TABLE_NAME,"` as ",VIEW_DEFINITION,";")  FROM   information_schema.VIEWS;

--导出触发器
SELECT CONCAT("DROP TRIGGER IF EXISTS `",TRIGGER_SCHEMA, "`.`", TRIGGER_NAME,"`;\nDELIMITER ;;\nCREATE TRIGGER `",TRIGGER_SCHEMA,"`.`",TRIGGER_NAME,"` ",ACTION_TIMING," ",EVENT_MANIPULATION," ON `",EVENT_OBJECT_SCHEMA,"`.`",EVENT_OBJECT_TABLE,"` FOR EACH ROW\n",ACTION_STATEMENT,";;\nDELIMITER ;")  FROM information_schema.TRIGGERS;

--导出事件
SELECT    CONCAT(     "DROP EVENT IF EXISTS `",     EVENT_SCHEMA,     "`.`",     EVENT_NAME,     "`;\nDELIMITER ;;\nCREATE EVENT `",     EVENT_SCHEMA,     "`.`",     EVENT_NAME,     "` ON SCHEDULE EVERY ",     INTERVAL_VALUE,     " ",     INTERVAL_FIELD,     " STARTS '",     STARTS,"'",     IF ( ENDS <>NULL, CONCAT (" ENDS '",ENDS,"'"),""),         " ON COMPLETION ",     ON_COMPLETION,     " ENABLE DO ",     EVENT_DEFINITION,     ";;\nDELIMITER ;"   )  FROM   information_schema.events; 

或者(针对版本mysql.5.6.14)
mysqldump -h127.0.0.1 -uroot  -proot@123 -P5320 -B -R -E --triggers --default-character-set=utf8 --opt --max-allowed-packet=64M --net_buffer_length=163840  --single-transaction  ad |sed '/DEFINER.*SQL SECURITY DEFINER/d;s/^CREATE DEFINER.*PROCEDURE /CREATE PROCEDURE /;s/^CREATE DEFINER.*FUNCTION /CREATE FUNCTION /;s/^.*CREATE.*DEFINER.*trigger /CREATE \/\*\!50003 trigger /;s/^.*CREATE.*DEFINER.*EVENT /CREATE \/\*\!50106 EVENT /;' > ad.sql

版权声明:QQ:597507041