在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