近期遇到一个需求,Mysql数据库中须要统计全部表的记录数据:
查了下资料能够调取information_schema数据表中数据获取全部表记录数据,可是查询出来的数据,发现和手动统计的记录数据不一致,information_schema查询出来的数据部分不许确【缘由应该是部分表数据没有自动同步】。折腾了下,因而仍是本身手动写个脚本,分享下也作下次备用。
程序结构:mysql
#!/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
donesql
执行脚本:
查看生成的统计记录:
查看统计数据:shell