码迷,mamicode.com
首页 > 数据库 > 详细

shell 脚本获取MySQL数据库中所有表记录总数

时间:2019-02-19 11:43:30      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:inf   原因   mat   数据   ash   mysq   schema   tables   .com   

近期遇到一个需求,Mysql数据库中需要统计所有表的记录数据:
查了下资料可以调取information_schema数据表中数据获取所有表记录数据,但是查询出来的数据,发现和手动统计的记录数据不一致,information_schema查询出来的数据部分不准确【原因应该是部分表数据没有自动同步】。折腾了下,于是还是自己手动写个脚本,分享下也做下次备用。
程序结构:
#!/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

技术图片
执行脚本:
技术图片
查看生成的统计记录:
技术图片
查看统计数据:
技术图片

shell 脚本获取MySQL数据库中所有表记录总数

标签:inf   原因   mat   数据   ash   mysq   schema   tables   .com   

原文地址:http://blog.51cto.com/jdonghong/2351689

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!