【MySQL】不影响IO安全删除大表脚本

#!/bin/bash
db_path="数据库路径"
db_user=''
db_pass=''
db_host=''
db_name=''



TruncateFile(){
  file_name=$1
  del_speed=10485760
  file_size=$(/usr/bin/stat -c%s "$file_name")
  if [[ -f /usr/bin/truncate ]];then
    for del_size in `seq $file_size -${del_speed} ${del_speed}`
    do
      echo $del_size
      /usr/bin/truncate -s $del_size $file_name
    done
    /usr/bin/rm -f $file_name
  fi
}

need_to_drop_ghost_tb=$(mysql -u$db_user  -h$db_host -p$db_pass -N -s -D $db_name -e "show tables like '%_del';")
for i in $need_to_drop_ghost_tb
do
	#echo $i
	/usr/bin/ln $db_path/${i}.ibd $db_path/${i}.ibd.hdlk
	mysql -u$db_user -h$db_host -p$db_pass -e "set session sql_log_bin=0;drop table $db_name.$i;"
	if [ $? -ne 0 ]
        then
            exit 1
        fi
	TruncateFile $db_path/${i}.ibd.hdlk
	echo "删除表${i}成功"
done

echo "Finish"

你可能感兴趣的:(MySQL,mysql,安全,数据库)