介绍
这篇文章主要介绍了如何使用壳小脚本精准统计Mysql每张表的行数,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获、下面让小编带着大家一起了解一下。
<强>循环获取数据库名
强>
直接上壳代码,显示数据库获取所有的库名。结果有一个我们不想要的,就是数据库,这个grep - v掉,轻松获取所有数据库
[root@shijiangeit ~] #, mysql -h 127.0.0.1 -uxxx -pxxx -e “show 数据库;“2在/dev/null + - - - - - - - - - - - - - - - - - - - - - - + |,Database ,,,, | + - - - - - - - - - - - - - - - - - - - - - - + | |,information_schema  |,mysql ,,,,, | | |,performance_schema  |,shijiange ,,, | |,test ,,,,,, | |,wordpress ,,, | + - - - - - - - - - - - - - - - - - - - - - - +
[root@shijiangeit ~] #, mysql -h 127.0.0.1 -uxxx -pxxx -e “show 数据库;“,2在/dev/null | grep -v 数据库 information_schema mysql performance_schema shijiange 测试 wordpress
<>强循环获取所有表强>
有了库信息,获取所有表就简单了,直接上壳代码,告诉表获取所有表名,其中Tables_in不需要,grep - v掉。
[root@shijiangeit ~] #, for onedb 拷贝美元(mysql -h  127.0.0.1 -uxxx -pxxx -e “show 数据库;“,2在/dev/null | grep -v 数据库); 祝辞,echo onedb美元 在,,mysql -h  127.0.0.1 -uxxx -pxxx onedb 美元;-e “show tables" 2在/dev/null 在做 information_schema +---------------------------------------+ |,Tables_in_information_schema ,,, | +---------------------------------------+ |,CHARACTER_SETS ,,,,,,,,,, | |,COLLATIONS ,,,,,,,,,,,, | | |,COLLATION_CHARACTER_SET_APPLICABILITY  |,COLUMNS ,,,,,,,,,,,,,, | |,COLUMN_PRIVILEGES ,,,,,,,,, | |,ENGINES ,,,,,,,,,,,,,, | |,EVENTS ,,,,,,,,,,,,,, | |,FILES ,,,,,,,,,,,,,,, | |,GLOBAL_STATUS ,,,,,,,,,,, | |,GLOBAL_VARIABLES ,,,,,,,,, | |,KEY_COLUMN_USAGE ,,,,,,,,,, |
<强>循环统计每张表的行数强>
取出库名加表名,一个select count(1)统计表的行数,循环统计,直接上壳代码。
[root@shijiangeit ~] #, for onedb 拷贝美元(mysql -h  127.0.0.1 -uxxx -pxxx -e “show 数据库;“,2在/dev/null | grep -v 数据库); 在,,for onetab 拷贝美元(mysql -h  127.0.0.1 -uxxx -pxxx onedb 美元;-e “show tables", 2在/dev/null | grep -v & # 39; Tables_in_& # 39;); 在,,,onetablength=$ (mysql -h  127.0.0.1 -uxxx -pxxx onedb 美元;-e “select 计数(1),得到onetab"美元,2在/dev/null | grep -v & # 39;计数# 39;) 在,,,echo -e “onedb美元。onetab \ t onetablength"美元; 祝辞,完成 在做 information_schema.CHARACTER_SETS 40 information_schema.COLLATIONS 219年 information_schema.COLLATION_CHARACTER_SET_APPLICABILITY 219年 information_schema.COLUMNS 1789 information_schema.COLUMN_PRIVILEGES 0 shijiange.logincount 4 shijiange.member 0 shijiange.user 2097153 test.detect_servers 0 wordpress.wp_commentmeta 0 wordpress.wp_comments 0 wordpress.wp_links 0 wordpress.wp_options , 156
<强>变量化,脚本直接用强>
需要统计哪个Mysql,前面三个变量一改,立马就能统计所有表的大小了。
mysqlhost=127.0.0.1 mysqluser=xxx mysqlpassword=xxx for onedb 拷贝$ (mysql -h  mysqlhost 美元;- u mysqluser 美元;- p mysqlpassword 美元;-e “show 数据库;“,2在/dev/null | grep -v 数据库); 美元,for onetab 拷贝(mysql -h  mysqlhost 美元;- u mysqluser 美元;- p mysqlpassword 美元;onedb 美元;-e “show tables", 2在/dev/null | grep -v & # 39; Tables_in_& # 39;); null null null null null null null null null null null null null如何使用壳小脚本精准统计Mysql每张表的行数