关于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
相关文章
- Mysql net start mysql启动,提示发生系统错误 5 拒绝訪问 解决之道
- Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)
- mysql大数据量分页查询靠后页数的语句写法
- Mysql错误check the manual that corresponds to your MySQL server version for the right syntax
- MySQL数据直接实时同步到ES
- Mysql 数据库设计规范
- 【MySQL】如何向mysql数据库插入当前时间
- 对mysql乐观锁、悲观锁、共享锁、排它锁、行锁、表锁概念的理解 (转)
- 项目访问mysql时报: Failed to obtain JDBC Connection...:Host ‘X‘is not allowed to connect to this MySQL ser
- 叶金荣:MySQL通用优化技巧
- 基于php和mysql实现(Web)学生课外活动成果统计系统【100010274】
- MariaDB/MySQL备份和恢复(一):mysqldump工具用法详述
- mysql只更新日期不更新时分秒,Mysql取30天内每天最大的数据
- mysql PXC配置
- python操作mysql数据库系列-操作MySql数据库(五)
- Windows 免安装版mysql,适用于版本MySQL 5.7.7及以上,快速配置教程
- MySQL EXPLAIN 命令: 查看查询执行计划
- mysql—MySQL数据库中10位时间戳转换为标准时间后,如何对标准时间进行加减X天处理
- Mysql_mysql 性能分析及explain用法
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
- 在centos中php 在连接mysql的时候,出现Can't connect to MySQL server on 'XXX' (13)
- MySQL-8.0.21.0社区版安装教程
- 熟知MySQL存储过程
- 今天用pro安装nginx+php+mysql出现故障的解决方法
- Docker Compose安装使用,及搭建nginx+php+mysql基础应用实例
- Eclipse中使用MySql遇到:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading o
- 高性能 MySQL(五):设计表结构时,如何选择数据类型会更高效?