濮阳杆衣贸易有限公司

主頁 > 知識庫 > 一個Shell小腳本精準(zhǔn)統(tǒng)計Mysql每張表的行數(shù)實現(xiàn)

一個Shell小腳本精準(zhǔn)統(tǒng)計Mysql每張表的行數(shù)實現(xiàn)

熱門標(biāo)簽:外呼系統(tǒng)的合法性 威海電銷外呼系統(tǒng)好用嗎 房產(chǎn)證地圖標(biāo)注的兩個面積 輝縣市地圖標(biāo)注 武漢語音電銷機器人加盟 地圖標(biāo)注x是啥意思 湖北孝感如何辦理 北京電銷機器人對市場的影響 同花順電話機器人微信

前言

對于開發(fā)或者運維人員來說,Mysql數(shù)據(jù)庫每張表的數(shù)量肯定是要了解下,有助于我們清理無用數(shù)據(jù)或者了解哪張表比較占用空間。

另外多次統(tǒng)計表的行數(shù),還能發(fā)現(xiàn)Mysql表的增量情況,能夠預(yù)測表未來會有多大的量。

廢話不多說,直接帶大家寫一個簡單的Shell小腳本

循環(huán)獲取數(shù)據(jù)庫名

直接上Shell代碼,show databases獲取所有的庫名。結(jié)果有一個我們不想要的,就是Database,這個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)計每張表的行數(shù)

取出庫名加表名,一個select count(1)統(tǒng)計表的行數(shù),循環(huán)統(tǒng)計,直接上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)計哪個Mysql,前面三個變量一改,立馬就能統(tǒng)計所有表的大小了。

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ù)最多?

之前的腳本加個 |sort -nrk 2|less 搞定,超實用的小腳本就這樣完成了

[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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 監(jiān)控MySQL主從狀態(tài)的shell腳本
  • shell腳本一鍵安裝MySQL5.7.29的方法
  • mysql常用備份命令和shell備份腳本分享
  • shell腳本定時備份MySQL數(shù)據(jù)庫數(shù)據(jù)并保留指定時間
  • shell腳本自動化創(chuàng)建虛擬機的基本配置之tomcat--mysql--jdk--maven
  • shell腳本實現(xiàn)mysql定時備份、刪除、恢復(fù)功能
  • 通過Shell腳本批量創(chuàng)建服務(wù)器上的MySQL數(shù)據(jù)庫賬號
  • 使用shell腳本來給mysql加索引的方法
  • 干掉一堆mysql數(shù)據(jù)庫,僅需這樣一個shell腳本(推薦)
  • 使用shell腳本每天對MySQL多個數(shù)據(jù)庫自動備份的講解
  • MySQL Shell的介紹以及安裝

標(biāo)簽:安康 紹興 西寧 武威 日喀則 蚌埠 迪慶 麗江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《一個Shell小腳本精準(zhǔn)統(tǒng)計Mysql每張表的行數(shù)實現(xiàn)》,本文關(guān)鍵詞  一個,Shell,小,腳本,精準(zhǔn),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《一個Shell小腳本精準(zhǔn)統(tǒng)計Mysql每張表的行數(shù)實現(xiàn)》相關(guān)的同類信息!
  • 本頁收集關(guān)于一個Shell小腳本精準(zhǔn)統(tǒng)計Mysql每張表的行數(shù)實現(xiàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    贵州省| 郯城县| 新绛县| 永福县| 乐昌市| 百色市| 淳化县| 凤翔县| 元朗区| 临汾市| 清河县| 安西县| 兴义市| 通化县| 扎鲁特旗| 罗平县| 玉田县| 兴仁县| 井陉县| 新民市| 商城县| 衡南县| 肇庆市| 合川市| 沅陵县| 新丰县| 彭山县| 大方县| 南漳县| 额敏县| 林西县| 长顺县| 苍溪县| 闸北区| 南郑县| 普宁市| 凤城市| 大渡口区| 根河市| 双牌县| 昭觉县|