📫 作者简介:「六月暴雪飞梨花」,专注于研究Java,就职于科技型公司后端工程师
🏆 近期荣誉:华为云云享专家、阿里云专家博主、
🔥 三连支持:欢迎 ❤️关注、👍点赞、👉收藏三连,支持一下博主~
文章目录
引言
我是一名开发运维工程师
,之前就有看到过有人说开发与运维就是挖坑和填坑的关系」。开发和运维之间确实存在密切的关系,但是就上述观点有失公平,过于片面。这样子说,会给所有开发者一个先入为主的不好印象,误导项目团队成员认为开发只是在制造问题,而运维只是在解决问题。实际上,开发工程师和运维工程师应该是协作的关系,只不过开发工程师交付在前,运维工程师维护在后。
无论做什么运维,运维工程师最基本的职责都是负责服务的稳定性,业务的稳定性。确保服务可以7*24H
不间断地为用户提供持续不断的服务。所以,我们有了一个响当当的名号:运维侠 。
写作背景
在此之上运维工程师的主要工作职责是质量、时间(效率)、成本,三者的构成一个稳固的三角形,使得服务稳定输出,得到客户的好评。有些工作对于开发工程师和运维工程师来说,可能是有交叉。区别还是有差异的,例如本篇要讲的运维命令——mysqlshow
,而站在开发工程师的角度来说,使用 mysql show xxx……;
或者 show xxx;
的概率比较多。
运维命令mysqlshow
1、什么是mysqlshow
如果作为一个运维工程师还不了解这个命令,那么我建议读者朋友可以移步到这里来详细看下这个命令的使用方式方法(本篇在使用时,使用mysql 8.0讲解):https://dev.mysql.com/doc/refman/8.0/en/mysqlshow.html
我的英语水平比较差,将就的翻译下,mysqlshow客户端可用于快速查看存在数据库、表、列和索引。这就有些像我们开发同事经常使用的登录到数据库,使用的show xxx;
命令。
2、如何使用mysqlshow
官方网给予了很明确的使用方式:
$ mysqlshow [options] [db_name [tbl_name [col_name]]]
$
当前我的命令演示所在的版本:
$ mysqlshow --version
mysqlshow Ver 8.0.31 for macos13.0 on x86_64 (Homebrew)
$
注意⚠️:输出仅显示当前使用的帐号拥有某些权限的那些数据库、表或列的名称。下面所有的操作并不是详细解说,我罗列我认为在使用过程中出现频率比较高的或者以使用为目的来演示。
(1)参数选项
-
options:
如果不知道如何使用选项命令,可以使用查询帮助方法来了解。例如mysqlshow options --help
,具体可以参考 附录1。 -
db_name:数据库名称
-
tbl_name:表名称
-
col_name:列名称
(2)显示数据库
显示帐号下可以查看到的数据库/数据实例。
$ mysqlshow
mysqlshow: Access denied for user 'mysql'@'localhost' (using password: YES)
我本地数据库有密码,所以此时提示我需要输入密码。
$ mysqlshow -u root -p
Enter password:
+---------------------+
| Databases |
+---------------------+
| db_learn |
| flowable_learn |
| information_schema |
| mysql |
| performance_schema |
| sys |
| wefe_board |
| wefe_data_fusion_1 |
| wefe_serving |
| xxl_job |
+---------------------+
$
当我使用管理员帐号root输入密码后,我可以看到当前帐号下的数据库。当我使用子帐号test01查询时,只能查看自己帐号权限下的数据库。
$ mysqlshow -u test01 -p
Enter password:
+---------------------+
| Databases |
+---------------------+
| db_learn |
| flowable_learn |
| information_schema |
| mysql |
| performance_schema |
| sys |
+---------------------+
$
如果作为开发工程师,那么此时可能需要使用的命令是 show databases;
。在输出结果,或者说结果导向上是一致的,思路上略有转变。
(3)显示数据库下的表
数据库db_learn
下所拥有的表。
$ mysqlshow -u root -p db_learn
Enter password:
Database: db_learn
+--------------------------+
| Tables |
+--------------------------+
| Activity |
| customers |
| customers_bak20230904 |
| dc_obj |
| dc_obj_attr |
| it_teacher |
| Point |
| query_activity |
| regions |
| shirts |
| Student |
| t1 |
| t_sss |
| Test |
| TestAttempt |
| Triangle |
| tt |
| tt_from |
| tt_subComments |
+--------------------------+
Aion@Macbook ~ $
当然,如果你看到了这里,那么恭喜你。你应可以学会了举一反三。使用show tables;
来做一个替换。
(4)显示表下的列
数据库db_learn下的Activity表有如下列,其中展示的列名称、数据存储类型、是否为空、是否主键、默认值、扩展、权限、备注说明等信息。
$ mysqlshow -u root -p db_learn Activity
Enter password:
Database: db_learn Table: Activity
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
| player_id | int | | NO | PRI | | | select,insert,update,references | |
| device_id | int | | NO | | | | select,insert,update,references | |
| event_date | date | | NO | PRI | | | select,insert,update,references | |
| games_played | int | | NO | | | | select,insert,update,references | |
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
$
(5)显示表/库的行数
如果使用--count
来来放在数据库后面,则统计数据库中所有的表行数。展示结果与增加特定表不同。此处则展示表、表的列数量、表的数据行数(总记录数)。
Aion@Macbook ~ $ mysqlshow -u root -p db_learn --count
Enter password:
Database: db_learn
+--------------------------+----------+------------+
| Tables | Columns | Total Rows |
+--------------------------+----------+------------+
| Activity | 4 | 99 |
| customers | 4 | 10 |
| customers_bak20230904 | 4 | 10 |
| dc_obj | 76 | 477 |
| dc_obj_attr | 62 | 8572 |
| Point | 4 | 1000 |
| query_activity | 2 | 99 |
| regions | 2 | 2 |
| shirts | 2 | 3 |
| Student | 2 | 100 |
| t1 | 2 | 0 |
| t_sss | 3 | 4 |
| Test | 2 | 10 |
| TestAttempt | 4 | 10000 |
| Triangle | 4 | 10000 |
| tt | 19 | 0 |
| tt_from | 5 | 0 |
| tt_subComments | 3 | 0 |
+--------------------------+----------+------------+
17 rows in set.
如果使用--count
来放在表后面,则统计表的行数。表Activity显示结果为有99行数据。
$ mysqlshow -u root -p db_learn Activity --count
Enter password:
Database: db_learn Table: Activity Rows: 99
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
| player_id | int | | NO | PRI | | | select,insert,update,references | |
| device_id | int | | NO | | | | select,insert,update,references | |
| event_date | date | | NO | PRI | | | select,insert,update,references | |
| games_played | int | | NO | | | | select,insert,update,references | |
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
$
注意⚠️:
1⃣️显示每个表的行数。对于非MyISAM表,这可能会很慢。
2⃣️此时,也相信独具慧眼的你,发现了端倪,我是将命令属性从多减少了,如果我将数据库db_learn去掉呢,你会得到意想不到的效果,那就是这里展示数据库的统计数据,展示数据为数据库名称,表数量,表中数据记录数,此处不在演示。
(6)显示库中表的类型
应用在数据库下,如果应用在表/视图下,则会被忽略。
$ mysqlshow -u root -p db_learn --show-table-type
Enter password:
Database: db_learn
+--------------------------+------------+
| Tables | table_type |
+--------------------------+------------+
| Activity | BASE TABLE |
| customers | BASE TABLE |
| customers_bak20230904 | BASE TABLE |
| dc_obj | BASE TABLE |
| dc_obj_attr | BASE TABLE |
| Point | BASE TABLE |
| query_activity | VIEW |
| regions | BASE TABLE |
| shirts | BASE TABLE |
| Student | BASE TABLE |
| t1 | BASE TABLE |
| t_sss | BASE TABLE |
| Test | BASE TABLE |
| TestAttempt | BASE TABLE |
| Triangle | BASE TABLE |
| tt | BASE TABLE |
| tt_from | BASE TABLE |
| tt_subComments | BASE TABLE |
+--------------------------+------------+
此时,我们可以看到,显示的类型为基础表(BASE TABLE)和视图(VIEW)两种。这个有点像开发工程师常常使用的命令:SHOW FULL TABLES;
。
(7)显示表的主键信息
使用-k/–keys都可以达到效果,将命令放在表前和表后,这个无关紧要。此时会显示表中表名称、是否唯一(0/1)、键类型名称、索引号、映射的列名称、索引类型、备注信息、索引备注信息等。
$ mysqlshow -u root -p db_learn -k Activity
Enter password:
Database: db_learn Table: Activity
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+--------------+------+-----------+------+-----+---------+-------+---------------------------------+---------+
| player_id | int | | NO | PRI | | | select,insert,update,references | |
| device_id | int | | NO | | | | select,insert,update,references | |
| event_date | date | | NO | PRI | | | select,insert,update,references | |
| games_played | int | | NO | | | | 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 | Visible | Expression |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Activity | 0 | PRIMARY | 1 | player_id | A | 100 | | | | BTREE | | | YES | |
| Activity | 0 | PRIMARY | 2 | event_date | A | 100 | | | | BTREE | | | YES | |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
(8)查询详细信息
这个命令--status/-i
很有意思,你可以认为这个是在查询information_schema数据库
中的表信息。虽然命令既不是简写,也不相同,但是所表达的意思相同,对于我们运维工程师来说获取的结果是相同的。一般情况下,我们可以使用这个命令做一些库数据统计或者表数据统计。可以着手从这里学习下。
$ mysqlshow -u root -p db_learn Activity -i
Enter password:
Database: db_learn Wildcard: Activity
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
| 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 |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
| Activity | InnoDB | 10 | Dynamic | 100 | 163 | 16384 | 0 | 0 | 0 | | 2023-05-28 18:10:44 | | | utf8mb4_0900_ai_ci | | | |
+----------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
Aion@Macbook ~ $
$ mysqlshow -u root -p db_learn -i
Enter password:
Database: db_learn
+--------------------------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+--------------------+-----------------------------+
| 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 |
+--------------------------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+--------------------+-----------------------------+
| Activity | InnoDB | 10 | Dynamic | 100 | 163 | 16384 | 0 | 0 | 0 | | 2023-05-28 18:10:44 | | | utf8mb4_0900_ai_ci | | | |
| customers | InnoDB | 10 | Dynamic | 10 | 1638 | 16384 | 0 | 0 | 0 | 11 | 2023-08-21 20:47:22 | | | utf8mb4_0900_ai_ci | | | |
| customers_bak20230904 | InnoDB | 10 | Dynamic | 10 | 1638 | 16384 | 0 | 0 | 0 | | 2023-09-04 15:13:40 | | | utf8mb4_0900_ai_ci | | | |
| dc_obj | InnoDB | 10 | Dynamic | 477 | 274 | 131072 | 0 | 196608 | 0 | | 2023-10-23 00:21:37 | | | utf8mb4_0900_ai_ci | | | 实体元模型 |
| dc_obj_attr | InnoDB | 10 | Dynamic | 8423 | 313 | 2637824 | 0 | 4030464 | 4194304 | | 2023-10-23 00:21:37 | | | utf8mb4_0900_ai_ci | | | 属性元模型 |
| Point | InnoDB | 10 | Dynamic | 1000 | 81 | 81920 | 0 | 0 | 0 | | 2023-05-28 18:12:47 | | | utf8mb4_0900_ai_ci | | | |
| query_activity | | | | | | | | | | | 2023-09-16 22:33:10 | | | | | | VIEW |
| regions | InnoDB | 10 | Dynamic | 2 | 8192 | 16384 | 0 | 0 | 0 | | 2023-03-07 09:19:55 | | | utf8mb4_0900_ai_ci | | | |
| shirts | InnoDB | 10 | Dynamic | 3 | 5461 | 16384 | 0 | 0 | 0 | | 2023-10-16 17:20:08 | | | utf8mb4_0900_ai_ci | | | |
| Student | InnoDB | 10 | Dynamic | 100 | 163 | 16384 | 0 | 0 | 0 | | 2023-05-25 10:37:26 | | | utf8mb4_0900_ai_ci | | | |
| t1 | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | | 2023-03-07 09:20:56 | | | utf8mb4_0900_ai_ci | | | |
| t_sss | InnoDB | 10 | Dynamic | 4 | 4096 | 16384 | 0 | 0 | 0 | | 2023-04-09 18:04:21 | | | utf8mb4_0900_ai_ci | | | |
| Test | InnoDB | 10 | Dynamic | 10 | 1638 | 16384 | 0 | 0 | 0 | | 2023-05-25 10:37:26 | | | utf8mb4_0900_ai_ci | | | |
| TestAttempt | InnoDB | 10 | Dynamic | 9921 | 41 | 409600 | 0 | 475136 | 0 | | 2023-05-25 10:37:26 | | | utf8mb4_0900_ai_ci | | | |
| Triangle | InnoDB | 10 | Dynamic | 9991 | 40 | 409600 | 0 | 835584 | 4194304 | | 2023-05-28 18:12:47 | | | utf8mb4_0900_ai_ci | | | |
| tt | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | 1 | 2023-10-23 00:21:10 | | | utf8mb4_0900_ai_ci | | | |
| tt_from | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | 1 | 2023-10-22 23:01:51 | | | utf8mb4_0900_ai_ci | | | |
| tt_subComments | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 16384 | 0 | 1 | 2023-10-22 23:01:51 | | | utf8mb4_0900_ai_ci | | | |
+--------------------------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+---------------------+------------+--------------------+----------+--------------------+-----------------------------+
附录1 optisons选项 原版/译版
$ mysqlshow options --help
mysqlshow Ver 8.0.31 for macos13.0 on x86_64 (Homebrew)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Shows the structure of a MySQL database (databases, tables, and columns).
Usage: mysqlshow [OPTIONS] [database [table [column]]]
If last argument contains a shell or SQL wildcard (*,?,% or _) then only
what's matched by the wildcard is shown.
If no database is given then all matching databases are shown.
If no table is given, then all matching tables in database are shown.
If no column is given, then all matching columns and column types in table
are shown.
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
The following groups are read: mysqlshow client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--defaults-group-suffix=#
Also read groups with concat(group, suffix)
--login-path=# Read this path from the login file.
--bind-address=name IP address to bind to.
-c, --character-sets-dir=name
Directory for character set files.
--default-character-set=name
Set the default character set.
--count Show number of rows per table (may be slow for non-MyISAM
tables).
-C, --compress Use compression in server/client protocol.
-#, --debug[=name] Output debug log. Often this is 'd:t:o,filename'.
--debug-check Check memory and open file usage at exit.
--debug-info Print some debug info at exit.
--default-auth=name Default authentication client-side plugin to use.
--enable-cleartext-plugin
Enable/disable the clear text authentication plugin.
-?, --help Display this help and exit.
-h, --host=name Connect to host.
-i, --status Shows a lot of extra information about each table.
-k, --keys Show keys for table.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's asked from the tty.
-,, --password1[=name]
Password for first factor authentication plugin.
-,, --password2[=name]
Password for second factor authentication plugin.
-,, --password3[=name]
Password for third factor authentication plugin.
--plugin-dir=name Directory for client-side plugins.
-P, --port=# Port number to use for connection or 0 for default to, in
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/services, built-in default (3306).
--protocol=name The protocol to use for connection (tcp, socket, pipe,
memory).
-t, --show-table-type
Show table type column.
-S, --socket=name The socket file to use for connection.
--server-public-key-path=name
File path to the server public RSA key in PEM format.
--get-server-public-key
Get server public key
--ssl-mode=name SSL connection mode.
--ssl-ca=name CA file in PEM format.
--ssl-capath=name CA directory.
--ssl-cert=name X509 cert in PEM format.
--ssl-cipher=name SSL cipher to use.
--ssl-key=name X509 key in PEM format.
--ssl-crl=name Certificate revocation list.
--ssl-crlpath=name Certificate revocation list path.
--tls-version=name TLS version to use, permitted values are: TLSv1.2,
TLSv1.3
--ssl-fips-mode=name
SSL FIPS mode (applies only for OpenSSL); permitted
values are: OFF, ON, STRICT
--tls-ciphersuites=name
TLS v1.3 cipher to use.
--ssl-session-data=name
Session data file to use to enable ssl session reuse
--ssl-session-data-continue-on-failed-reuse
If set to ON, this option will allow connection to
succeed even if session data cannot be reused.
-u, --user=name User for login if not current user.
-v, --verbose More verbose output; you can use this multiple times to
get even more verbose output.
-V, --version Output version information and exit.
--compression-algorithms=name
Use compression algorithm in server/client protocol.
Valid values are any combination of
'zstd','zlib','uncompressed'.
--zstd-compression-level=#
Use this compression level in the client/server protocol,
in case --compression-algorithms=zstd. Valid range is
between 1 and 22, inclusive. Default is 3.
Variables (--variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
----------------------------------------- --------------------------------
bind-address (No default value)
character-sets-dir (No default value)
default-character-set auto
count FALSE
compress FALSE
debug-check FALSE
debug-info FALSE
default-auth (No default value)
enable-cleartext-plugin FALSE
host (No default value)
status FALSE
keys FALSE
plugin-dir (No default value)
port 0
show-table-type FALSE
socket (No default value)
server-public-key-path (No default value)
get-server-public-key FALSE
ssl-ca (No default value)
ssl-capath (No default value)
ssl-cert (No default value)
ssl-cipher (No default value)
ssl-key (No default value)
ssl-crl (No default value)
ssl-crlpath (No default value)
tls-version (No default value)
tls-ciphersuites (No default value)
ssl-session-data (No default value)
ssl-session-data-continue-on-failed-reuse FALSE
user mysql
compression-algorithms (No default value)
zstd-compression-level 3
翻译版本:
--print-defaults # 打印程序参数列表并退出.
--no-defaults # 不要从任何选项文件读取默认选项,登录文件除外
--defaults-file=# # 仅从给定文件中读取默认选项
--defaults-extra-file=# # 读取全局文件后读取此文件
--defaults-group-suffix=# # 同时读取带有concat(group,suffix)的组
--login-path=# # 从登录文件读取此路径
--bind-address=name # 绑定的IP
-c, --character-sets-dir=name # 字符集的安装目录
--default-character-set=name # 设置默认字符集
--count # 显示每个表数据的行数(非MyISAM引擎的表可能比较慢).
-C, --compress # 在客户端和服务器之间启用压缩传递所有信息
-#, --debug[=#] # 输出debug信息,用于调试。默认值为:d:t,/tmp/mysqldump.trace
--debug-check # 当程序退出时打印一些调试信息
--debug-info # 当程序退出时打印调试信息和内存和CPU使用统计信息
--default-auth=name # 默认的客户端插件
--enable-cleartext-plugin # 启用/禁用明文身份验证插件
-?, --help # 显示帮助信息并退出
-h, --host=name # 指定连接的主机
-i, --status # 显示了关于每个表的大量额外信息
-k, --keys # 显示表的键
-p, --password[=name] # 指定登陆服务器的密码
--plugin-dir=name # 客户端插件的目录
-P, --port=# # 指定登陆的端口号
--protocol=name # 用于连接的协议 (tcp, socket, pipe,memory).
-t, --show-table-type # 显示表列类型
-S, --socket=name # 连接使用的socket文件
--secure-auth # 如果客户端使用旧的(4.1.1之前的)协议,则拒绝连接到服务器
--ssl-mode=name # SSL连接模式
--ssl-ca=name # PEM格式的CA文件
--ssl-capath=name # CA目录
--ssl-cert=name # PEM格式的X509证书
--ssl-cipher=name # 要使用的SSL密码
--ssl-key=name # PEM格式的X509密钥
--ssl-crl=name # 证书吊销列表
--ssl-crlpath=name # 证书吊销列表路径
--tls-version=name # 要使用的TLS版本,允许值为:tlsv1、tlsv1.1
-u, --user=name # 登陆服务器指定用户名
-v, --verbose # 更详细的输出;您可以多次使用它来获得更详细的输出
-V, --version # 输出版本信息并退出
附录2 v8.0+版本options说明和弃用
具体使用和弃用明细,大家可以参考:https://dev.mysql.com/doc/refman/8.0/en/mysqlshow.html
结束语
回到开头语那段文字,我们是否了解开发工程师和运维工程师之间的差异或者说工作的交叉点?理想情况下,开发团队(交付)和运维团队(实施)应该携手合作,转变思想观念,共同设计和实施系统,确保服务的稳定性和可用性。我们应该共同制定作业计划、确定优先级顺序、解决出现的问题,并互相尊重和信任。这样的合作关系可以促进更好的沟通和协作,提高系统的质量和效率。