MySQL · 工具使用 · MySQL client pager/edit/tee 介绍

Author: baotiao

我们日常使用的MySQL client 有用的命令使用介绍

  1. pager

pager + 任何命令

常用的比如:

pager grep ‘Pending normal aio reads’

就可以执行show engine innodb status 以后只看grep 的内容

```

  1. mysql> pager grep 'Pending normal aio reads'
  2. PAGER set to 'grep 'Pending normal aio reads''
  3. mysql> show engine innodb status\G
  4. Pending normal aio reads: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] , aio writes: [256, 256, 256, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ,
  5. 1 row in set (0.00 sec) ```

pager less

那么执行show engine innodb status 以后直接less 查看结果

pager vim -

然后执行show engine innodb status 就可以直接进入到vim 里面编辑执行结果

关闭pager 就是执行 nopager 或者 \n 就可以

\P 又重新恢复上一个pager 的设置

还有一些骚操作 比如:

  • 如果我只想要看执行的时间, 不想要看具体的结果, 这么多次执行可以在同一个屏幕里面显示, 那么可以执行

```

  1. mysql> pager cat > /dev/null
  2. PAGER set to 'cat > /dev/null'

  3. Trying an execution plan

  4. mysql> SELECT ...
  5. 1000 rows in set (0.91 sec)

  6. Another execution plan

  7. mysql> SELECT ...
  8. 1000 rows in set (1.63 sec) ```
  • 比如我要对比两次查询的结果是否一致, 那么可以通过md5 命令来进行对比

    ```

    1. mysql> pager md5sum
    2. PAGER set to 'md5sum'

    3. Original query

    4. mysql> SELECT ...
    5. 32a1894d773c9b85172969c659175d2d -
    6. 1 row in set (0.40 sec)

    7. Rewritten query - wrong

    8. mysql> SELECT ...
    9. fdb94521558684afedc8148ca724f578 -
    10. 1 row in set (0.16 sec) ```

比如最常用的show processlist 里面, 也可以使用pager 去查有多少个sleep 的线程

```

  1. mysql> pager grep Sleep | wc -l
  2. PAGER set to 'grep Sleep | wc -l'
  3. mysql> show processlist;
  4. 337
  5. 346 rows in set (0.00 sec)

  6. 或者可以写的更复杂一些, 统计所有的.

  7. mysql> pager awk -F '|' '{print $6}' | sort | uniq -c | sort -r
  8. PAGER set to 'awk -F '|' '{print $6}' | sort | uniq -c | sort -r'
  9. mysql> show processlist;
  10. 309 Sleep
  11. 3
  12. 2 Query
  13. 2 Binlog Dump
  14. 1 Command ```

pager 后面也可以用写一个脚本来承接, 更骚

```

  1. !/bin/sh

  2. grep -A 1 'TRX HAS BEEN WAITING' ```

把这个脚本保存在 /tmp/lock_waits 上, 那么就可以过滤show engine innodb status 里面 trx wait 的

```

  1. mysql> pager /tmp/lock_waits
  2. PAGER set to '/tmp/lock_waits'
  3. mysql> show innodb status\G
  4. ------- TRX HAS BEEN WAITING 50 SEC FOR THIS LOCK TO BE GRANTED:
  5. RECORD LOCKS space id 0 page no 52 n bits 72 index GEN_CLUST_INDEX of table test/t trx id 0 14615 lock_mode X waiting
  6. 1 row in set, 1 warning (0.00 sec) ```

当然还有更复杂的, 把explain 的结果进行更详细的展示的 2. edit

edit 命令能够把你上一句命令放在vim 编辑器里面进行编辑, 然后再执行

```

  1. mysql> select count(*) from film left join film_category using(film_id) left join category using(category_id) where name='Music'; ```

然后执行 edit 命令, 就进入到vim 终端编辑了 3. tee

把执行的结果输出到另外一个文件里面

```

  1. mysql> tee queries.log
  2. Logging to file 'queries.log'
  3. mysql> use sakila
  4. Reading table information for completion of table and column names
  5. You can turn off this feature to get a quicker startup with -A

  6. Database changed

  7. mysql> select count(*) from sakila;
  8. ERROR 1146 (42S02): Table 'sakila.sakila' doesn't exist
  9. mysql> select count(*) from film;
  10. +----------+
  11. | count(*) |
  12. +----------+
  13. | 1000 |
  14. +----------+
  15. 1 row in set (0.00 sec)

  16. mysql> exit ```

原文:http://mysql.taobao.org/monthly/2023/09/02/