zoukankan      html  css  js  c++  java
  • 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.导入

    1 ls -l *.sql |awk '{print $9}' | while read line
    2 do
    3 mysql -h5.5.5.101 -uroot -proot -P3306  test < $line
    4 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
    数据量统计脚本
  • 相关阅读:
    LeetCode 842. Split Array into Fibonacci Sequence
    LeetCode 1087. Brace Expansion
    LeetCode 1219. Path with Maximum Gold
    LeetCode 1079. Letter Tile Possibilities
    LeetCode 1049. Last Stone Weight II
    LeetCode 1046. Last Stone Weight
    LeetCode 1139. Largest 1-Bordered Square
    LeetCode 764. Largest Plus Sign
    LeetCode 1105. Filling Bookcase Shelves
    LeetCode 1027. Longest Arithmetic Sequence
  • 原文地址:https://www.cnblogs.com/imdba/p/10114743.html
Copyright © 2011-2022 走看看