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

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

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

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

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)
mysql> nopager; //恢复之前的输出状态
PAGER set to stdout
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。

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值。

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
04-28 03:01