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
    数据量统计脚本
  • 相关阅读:
    【转】CUDA5/CentOS6.4
    【转】centos 6.4 samba 安装配置
    【转】Install MATLAB 2013a on CentOS 6.4 x64 with mode silent
    【转】Getting xrdp to work on CentOS 6.4
    【VLFeat】使用matlab版本计算HOG
    Unofficial Windows Binaries for Python Extension Packages
    March 06th, 2018 Week 10th Tuesday
    March 05th, 2018 Week 10th Monday
    March 04th, 2018 Week 10th Sunday
    March 03rd, 2018 Week 9th Saturday
  • 原文地址:https://www.cnblogs.com/imdba/p/10114743.html
Copyright © 2011-2022 走看看