MySQL Utilities之系统层面常用命令

1.1 mysqldiskusage

显示一个或多个数据库所使用磁盘空间大小,可显示二进制日志、慢查询日志、错误日志、查询日志、二进制中继日志、innodb表空间大小
读取文件系统失败,或者服务不在本地,那么将不能确定文件大小

选项:
- b , – binlog
- r , – relaylo
- l , – logs
- i , – innodb
- m , – empty
- a , – all

[root@zijie data]# mysqldiskusage --server=master57 --all -vvv
WARNING: Using a password on the command line interface can be insecure.
# Source on 127.0.0.1: ... connected.
# Database totals:
+---------------------+--------------+------------+-------------+-------------+
| db_name             | db_dir_size  | data_size  | misc_files  |      total  |
+---------------------+--------------+------------+-------------+-------------+
| test2               | 217,943      | 32,768     | 21,335      | 217,943     |
| mysql               | 12,190,570   | 350,863    | 271,125     | 12,190,570  |
| performance_schema  | 818,071      | 0          | 818,071     | 818,071     |
| sys                 | 484,338      | 0          | 386,034     | 0           |
| test                | 111,053      | 16,384     | 12,749      | 111,053     |
| zijie               | 217,943      | 32,768     | 21,335      | 217,943     |
+---------------------+--------------+------------+-------------+-------------+

Total database disk usage = 13,821,975 bytes or 13.18 MB

# Log information.
# The general_log is turned off on the server.
+------------+--------------+
| log_name   |        size  |
+------------+--------------+
| slow.log   | 171,047,421  |
| error.log  |   1,741,031  |
+------------+--------------+

Total size of logs = 172,788,452 bytes or 164.78 MB

# Binary log information:
Current binary log file = mybinlog.000022
+------------------+-------+
| log_file         | size  |
+------------------+-------+
| mybinlog.000021  | 3171  |
| mybinlog.000022  | 679   |
| mybinlog.index   | 74    |
+------------------+-------+

Total size of binary logs = 3,924 bytes or 3.83 KB

# Server is not an active slave - no relay log information.
# InnoDB tablespace information:
+--------------+----------------+--------------------+------------------------+
| innodb_file  |          size  | type               | specificaton           |
+--------------+----------------+--------------------+------------------------+
| ib_logfile0  | 1,073,741,824  | log file           |                        |
| ib_logfile1  | 1,073,741,824  | log file           |                        |
| ibdata1      | 1,073,741,824  | shared tablespace  | ibdata1:1G:autoextend  |
+--------------+----------------+--------------------+------------------------+

Total size of InnoDB files = 3,221,225,472 bytes or 3.00 GB

#...done.

1.2 mysqlfrm

恢复工具,读取frm文件并从中找到 视图定义语句
注意:不能读取到外键约束和自增长序列

再生实例模式:
指定-server选项连接到已经安装的实例。该过程不会改变原始frm文件,指定–port选项给再生实例使用,该端口不能与现有端口冲突,读取完frm文件后,再生实例会关闭,所有临时文件将被删除

[root@zijie data]# mysqlfrm --server=master57 --user=root --port=3310  /data/mysql57/data/test/t2.frm --show-stat
# Source on 127.0.0.1: ... connected.
# Spawning server with --user=root.
# Starting the spawned server on port 3310 ... done.
# Reading .frm files
#
# Reading the t2.frm file.
#
# CREATE statement for /data/mysql57/data/test/t2.frm:
#

CREATE TABLE `test`.`t2` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

# File Statistics:
#         Last Modified : Thu Jul 30 15:27:19 2020
#         Creation Time : Thu Jul 30 15:27:19 2020
#         Last Accessed : Thu Jul 30 15:27:19 2020
#                  Mode : 33184
#                  Size : 8586

# Table Statistics:
#                Engine : HEAP
#           frm Version : 10
#         MySQL Version : 5.7.29
#      frm File_Version : 5
#               IO_SIZE : 4096
#  Def Partition Engine : None

#...done.

