龙空技术网

一键导出mysql视图、函数、存储过程、事件、触发器的定义脚本

波波说运维 827

前言:

此刻小伙伴们对“mysql视图备份”大致比较关注,咱们都想要分析一些“mysql视图备份”的相关内容。那么小编也在网摘上收集了一些有关“mysql视图备份””的相关知识,希望看官们能喜欢,各位老铁们一起来学习一下吧!

概述

写这个脚本是因为如果用navicat迁移视图,可以发现修改视图内容很不方便,所以导出其中定义来改,如果用navicat是无法实现的。

所以就顺便整了一键导出视图、函数、存储过程、事件、触发器的定义脚本,下面一起来看看吧~

一键导出脚本

脚本明细如下:

#!/bin/bash# ./output_db_object_definition.sh > /dev/null 2>&1db_user="root"db_pwd="xxxxb"db_host="172.26.151.88"db_port=3306db_name="ycapp_prod" save_file="/home/scripts/${db_name}_object_definition.sql"# view,function,procedure,event,triggeroutput_type='view,function,procedure,event,trigger'  (cat <<out/*ouput object‘s definition for database "$db_name"ouput time: $(date "+%Y-%m-%d %H:%M:%S")ouput object type: $output_type*/out)>$save_fileecho "">> $save_fileecho "">> $save_file # 视图if [[ $output_type == *"view"* ]]then	echo "-- ------------------------------------------------------------" >> $save_file	echo "-- views" >> $save_file	echo "-- ------------------------------------------------------------" >> $save_file	mysql -h$db_host -P$db_port -u$db_user -p$db_pwd --skip-column-names \	-e "select concat('SHOW CREATE VIEW ',table_schema,'.',table_name,';') from information_schema.views where table_schema='$db_name'" |\	sed 's/;/\\G/g' | mysql -h$db_host -P$db_port -u$db_user -p$db_pwd $db_name |\	sed 's/Create View: /kk_begin\n/g' | sed 's/[ ]*character_set_client:/;\nkk_end/g' |\	sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin\|kk_end.*//g;p}'  >> $save_filefi # 函数if [[ $output_type == *"function"* ]]then	echo "-- ------------------------------------------------------------" >> $save_file	echo "-- function" >> $save_file	echo "-- ------------------------------------------------------------" >> $save_file	mysql -h$db_host -P$db_port -u$db_user -p$db_pwd --skip-column-names \	-e "select concat('SHOW CREATE FUNCTION ',routine_schema,'.',routine_name,';') from information_schema.routines where routine_schema='$db_name' and ROUTINE_TYPE='FUNCTION'" |\	sed 's/;/\\G/g' | mysql -h$db_host -P$db_port -u$db_user -p$db_pwd $db_name |\	sed 's/Create Function: /kk_begin\ndelimiter $$\n/g' | sed 's/[ ]*character_set_client:/$$ \ndelimiter ;\nkk_end/g' |\	sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin\|kk_end.*//g;p}' >> $save_file fi # 存储过程if [[ $output_type == *"procedure"* ]]then	echo "-- ------------------------------------------------------------" >> $save_file	echo "-- procedure" >> $save_file	echo "-- ------------------------------------------------------------" >> $save_file	mysql -h$db_host -P$db_port -u$db_user -p$db_pwd --skip-column-names \	-e "select concat('SHOW CREATE PROCEDURE ',routine_schema,'.',routine_name,';') from information_schema.routines where routine_schema='$db_name' and ROUTINE_TYPE='PROCEDURE'" |\	sed 's/;/\\G/g' | mysql -h$db_host -P$db_port -u$db_user -p$db_pwd $db_name |\	sed 's/Create Procedure: /kk_begin\ndelimiter $$\n/g' | sed 's/[ ]*character_set_client:/$$ \ndelimiter ;\nkk_end/g' |\	sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin\|kk_end.*//g;p}' >> $save_filefi # 事件if [[ $output_type == *"event"* ]]then	echo "-- ------------------------------------------------------------" >> $save_file	echo "-- event" >> $save_file	echo "-- ------------------------------------------------------------" >> $save_file	mysql -h$db_host -P$db_port -u$db_user -p$db_pwd --skip-column-names \	-e "select concat('SHOW CREATE EVENT ',EVENT_SCHEMA,'.',EVENT_NAME,';') from information_schema.events where EVENT_SCHEMA='$db_name'" |\	sed 's/;/\\G/g' | mysql -h$db_host -P$db_port -u$db_user -p$db_pwd |\	sed 's/Create Event: /kk_begin\ndelimiter $$\n/g' | sed 's/[ ]*character_set_client:/$$ \ndelimiter ;\nkk_end/g' |\	sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin\|kk_end.*//g;p}' >> $save_filefi # 触发器if [[ $output_type == *"trigger"* ]]then	echo "-- ------------------------------------------------------------" >> $save_file	echo "-- trigger" >> $save_file	echo "-- ------------------------------------------------------------" >> $save_file	mysql -h$db_host -P$db_port -u$db_user -p$db_pwd --skip-column-names \	-e "select concat('SHOW CREATE TRIGGER ',TRIGGER_SCHEMA,'.',TRIGGER_NAME,';') from information_schema.triggers where TRIGGER_SCHEMA='$db_name';" |\	sed 's/;/\\G/g' | mysql -h$db_host -P$db_port -u$db_user -p$db_pwd $db_name|\	sed 's/SQL Original Statement: /kk_begin\ndelimiter $$\n/g' | sed 's/[ ]*character_set_client:/$$ \ndelimiter ;\nkk_end/g' |\	sed -n '/kk_begin/{:a;N;/kk_end/!ba;s/.*kk_begin\|kk_end.*//g;p}' >> $save_filefi # ^M, you need to type CTRL-V and then CTRL-Msed -i "s/\^M//g" $save_file

执行过程如下:

后面会分享更多devops和DBA方面内容,感兴趣的朋友可以关注下~

标签: #mysql视图备份 #mysql函数和过程 #mysql导出成mysql #mysql如何写函数 #mysql有存储过程和触发器吗