批量给mycat配置文件增加数据库节点

#!/bin/bash

#修改mysql schema
mycat_schema_file=/opt/mycat/conf/schema.xml
cp $mycat_schema_file ${mycat_schema_file}.bak

database_list='message xuejie'
#找到usercenter的行号,由于schema成对出现有可能换行,在其上一行增加schema更可靠
last_schema_row_number=`sed -n  '/<schema name="usercenter"/=' $mycat_schema_file`
if [ ! -z "$last_schema_row_number" ];
then
	last_schema_row_number=`expr ${last_schema_row_number} - 1`
	for database in $database_list
	do
	    new_schema_row_number=`sed -n "/<schema name=\"$database\"/=" $mycat_schema_file`
	    if [ -z "$new_schema_row_number" ];
	    then
	        shell_cmd='<schema name="'${database}'" checkSQLschema="false" sqlMaxLimit="20000" dataNode="'${database}'"></schema>'
	        sed -i "${last_schema_row_number}a ${shell_cmd}" $mycat_schema_file
	        last_schema_row_number=`expr ${last_schema_row_number} + 1`
	        sed -i "${last_schema_row_number}s/^/       /g"  $mycat_schema_file
	    fi
	done
fi

#找到usercenter的行号,在下面增加dataNode
last_node_row_number=`sed -n  '/database="usercenter"/=' $mycat_schema_file`
if [ ! -z "$last_node_row_number" ];
then
	for database in $database_list
	do
	    new_node_row_number=`sed -n  "/database=\"$database\"/=" $mycat_schema_file`
	    if [ -z "$new_node_row_number" ];
	    then
	        shell_cmd='<dataNode name="'$database'" dataHost="localhost1" database="'$database'" />'
	        sed -i "${last_node_row_number}a ${shell_cmd}" $mycat_schema_file
	        last_node_row_number=`expr ${last_node_row_number} + 1`
	        sed -i "${last_node_row_number}s/^/       /g"  $mycat_schema_file
	    fi
    done
fi

  将以上代码保存至multi_add_database_to_mycat_conf.sh

#赋予执行权限
chmod +x multi_add_database_to_mycat_conf.sh
#执行脚本
/bin/bash multi_add_database_to_mycat_conf.sh
#执行脚本(打印每行执行debug过程)
/bin/bash -x multi_add_database_to_mycat_conf.sh

  

posted @ 2023-04-14 16:38  雪洁  阅读(45)  评论(0编辑  收藏  举报