诊断模式:
指定–diagnostic选项,byte-by-byte读取frm文件,尽可能多的恢复信息,该模式不能校验字符集

[root@zijie data]# mysqlfrm --diagnostic /data/mysql57/data/test/t2.frm --show-stats
# WARNING: Cannot generate character set or collation names without the --server option.
# CAUTION: The diagnostic mode is a best-effort parse of the .frm file. As such, it may not identify all of the components of the table correctly. This is especially true for damaged files. It will also not read the default values for the columns and the resulting statement may not be syntactically correct.
# Reading .frm file for /data/mysql57/data/test/t2.frm:
# The .frm file is a TABLE.
# CREATE TABLE Statement:

CREATE TABLE `test`.`t2` (
  `id` int(11) NOT NULL,
  `name` varchar(80) DEFAULT NULL,
PRIMARY KEY `PRIMARY` (`id`)
) ENGINE=InnoDB;

# File Statistics:
#         Last Modified : Thu Jul 30 14:24:12 2020
#         Creation Time : Thu Jul 30 14:24:12 2020
#         Last Accessed : Thu Jul 30 14:24:12 2020
#                  Mode : 33184
#                  Size : 8586

# Table Statistics:
#                Engine : INNODB
#           frm Version : 10
#         MySQL Version : 5.7.29
#      frm File_Version : 5
#               IO_SIZE : 4096
#  Def Partition Engine : None

#...done.

选项:
–quiet 仅显示create语句和警告、错误信息
–show-stats 统计frm文件信息

1.2.1 利用frm和ibd恢复数据

在某此情况下,数据库崩溃或者数据丢失,无法启动;除了常规的备份恢复,binlog闪回外,还可以利用备份的或者磁盘上的frm和ibd文件恢复数据,因为数据库存储的数据实际上还是存储在文件里的

myisam引擎:直接复制这三个文件即可使用,不需要恢复
xxx.frm 表结构文件
xxx.MYD 数据文件
xxx.MYI 索引文件
innodb引擎: innodb某些元数据是存储在 ibdata1文件中的
xxx.frm 表结构文件 -> 使用mysqlfrm获取
xxx.ibd 数据和索引文件 -> 覆盖文件

利用frm和ibd恢复数据操作流程

第一步:利用mysqlfrm工具恢复表结构
第二步:创建该表
第三步:卸载表空间

第四步:复制ibd文件

第五步:导入表空间

第六步:检查数据是否已恢复

1.3 mysqlindexcheck

[root@zijie test]# mysqlindexcheck --server=master57 zijie --show-drops --show-indexes --report-indexes
# Source on 127.0.0.1: ... connected.
# Showing indexes from zijie.t1:
#
+-----------+--------+----------+--------+----------+
| database  | table  | name     | type   | columns  |
+-----------+--------+----------+--------+----------+
| zijie     | t1     | PRIMARY  | BTREE  | id       |
+-----------+--------+----------+--------+----------+
#
# Showing indexes from zijie.t2:
#
+-----------+--------+-------------+--------+-----------+
| database  | table  | name        | type   | columns   |
+-----------+--------+-------------+--------+-----------+
| zijie     | t2     | PRIMARY     | BTREE  | id        |
| zijie     | t2     | idx_id      | BTREE  | id        |
| zijie     | t2     | idx_idname  | BTREE  | id, name  |
+-----------+--------+-------------+--------+-----------+
#
# The following indexes are duplicates or redundant for table zijie.t2:
#
CREATE INDEX `idx_id` ON `zijie`.`t2` (`id`) USING BTREE
#     may be redundant or duplicate of:
ALTER TABLE `zijie`.`t2` ADD PRIMARY KEY (`id`)
#
CREATE INDEX `idx_idname` ON `zijie`.`t2` (`id`, `name`) USING BTREE
#     may be redundant or duplicate of:
ALTER TABLE `zijie`.`t2` ADD PRIMARY KEY (`id`)
#
# DROP statements:
#
ALTER TABLE `zijie`.`t2` DROP INDEX `idx_id`;
ALTER TABLE `zijie`.`t2` DROP INDEX `idx_idname`;
#
# The following indexes for table zijie.t2 contain the clustered index and might be redundant:
#
CREATE INDEX `idx_id` ON `zijie`.`t2` (`id`) USING BTREE
#
CREATE INDEX `idx_idname` ON `zijie`.`t2` (`id`, `name`) USING BTREE
#
# DROP/ADD statements:
#
ALTER TABLE `zijie`.`t2` DROP INDEX `idx_id`;
ALTER TABLE `zijie`.`t2` DROP INDEX `idx_idname`, ADD INDEX `idx_idname` (name);
#
# Table `zijie`.`t_access` is not indexed.

