MySQL用戶端輸出格式化神奇現象

來源:互聯網
上載者:User

MySQL用戶端輸出格式化神奇現象,以前沒注意到,在此分享一下:
$ mysql -uroot -e “show global status like ‘Uptime’”
+—————+———+
| Variable_name | Value |
+—————+———+
| Uptime | 2174838 |
+—————+———+

$ mysql -uroot -e “show global status like ‘Uptime’” > /tmp/test
[mysql@dev81 ~]$ cat /tmp/test
Variable_name Value
Uptime 2174855

發現奇怪的地方了嗎?格式化的那些符號都不見了!同樣的命令,重新導向到檔案之後,輸出變了!
+—————+———+
| Variable_name | Value |
+—————+———+
| Uptime | 2174838 |
+—————+———+
變成了
Variable_name Value
Uptime 2174855

想了很長時間,沒想明白。問了下曲山,他二話不說開始用strace,馬上就發現了奧妙所在。
$ strace mysql -uroot -e “show global status like ‘Uptime’”
……
……
write(1, “+—————+———+\n”, 28+—————+———+
) = 28
write(1, “| Variable_name | Value |\n”, 28| Variable_name | Value |
) = 28
write(1, “+—————+———+\n”, 28+—————+———+
) = 28
write(1, “| Uptime | 2175430 | \n”, 29| Uptime | 2175430 |
) = 29
write(1, “+—————+———+\n”, 28+—————+———+
) = 28
poll([{fd=3, events=POLLIN|POLLPRI}], 1, 0) = 0
write(3, “\1\0\0\0\1″, 5) = 5
shutdown(3, 2 /* send and receive */) = 0
close(3) = 0
exit_group(0) = ?
Process 24577 detached

$ strace mysql -uroot -e “show global status like ‘Uptime’” > /tmp/test
……
……
write(1, “Variable_name\tValue\nUptime\t21755″…, 35) = 35
exit_group(0) = ?
Process 24581 detached

針對不同的輸出渠道,MySQL的用戶端似乎有方法可以捕捉到並採取不同的格式化輸出策略。不過MySQL為什麼要這樣做的原因還是不知道,估計只有天知道了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.