MySQL基础:数据库对象查看工具mysqlshow

在这里插入图片描述
mysql自带一个数据库查看工具,使用起来非常方便,本文对mysqlshow常见的使用方式进行介绍。

常见场景

mysql中的某一个schema,希望确认一下其中的数据库表的行数

很有可能你使用给一个简单的shell就完成了这个功能,你的脚本可能是这样写的(使用容器方式启动的mysql)

[root@host131 ~]# cat get_db_cnt.sh
#!/bin/sh

CONTAINER_NAME=mysql_mysql_1
DB_NAME=mysql
DB_USER=root
DB_PSWD=liumiaocn

execute_cmd(){
  cmd="$*"
  docker exec ${CONTAINER_NAME} mysql -u${DB_USER} -p${DB_PSWD} -e "use ${DB_NAME}; ${cmd};" 2>/dev/null
}

count_table(){
  table="$1"
  cnt=`docker exec ${CONTAINER_NAME} mysql -u${DB_USER} -p${DB_PSWD} -e "use ${DB_NAME}; select count(*) from ${table}" 2>/dev/null`
  echo $cnt |awk '{print $2}'
}

ALL_TABLES=`execute_cmd "show tables" |grep -v "Tables_in_${DB_NAME}"`
for table in ${ALL_TABLES}
do
  echo -n "$table  "
  count_table ${table}
done
[root@host131 ~]#

而执行则会得到这样的结果

[root@host131 ~]# sh get_db_cnt.sh 
columns_priv  0
db  2
engine_cost  2
event  0
func  0
general_log  0
gtid_executed  0
help_category  40
help_keyword  681
help_relation  1339
help_topic  637
innodb_index_stats  1736
innodb_table_stats  289
ndb_binlog_index  0
plugin  0
proc  48
procs_priv  0
proxies_priv  1
server_cost  6
servers  0
slave_master_info  0
slave_relay_log_info  0
slave_worker_info  0
slow_log  0
tables_priv  1
time_zone  1811
time_zone_leap_second  0
time_zone_name  1811
time_zone_transition  121792
time_zone_transition_type  8771
user  2
[root@host131 ~]# 

而如果你使用mysqlshow,则可以更简单地得到这样的结果

# mysqlshow -uroot -pliumiaocn mysql --count
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: mysql
+---------------------------+----------+------------+
|          Tables           | Columns  | Total Rows |
+---------------------------+----------+------------+
| columns_priv              |        7 |          0 |
| db                        |       22 |          2 |
| engine_cost               |        6 |          2 |
| event                     |       22 |          0 |
| func                      |        4 |          0 |
| general_log               |        6 |          0 |
| gtid_executed             |        3 |          0 |
| help_category             |        4 |         40 |
| help_keyword              |        2 |        681 |
| help_relation             |        2 |       1339 |
| help_topic                |        6 |        637 |
| innodb_index_stats        |        8 |       1736 |
| innodb_table_stats        |        6 |        289 |
| ndb_binlog_index          |       12 |          0 |
| plugin                    |        2 |          0 |
| proc                      |       20 |         48 |
| procs_priv                |        8 |          0 |
| proxies_priv              |        7 |          1 |
| server_cost               |        4 |          6 |
| servers                   |        9 |          0 |
| slave_master_info         |       25 |          0 |
| slave_relay_log_info      |        9 |          0 |
| slave_worker_info         |       13 |          0 |
| slow_log                  |       12 |          0 |
| tables_priv               |        8 |          1 |
| time_zone                 |        2 |       1811 |
| time_zone_leap_second     |        2 |          0 |
| time_zone_name            |        2 |       1811 |
| time_zone_transition      |        3 |     121792 |
| time_zone_transition_type |        5 |       8771 |
| user                      |       45 |          2 |
+---------------------------+----------+------------+
31 rows in set.

# 

在一刹那,我们知道自己重复制造了一个既不好看也不是很好用的轮子。接下来我们来看一下这个已经有了的轮子常见的功能。

列出mysql中数据库信息

# mysqlshow -uroot -pliumiaocn
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| devops             |
| mysql              |
| performance_schema |
| redmine            |
| sonarqube          |
| sys                |
+--------------------+
#

查询包含的表的信息

类似与show tables,可以查询指定库包含那些表的信息

# mysqlshow -uroot -pliumiaocn mysql
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: mysql
+---------------------------+
|          Tables           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
# 

查询表中某一列的详细信息

# mysqlshow -uroot -pliumiaocn mysql user Host
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: mysql  Table: user  Wildcard: Host
+-------+----------+-----------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type     | Collation | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+----------+-----------+------+-----+---------+-------+---------------------------------+---------+
| Host  | char(60) | utf8_bin  | NO   | PRI |         |       | select,insert,update,references |         |
+-------+----------+-----------+------+-----+---------+-------+---------------------------------+---------+
# 