识别重复的和潜在冗余的表索引
除了这些库的表mysql, INFORMATION_SCHEMA, performance_schema,可以扫描所有其他库

选项:
–show-drops 显示drop语句删除冗余索引
– show - indexes 显示每个表的索引

1.4 mysqlmetagrep

搜索数据库对象

[root@zijie test]# mysqlmetagrep --server=master57 -Gb --pattern="t1"
+------------------------+--------------+--------------+-----------+-------------+----------+
| Connection             | Object Type  | Object Name  | Database  | Field Type  | Matches  |
+------------------------+--------------+--------------+-----------+-------------+----------+
| root:*@127.0.0.1:3306  | TABLE        | t1           | zijie     | TABLE       | t1       |
+------------------------+--------------+--------------+-----------+-------------+----------+

1.5 mysqlprocgrep

  搜索线程,并执行某些操作

选项:
–age 指定时间
–match-xxx 指定匹配条件,xxx可以是 id user host db command info state
– kill - connection
– kill - query

[root@zijie test]# mysqlprocgrep --server=master57
+------------------------+------+-------+------------------+-------+-------------------+-------+----------------------------------------------------------------+------------------------------------------------------------------------------------------------+
| Connection             | Id   | User  | Host             | Db    | Command           | Time  | State
                                        | Info
                 |
+------------------------+------+-------+------------------+-------+-------------------+-------+----------------------------------------------------------------+------------------------------------------------------------------------------------------------+
| root:*@127.0.0.1:3306  | 135  | repl  | 127.0.0.1:36130  | None  | Binlog Dump GTID  | 8293  | Master has sent all binlog to slave; waiting for more updates  | None
                 |
| root:*@127.0.0.1:3306  | 270  | root  | 127.0.0.1:37838  | None  | Query             | 0     | executing
                                        | SELECT
  Id, User, Host, Db, Command, Time, State, Info
FROM
  INFORMATION_SCHEMA.PROCESSLIST  |
+------------------------+------+-------+------------------+-------+-------------------+-------+----------------------------------------------------------------+------------------------------------------------------------------------------------------------+

1.6 mysqluserclone

mysqluserclone --source=user:pass@host:port:socket --destination=user:pass@host:port:socket joe@localhost sam:secret1@localhost

以现有数据库上的用户作为模板创作一个或多个具有相同权限的账户

选项:
- d , – dump 显示grant语句并不执行
- l , – list 列出所有用户

[root@zijie test]# mysqluserclone --source=master57 --list -vvv
# Source on 127.0.0.1: ... connected.
# All Users:
+----------------+------------+---------------------+
| user           | host       | database            |
+----------------+------------+---------------------+
| mysql.session  | localhost  | performance_schema  |
| mysql.sys      | localhost  | sys                 |
| repl           | %          | None                |
| root           | %          | None                |
| test           | %          | None                |
| root           | 127.0.0.1  | None                |
| sysbench       | 127.0.0.1  | None                |
| root           | localhost  | None                |
| t1             | localhost  | None                |
+----------------+------------+---------------------+

