1. 程式人生 > 資料庫 >一個Shell小指令碼精準統計Mysql每張表的行數實現

一個Shell小指令碼精準統計Mysql每張表的行數實現

前言

對於開發或者運維人員來說,Mysql資料庫每張表的數量肯定是要了解下,有助於我們清理無用資料或者瞭解哪張表比較佔用空間。

另外多次統計表的行數,還能發現Mysql表的增量情況,能夠預測表未來會有多大的量。

廢話不多說,直接帶大家寫一個簡單的Shell小指令碼

迴圈獲取資料庫名

直接上Shell程式碼,show databases獲取所有的庫名。結果有一個我們不想要的,就是Database,這個grep -v掉,輕鬆獲取所有資料庫

[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

迴圈獲取所有表

有了庫資訊,獲取所有表就簡單了,直接上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           |

迴圈統計每張表的行數

取出庫名加表名,一個select count(1)統計表的行數,迴圈統計,直接上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

變數化,指令碼直接用

需要統計哪個Mysql,前面三個變數一改,立馬就能統計所有表的大小了。

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

想看哪張表的行數最多?

之前的指令碼加個 |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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。