2010-07-07 80 views
1

我想獲得連接到mysql服務器的主機列表。我怎麼能得到這個? 連接到mysql服務器後應該怎麼做。如何獲取連接到mysql服務器的主機列表

代碼片段確實有幫助。

什麼是最好的API用於連接到MySQL使用C + +?你可以做到這一點

回答

3

的一種方法是執行查詢show processlist,這會給你IdUserHostdbCommandTimeStateInfo列的表。請記住,您的show processlist查詢將成爲輸出的一部分。

+0

,「SHOW PROCESSLIST」查詢WIL我是輸出的一部分,你的意思是可以有更多的主機嗎?在你的意思是它只會讓一部分主機連接? 運行「顯示進程列表」並查詢唯一的「主機」是否會這樣做? – AMM 2010-07-07 07:08:20

+0

是的,可以有任何主機,具體取決於您在運行查詢時連接到MySQL實例的不同客戶端計算機的數量。查詢將返回_all_進程,包括你的(這是我的觀點)。 'Host'字段顯示連接到進程的客戶端的主機名,所以如果你查詢一個唯一的主機,你將只得到該主機的結果,是的。 – 2010-07-07 07:22:01

0

您可以嘗試此查詢:select distinct host from information_schema.processlist; 例如,從10.9.0.10和一個本地連接有多個連接。

mysql> select distinct host from information_schema.processlist; +-----------------+ | host | +-----------------+ | 10.9.0.10:63668 | | 10.9.0.10:63670 | | 10.9.0.10:63664 | | 10.9.0.10:63663 | | 10.9.0.10:63666 | | 10.9.0.10:63672 | | 10.9.0.10:63665 | | 10.9.0.10:63671 | | 10.9.0.10:63669 | | 10.9.0.10:63667 | | localhost | | | +-----------------+ 12 rows in set (0,00 sec) 如果你只想主機(不不同的連接),你可以嘗試這樣的事:select distinct substring_index(host,':',1) from information_schema.processlist; 例子: mysql> select distinct substring_index(host,':',1) from information_schema.processlist; +-----------------------------+ | substring_index(host,':',1) | +-----------------------------+ | 10.9.0.10 | | localhost | | | +-----------------------------+ 3 rows in set (0,00 sec) 你可以看到,MySQL的顯示我一個空行,這是正常的(我有一個守護程序進程): mysql> select distinct substring_index(host,':',1),`command` from information_schema.processlist; +-----------------------------+---------+ | substring_index(host,':',1) | command | +-----------------------------+---------+ | 10.9.0.10 | Sleep | | localhost | Query | | | Daemon | +-----------------------------+---------+ 您可以where `command`!="Daemon"where `host`!='' 刪除它,這裏是查詢很好的鏈接也算從主機和顯示哪些用戶連接連接:http://blog.shlomoid.com/2011/08/how-to-easily-see-whos-connected-to.html當你說

相關問題