[root@zijie test]# mysqluserclone --source=master57 --dump --list
# Source on 127.0.0.1: ... connected.
# All Users:
+----------------+------------+
| user           | host       |
+----------------+------------+
| repl           | %          |
| root           | %          |
| test           | %          |
| root           | 127.0.0.1  |
| sysbench       | 127.0.0.1  |
| mysql.session  | localhost  |
| mysql.sys      | localhost  |
| root           | localhost  |
| t1             | localhost  |
+----------------+------------+
# Dumping grants for user 'repl'@'%'
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'
# Dumping grants for user 'root'@'%'
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1'
# Dumping grants for user 'test'@'%'
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%'
# Dumping grants for user 'root'@'127.0.0.1'
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1'
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'
# Dumping grants for user 'sysbench'@'127.0.0.1'
GRANT ALL PRIVILEGES ON *.* TO 'sysbench'@'127.0.0.1'
# Dumping grants for user 'mysql.session'@'localhost'
GRANT SUPER ON *.* TO 'mysql.session'@'localhost'
GRANT SELECT ON `performance_schema`.* TO 'mysql.session'@'localhost'
GRANT SELECT ON `mysql`.`user` TO 'mysql.session'@'localhost'
# Dumping grants for user 'mysql.sys'@'localhost'
GRANT USAGE ON *.* TO 'mysql.sys'@'localhost'
GRANT TRIGGER ON `sys`.* TO 'mysql.sys'@'localhost'
GRANT SELECT ON `sys`.`sys_config` TO 'mysql.sys'@'localhost'
# Dumping grants for user 'root'@'localhost'
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1'
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'
# Dumping grants for user 't1'@'localhost'
GRANT RELOAD ON *.* TO 't1'@'localhost'

[root@zijie test]# mysqluserclone --source=root:970125@localhost:/data/mysql57/data/mysql57.sock --destination=root:970125@localhost:/data/mysql57slave/data/mysql57slave.sock zijie@localhost zijieslave:970125@% -vvv
WARNING: Using a password on the command line interface can be insecure.
# Source on localhost: ... connected.
# Destination on localhost: ... connected.
# Cloning 1 users...
# Cloning zijie@localhost to user zijieslave:970125@%
CREATE USER 'zijieslave'@'%' IDENTIFIED BY '970125'
GRANT USAGE ON *.* TO 'zijieslave'@'%'
GRANT ALL PRIVILEGES ON `zijie`.* TO 'zijieslave'@'%'
# ...done.

1.7 mysqluc

  命令行工具,允许执行用户当前安装mysql工具的任何命令 --utildir用于指定MySQL Utilities安装路径。支持tab 支持管道

[root@zijie test]# mysqluc -e "help utilities"
Launching console ...

Utility            Description
-----------------  --------------------------------------------------------
mysqlauditadmin    audit log maintenance utility
mysqlauditgrep     audit log search utility
mysqlbinlogmove    binary log relocate utility
mysqlbinlogpurge   purges unnecessary binary log files
mysqlbinlogrotate  rotates the active binary log file
mysqldbcompare     compare databases for consistency
mysqldbcopy        copy databases from one server to another
mysqldbexport      export metadata and data from databases
mysqldbimport      import metadata and data from files
mysqldiff          compare object definitions among objects where the
                   difference is how db1.obj1 differs from db2.obj2
mysqldiskusage     show disk usage for databases
mysqlfailover      automatic replication health monitoring and failover
mysqlfrm           show CREATE TABLE from .frm files
mysqlgrants        display grants per object
mysqlindexcheck    check for duplicate or redundant indexes
mysqlmetagrep      search metadata
mysqlprocgrep      search process information
mysqlreplicate     establish replication with a master
mysqlrpladmin      administration utility for MySQL replication
mysqlrplcheck      check replication
mysqlrplms         establish multi-source replication
mysqlrplshow       show slaves attached to a master
mysqlrplsync       replication synchronization checker utility
mysqlserverclone   start another instance of a running server
mysqlserverinfo    show server information
mysqlslavetrx      skip transactions on slaves
mysqluserclone     clone a MySQL user account to one or more new users

猜你喜欢

转载自blog.csdn.net/qq_42979842/article/details/107702933