mysql下删除记录较多的表会出现如下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.

因此需要分批删除表中数据,脚本如下:

#!/bin/bash
DB_USER="root"
DB_PASS="pw"
DB_HOST="10.228.221.25"
DB_NAME="tjxx"
PORT="3306"

echo "Sql start" >> /home/xxf/log.txt

timeS=`date "+%Y-%m-%d"`
echo "${timeS}" >> /home/xxf/log.txt

delete_sql="delete from region_stastics_history where time < '${timeS}' order by id limit 1000;"


#mysql -u$DB_USER -p$DB_PASS -h$DB_HOST -P${PORT} $DB_NAME -e "${delete_sql}"

count=0
while [ $count -le 500 ];do
   echo $count >> /home/xxf/log.txt
   mysql -u$DB_USER -p$DB_PASS -h$DB_HOST -P${PORT} $DB_NAME -e "${delete_sql}"
   count=$((count + 1))
done
echo "Sql End" >> /home/xxf/log.txt

Logo

更多推荐