常用脚本

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 statusG;
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

   

原文地址:https://www.cnblogs.com/ivictor/p/5580695.html