Mysql分库分表导出导入和数据量统计测试

时间:2023-03-10 06:20:49
Mysql分库分表导出导入和数据量统计测试

需求:添加创建了分库分表后,业务可能将数据已经写入,但未来得及接入到otter汇总库。接入汇总库前需要初始化这部分数据。

1.导出

ip_port_list=(5.5.5.101:3306 5.5.5.102:3306)

len=${#ip_port_list[@]}
for ((i=0;i<=$len-1;i++))
do db_ip=`echo ${ip_port_list[i]} | awk -F[:] '{print $1}'`
db_port=`echo ${ip_port_list[i]} | awk -F[:] '{print $2}'`
mysql -h$db_ip -P$db_port -uroot -proot 2>/dev/null -Ns>test_shard_${db_ip}_${db_port} <<EOF
select table_schema,table_name from information_schema.TABLES where table_schema like 'test_shard_%' and table_name like 'test_%';
EOF cat test_shard_${db_ip}_${db_port} |while read line
do
schema_name=`echo $line | awk '{print $1}'`
table_name=`echo $line | awk '{print $2}'`
echo `date "+%Y-%m-%d %H:%M:%S"` "start dump ${schema_name}_${table_name}..."| tee -a all_tables.log
mysqldump -h$db_ip -P$db_port -uroot -proot 2>/dev/null --databases ${schema_name} --tables ${table_name} -t -c --single-transaction > $table_name.sql
sed -i "s/$table_name/test/g" $table_name.sql
done
done

导出脚本

2.导入

 ls -l *.sql |awk '{print $9}' | while read line
do
mysql -h5.5.5.101 -uroot -proot -P3306 test < $line
done

导入脚本

3.数据量统计

ip_port_list=(5.5.5.101:3306 5.5.5.102:3306)

len=${#ip_port_list[@]}
sum=0 for ((i=0;i<=$len-1;i++))
do db_ip=`echo ${ip_port_list[i]} | awk -F[:] '{print $1}'`
db_port=`echo ${ip_port_list[i]} | awk -F[:] '{print $2}'`
mysql -h$db_ip -P$db_port -uroot -proot 2>/dev/null -Ns>test_shard_${db_ip}_${db_port} <<EOF
select concat(table_schema,'|',table_name) from information_schema.TABLES where table_schema like 'test_shard_%' and table_name like 'test_%';
EOF for line in `cat test_shard_${db_ip}_${db_port}`
do
schema_name=`echo $line | awk -F['|'] '{print $1}'`
table_name=`echo $line | awk -F['|'] '{print $2}'`
query_sql="select count(*) from $schema_name.$table_name;"
cnt=$( echo "$query_sql" | mysql -h$db_ip -uroot -proot -P$db_port 2>/dev/null -s)
echo "$schema_name.$table_name have $cnt rows."
let "sum=sum+cnt"
#rm -rf test_shard_${db_ip}_${db_port}
done
done
echo $sum

数据量统计脚本