IT序号网

mysql操作命令梳理(3)-pager

wyy 2021年06月14日 数据库 339 0

在mysql日常操作中,妙用pager设置显示方式,可以大大提高工作效率。比如select出来的结果集超过几个屏幕,那么前面的结果一晃而过无法看到,这时候使用pager可以设置调用os的more或者less等显示查询结果,和在os中使用more或者less查看大文件的效果一样。

pager用法:
实际上等于将它设置以后的所有mysql操作命令的输出通过pager设置命令执行,类似于管道符的作用

nopager命令取消pager设置,恢复之前的输出状态。(如果不设置nopager,那么只能通过重启mysql服务才能恢复了)

举些例子来说明吧:
1)当处理大量数据时,不想显示查询的结果,而只需知道查询花费的时间。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mysql> select * from huanqiu.haha;
+ ----+------------+
| id | name       |
+ ----+------------+
|  1 | wangshibo  |
|  2 | wangshikui |
|  3 | wangjuan   |
|  4 | wangman    |
| 11 | wangshikui |
+ ----+------------+
5 rows in set (0.00 sec)
mysql> pager cat /dev/ null ;        //实际上等于后面执行的命令|cat /dev/ null ,这样显示结果就只是执行时间了
PAGER set to 'cat /dev/null'
mysql> select * from huanqiu.haha; 
5 rows in set (0.00 sec)<br>
mysql> nopager;                   //恢复之前的输出状态
PAGER set to stdout<br>
mysql> select * from huanqiu.haha;
+ ----+------------+
| id | name       |
+ ----+------------+
|  1 | wangshibo  |
|  2 | wangshikui |
|  3 | wangjuan   |
|  4 | wangman    |
| 11 | wangshikui |
+ ----+------------+
5 rows in set (0.00 sec)

2)如果有大量连接,用show processlist看不方便,想看有多少Sleep状态,则可以用pager。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> show processlist;
+ ------+-------+---------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
| Id   | User  | Host                | db     | Command     | Time | State                                                                 | Info             |
+ ------+-------+---------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
|    5 | root  | localhost           | huanpc | Query       |    0 | init                                                                  | show processlist |
| 1801 | slave | 192.168.1.102:37125 | NULL   | Binlog Dump | 9904 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |
+ ------+-------+---------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
 
mysql> pager grep Sleep |wc -l;    
PAGER set to 'grep Sleep |wc -l'
mysql> show processlist;           //类似于show processlist结果再通过grep Sleep |wc -l显示;下面表示一共有2个连接,其中0个Sleep状态的连接。
0
2 rows in set (0.00 sec)
mysql> nopager;                    //恢复之前的输出状态

3)设置pager,只查看slave状态的几个status值。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
mysql> show slave status \G;              //其中的\G表示显示要换行显示
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.1.101
                   Master_User: slave
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: mysql-bin.000001
           Read_Master_Log_Pos: 5370489
                Relay_Log_File: mysql-relay-bin.000005
                 Relay_Log_Pos: 2476520
         Relay_Master_Log_File: mysql-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
               Replicate_Do_DB: huanqiu,huanpc
           Replicate_Ignore_DB: mysql
            Replicate_Do_Table:
        Replicate_Ignore_Table:
       Replicate_Wild_Do_Table:
   Replicate_Wild_Ignore_Table:
                    Last_Errno: 0
                    Last_Error:
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 5370489
               Relay_Log_Space: 2476693
               Until_Condition: None
                Until_Log_File:
                 Until_Log_Pos: 0
            Master_SSL_Allowed: No
            Master_SSL_CA_File:
            Master_SSL_CA_Path:
               Master_SSL_Cert:
             Master_SSL_Cipher:
                Master_SSL_Key:
         Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                 Last_IO_Errno: 0
                 Last_IO_Error:
                Last_SQL_Errno: 0
                Last_SQL_Error:
   Replicate_Ignore_Server_Ids:
              Master_Server_Id: 101
                   Master_UUID: b667a58f-d6e0-11e6-8c0a-fa163e2d66ac
              Master_Info_File: /data/mysql/data/master.info
                     SQL_Delay: 0
           SQL_Remaining_Delay: NULL
       Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
            Master_Retry_Count: 86400
                   Master_Bind:
       Last_IO_Error_Timestamp:
      Last_SQL_Error_Timestamp:
                Master_SSL_Crl:
            Master_SSL_Crlpath:
            Retrieved_Gtid_Set:
             Executed_Gtid_Set:
                 Auto_Position: 0
1 row in set (0.00 sec)
 
ERROR:
No query specified
 
mysql> pager cat | egrep -i 'system user|Exec_Master_Log_Pos|Seconds_Behind_Master|Read_Master_Log_Pos' ;
PAGER set to 'cat | egrep -i ' system user |Exec_Master_Log_Pos|Seconds_Behind_Master|Read_Master_Log_Pos ''
 
mysql> show slave status \G;
           Read_Master_Log_Pos: 5370489
           Exec_Master_Log_Pos: 5370489
         Seconds_Behind_Master: 0
1 row in set (0.00 sec)
 
ERROR:
No query specified
 
mysql> nopager;                 //恢复之前的显示状态
PAGER set to stdout

评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!