zoukankan      html  css  js  c++  java
  • 关于Mysql 查询所有表的实时记录用于对比2个MySQL 库的数据是否异步

    Xu言:

      今天,为了研究一个MySQL主从同步开机后报错 问题,如下图

    故障原因分析:

     经过分析,可能是主从服务器开机顺序导致。(有待下次断电再次测试)

    主从错误提示:日志读取错误的问题。解决方法:更新日志记录文件,重新主从同步。

    担心主从问题过程中有数据写入,想去确认下主从库上的数据是否一致。想到了查询下数据库行数的方式。

    网上查询了下 ,一般有2种:

    方法一:查看当前表的记录行数

    SELECT count(*) from 表名

    方法二:"查看数据库中所有表的记录数"  # 这里之所以打引号,是因为这里的数据不准确

    SELECT table_name,table_rows FROM information_schema.tables 

    WHERE TABLE_SCHEMA = 'testdb' 

    ORDER BY table_rows DESC;

    所以,经过各路大仙帮助使用了第一种方法进行了改良。

    拼接法:

    借助information_schema库的tables表,来拼接出一个条sql语句

    use information_schema;
    
    select concat(
        'select "', 
        TABLE_name, 
        '", count(*) from ', 
        TABLE_SCHEMA, 
        '.',
        TABLE_name,
        ' union all'
    ) from tables 
    where TABLE_SCHEMA='数据库名';

    拼接出来以后,使用工具去掉“union all ”部分,批量执行。 

    Python方式循环:

    #!/usr/bin/env python
    # Author: Loki
    # Date: 2019-02-012
    # Version: 0.1
    
    import pymysql
    User = ''  # 这里补充你的MySQL用户名
    Pass = ''  # 这里补充你的MySQL密码
    Port = 33060  # 自己的MySQL端口
    
    db = pymysql.connect(host="192.168.x.x", port=Port, user=User, password=Pass, db='数据库名')
    
    cursor = db.cursor()
    
    cursor.execute("show tables")  # 查询本数据库的所有表名
    table_name = cursor.fetchall()
    
    count = 0
    for item in table_name:  # 循环
        count += 1
        tbn = item[0]
        sql_ = "SELECT count(*) FROM %s" % tbn
        cursor.execute(sql_)
        data = cursor.fetchone()
        print("table_name=%s, row=%s" % (tbn,data))
    print("table totle= %s" % count)
    
    # Close Connect
    cursor.close()
    db.close()

    以上Python方法输出比较不友好,可以优化为输出到文本里面。账号密码部分也可以使用input()函数方式来提示填入

    Shell方式循环:

    #!/bin/bash
    # Author:Jerry

    tb_name=`mysql -u账号 -p密码 -h192.168.x.x -P端口 -e "select table_name from information_schema.tables where table_schema='数据库名'"|awk 'NR>1{print $1}'` for name in $tb_name ; do tbl_count=`mysql -u账号 -p密码 -h192.168.x.x -P端口 -e "select count (*) as times from cwsys.$name;"| tail -1` echo "$name=$tbl_count" >>/home/xxx/xxx.log done

     以上就是使用的一些方法和思路,留个记录以作备忘。

     PS:最后鸣谢各路大仙,就不一一点名!你们懂得

    参考资料:

    https://www.cnblogs.com/woider/p/5926744.html

    https://blog.csdn.net/a19860903/article/details/52311765

    https://www.cnblogs.com/xfxing/p/9322199.html

  • 相关阅读:
    无线鼠标换电池了
    Jython Interactive Servlet Console YOU WILL NEVER KNOW IT EXECLLENT!!! GOOD
    Accessing Jython from Java Without Using jythonc
    jython podcast cool isnt't it?
    Python里pycurl使用记录
    Creating an Interactive JRuby Console for the Eclipse Environment
    微软为AJAX和jQuery类库提供CDN服务
    Download A File Using Cygwin and cURL
    What is JMRI?这个是做什么用的,我真没看懂但看着又很强大
    用curl 发送指定的大cookie的http/https request
  • 原文地址:https://www.cnblogs.com/Cong0ks/p/10364502.html
Copyright © 2011-2022 走看看