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

近期遇到一个需求,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数据库中所有表记录总数

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

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

猜你喜欢

转载自blog.51cto.com/jdonghong/2351689