概述
有朋友問怎么去看連接MySQL數據庫有哪些用戶及對應的IP地址等等信息,所以順便整理了下這塊內容,下面是涉及的一些命令。
1、查看當前連接到數據庫的用戶和Host
## 查看當前連接到數據庫的用戶和Host ## SELECT DISTINCT USER,HOST FROM `information_schema`.`PROCESSLIST` P WHERE P.USER NOT IN('root','repl','system user');
2、查看每個host的當前連接數和總連接數
SELECT * FROM performance_schema.hosts;
PS1: 系統表performance_schema.hosts在MySQL 5.6.3版本中引入,用來保存MySQL服務器啟動后的連接情況。
3、按照登錄用戶+登錄服務器查看登錄信息
SELECT USER AS login_user, LEFT ( HOST, POSITION( ':' IN HOST ) - 1 ) AS login_ip, count( 1 ) AS login_count FROM `information_schema`.`PROCESSLIST` P WHERE P.USER NOT IN ( 'root', 'repl', 'system user' ) GROUP BY USER, LEFT ( HOST, POSITION( ':' IN HOST ) - 1 );
4、按照登錄用戶+數據庫+登錄服務器查看登錄信息
SELECT DB as database_name, USER as login_user, LEFT(HOST,POSITION(':' IN HOST)-1) AS login_ip, count(1) as login_count FROM `information_schema`.`PROCESSLIST` P WHERE P.USER NOT IN('root','repl','system user') GROUP BY DB,USER,LEFT(HOST,POSITION(':' IN HOST)-1);