常用脚本

1. 自动生成分区

复制代码
#!/bin/bash
today="20160401"
while true
do
if [ $today -gt "20160731" ];then
exit 0
fi
par="PARTITION p$today VALUES LESS THAN ('$today'),"
#par="PARTITION p$today VALUES IN ($today),"
today=`date -d "$today 1 days" +%Y%m%d`
echo $par
done
复制代码

 

2. 将行构造成json字段

复制代码
#!/bin/bash
mysql -s -phello test >1.log <<EOF
desc t1;
EOF
lines="concat_ws(',',"
count=`cat 1.log|wc -l`
linenum=0
while read line
do
 coloumname=`echo $line |awk '{print $1}'`
 let linenum=linenum+1
  if [ $linenum -eq 1 ];then
    lines=$lines"concat_ws(':','{"'"'$coloumname'"'"',if("$coloumname" is null or $coloumname='','null',concat('"'"'"',$coloumname,'"'"'"')))"
 elif [ $linenum -eq $count ];then
    lines=$lines",concat_ws(':','"'"'$coloumname'"'"',if("$coloumname" is null or $coloumname='','null}',concat('"'"'"',$coloumname,'"'"'"}'))))"
 else
    lines=$lines",concat_ws(':','"'"'$coloumname'"'"',if("$coloumname" is null or $coloumname='','null',concat('"'"'"',$coloumname,'"'"'"')))"
  fi
done < 1.log
echo $lines
复制代码

 

3. MySQL主从搭建

复制代码
#!/bin/bash
master_ip=192.168.244.145
master_user=root
master_password=123456
slave_ip=192.168.244.146
slave_user=root
slave_password=123456
position=`mysql -u"$master_user" -p"$master_password" -e "show master status;" |awk 'NR==2{print $1,$2}'`
file=`echo $position |awk '{print $1}'`
pos=`echo $position |awk '{print $2}'`
mysql -h"$slave_ip" -u"$slave_user" -p"$slave_password" <<EOF
stop slave;
CHANGE MASTER TO
  MASTER_HOST='$master_ip',
  MASTER_USER='repl',
  MASTER_PASSWORD='repl',
  MASTER_LOG_FILE='$file',
  MASTER_LOG_POS=$pos;
start slave;
select sleep(3);
show slave status\G;
EOF
复制代码

 

4. 分区表迁移脚本

复制代码
#!/bin/bash
mysqlclient="mysql -uroot -p123456 --socket=/home/bdp/mysql/tmp/mysql.sock"

function move_partition(){
mp_schema=$1
mp_table=$2
mp_tmp=$3
mp_partition=$4
$mysqlclient <<EOF
use $mp_schema;
alter table $mp_table exchange partition $mp_partition with table $mp_tmp;
exit
EOF
}

function create_tmptable(){
ct_schema=$1
ct_origin_table=$2
ct_tmp_table=$3
$mysqlclient <<EOF
use $ct_schema;
create table $ct_tmp_table like $ct_origin_table;
alter table $ct_tmp_table remove partitioning;
exit
EOF
}

function drop_table(){
dt_schema=$1
dt_table=$2
$mysqlclient <<EOF
use $dt_schema;
drop table $dt_table;
exit
EOF
}

function remove_partition(){
rp_schema=$1
rp_table=$2
rp_partition=$3
$mysqlclient <<EOF
use $rp_schema;
alter table $rp_table drop partition $rp_partition;
exit
EOF
}

function tmp_to_bak(){
ttb_schema=$1
ttb_table=$2
ttb_tmp=$3
$mysqlclient <<EOF
use $ttb_schema;
set autocommit=0;
insert into $ttb_table select * from $ttb_tmp;
truncate table $ttb_tmp;
commit;
exit
EOF
}

function migrate_data(){
md_schema=$1
md_origin_table=$2
md_begindate=$3
md_enddate=$4
md_tmp_table="$md_origin_table"_tmp
md_bak_table="$md_origin_table"_bak
create_tmptable $md_schema $md_origin_table $md_tmp_table
while true
do
if [ "$md_begindate" -gt "$md_enddate" ];then
drop_table $md_schema $md_tmp_table
return 0
fi
move_partition $md_schema $md_origin_table $md_tmp_table p"$md_begindate"
remove_partition $md_schema $md_origin_table p"$md_begindate"
tmp_to_bak $md_schema $md_bak_table $md_tmp_table
md_begindate=`date -d "$md_begindate 1 days" +%Y%m%d`
echo $md_begindate
done
}

while read schema origin_table begindate enddate
do
migrate_data $schema $origin_table $begindate $enddate
done < tablename.txt
复制代码

其中tablename.txt的内容如下:

test t 20151211 20160131
test1 t1 220151211 20160131
test2 t2 20151211 20160131

 

   

posted @   iVictor  阅读(523)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示