您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何使用Shell小腳本精準(zhǔn)統(tǒng)計(jì)Mysql每張表的行數(shù),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
循環(huán)獲取數(shù)據(jù)庫名
直接上Shell代碼,show databases獲取所有的庫名。結(jié)果有一個(gè)我們不想要的,就是Database,這個(gè)grep -v掉,輕松獲取所有數(shù)據(jù)庫
[root@shijiangeit ~]# mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | shijiange | | test | | wordpress | +--------------------+
[root@shijiangeit ~]# mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null |grep -v Database information_schema mysql performance_schema shijiange test wordpress
循環(huán)獲取所有表
有了庫信息,獲取所有表就簡單了,直接上Shell代碼。show tables獲取所有表名,其中Tables_in不需要,grep -v掉。
[root@shijiangeit ~]# for onedb in $(mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null |grep -v Database);do > echo $onedb > mysql -h 127.0.0.1 -uxxx -pxxx $onedb -e "show tables" 2>/dev/null > done 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 |
循環(huán)統(tǒng)計(jì)每張表的行數(shù)
取出庫名加表名,一個(gè)select count(1)統(tǒng)計(jì)表的行數(shù),循環(huán)統(tǒng)計(jì),直接上Shell代碼。
[root@shijiangeit ~]# for onedb in $(mysql -h 127.0.0.1 -uxxx -pxxx -e "show databases;" 2>/dev/null |grep -v Database);do > for onetab in $(mysql -h 127.0.0.1 -uxxx -pxxx $onedb -e "show tables" 2>/dev/null |grep -v 'Tables_in_');do > onetablength=$(mysql -h 127.0.0.1 -uxxx -pxxx $onedb -e "select count(1) from $onetab" 2>/dev/null |grep -v 'count') > echo -e "$onedb.$onetab\t$onetablength" > done > done 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
變量化,腳本直接用
需要統(tǒng)計(jì)哪個(gè)Mysql,前面三個(gè)變量一改,立馬就能統(tǒng)計(jì)所有表的大小了。
mysqlhost=127.0.0.1 mysqluser=xxx mysqlpassword=xxx for onedb in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword -e "show databases;" 2>/dev/null |grep -v Database);do for onetab in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "show tables" 2>/dev/null |grep -v 'Tables_in_');do onetablength=$(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "select count(1) from $onetab" 2>/dev/null |grep -v 'count') echo -e "$onedb.$onetab\t$onetablength" done done
想看哪張表的行數(shù)最多?
之前的腳本加個(gè) |sort -nrk 2|less 搞定,超實(shí)用的小腳本就這樣完成了
[root@shijiangeit ~]# for onedb in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword -e "show databases;" 2>/dev/null |grep -v Database);do > for onetab in $(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "show tables" 2>/dev/null |grep -v 'Tables_in_');do > onetablength=$(mysql -h $mysqlhost -u$mysqluser -p$mysqlpassword $onedb -e "select count(1) from $onetab" 2>/dev/null |grep -v 'count') > echo -e "$onedb.$onetab\t$onetablength" > done > done | sort -nrk 2 shijiange.user 2097153 information_schema.INNODB_BUFFER_PAGE 8191 performance_schema.events_waits_summary_by_thread_by_event_name 5320 information_schema.INNODB_BUFFER_PAGE_LRU 3453
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用Shell小腳本精準(zhǔn)統(tǒng)計(jì)Mysql每張表的行數(shù)”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。