显示数据库的统计信息

前面我们使用mysqlshow -uroot -pliumiaocn mysql --count命令统计了mysql的内置mysql数据库的统计信息,如果不指定数据库,则是统计所有库的整体信息,具体执行示例如下所示:

# mysqlshow -uroot -pliumiaocn --count
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
+--------------------+--------+--------------+
|     Databases      | Tables |  Total Rows  |
+--------------------+--------+--------------+
| information_schema |     61 |        23880 |
| devops             |    140 |          874 |
| mysql              |     31 |       138969 |
| performance_schema |     87 |        43576 |
| redmine            |     55 |          274 |
| sonarqube          |     45 |         4813 |
| sys                |    101 |        10144 |
+--------------------+--------+--------------+
7 rows in set.
# 
  • 显示到表级别的统计信息
# mysqlshow -uroot -pliumiaocn mysql user --count
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: mysql  Table: user  Rows: 2
+------------------------+-----------------------------------+-----------------+------+-----+-----------------------+-------+---------------------------------+---------+
| Field                  | Type                              | Collation       | Null | Key | Default               | Extra | Privileges                      | Comment |
+------------------------+-----------------------------------+-----------------+------+-----+-----------------------+-------+---------------------------------+---------+
| Host                   | char(60)                          | utf8_bin        | NO   | PRI |                       |       | select,insert,update,references |         |
| User                   | char(32)                          | utf8_bin        | NO   | PRI |                       |       | select,insert,update,references |         
...省略
| account_locked         | enum('N','Y')                     | utf8_general_ci | NO   |     | N                     |       | select,insert,update,references |         |
+------------------------+-----------------------------------+-----------------+------+-----+-----------------------+-------+---------------------------------+---------+
# 

显示索引相关信息:-k

使用-k选项可以确认索引相关信息,以下为显示user表的索引信息的示例:

# mysqlshow -uroot -pliumiaocn mysql user -k
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: mysql  Table: user
+------------------------+-----------------------------------+-----------------+------+-----+-----------------------+-------+---------------------------------+---------+
| Field                  | Type                              | Collation       | Null | Key | Default               | Extra | Privileges                      | Comment |
+------------------------+-----------------------------------+-----------------+------+-----+-----------------------+-------+---------------------------------+---------+
| Host                   | char(60)                          | utf8_bin        | NO   | PRI |                       |       | select,insert,update,references |         |
| User                   | char(32)                          | utf8_bin        | NO   | PRI |                       |       | select,insert,update,references |         |
| Select_priv            | enum('N','Y')                     | utf8_general_ci | NO   |     | N                     |       | select,insert,update,references |         |
| Insert_priv            | enum('N','Y')                     | utf8_general_ci | NO   |     | N                     |       | select,insert,update,references |         |
| Update_priv            | enum('N','Y')                     | utf8_general_ci | NO   |     | N                     |       | select,insert,update,references |         |
| Delete_priv            | enum('N','Y')                     | utf8_general_ci 
...省略
| password_lifetime      | smallint(5) unsigned              |                 | YES  |     |                       |       | select,insert,update,references |         |
| account_locked         | enum('N','Y')                     | utf8_general_ci | NO   |     | N                     |       | select,insert,update,references |         |
+------------------------+-----------------------------------+-----------------+------+-----+-----------------------+-------+---------------------------------+---------+
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user  | 0          | PRIMARY  | 1            | Host        | A         |             |          |        |      | BTREE      |         |               |
| user  | 0          | PRIMARY  | 2            | User        | A         | 2           |          |        |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
# 

可以看出Host和User两个字段是BTREE类型的索引(因为是主键的原因)。

显示表的状态信息:-i

使用-i选项可以确认表的状态信息,可以指定到库,也可以指定到表。指定到库用于显示所有表的信息,以下指定到表作为显示例子:

# mysqlshow -uroot -pliumiaocn mysql user -i
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
Database: mysql  Wildcard: user
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------+----------+----------------+-----------------------------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time         | Check_time | Collation | Checksum | Create_options | Comment                     |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------+----------+----------------+-----------------------------+
| user | MyISAM | 10      | Dynamic    | 2    | 120            | 240         | 281474976710655 | 4096         | 0         |                | 2018-10-28 20:58:59 | 2019-03-11 14:22:54 |            | utf8_bin  |          |                | Users and global privileges |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+-----------+----------+----------------+-----------------------------+
# 

你可能感兴趣的:(#,MySQL)