有朋友问怎么去看连接MySQL数据库有哪些用户及对应的IP地址等等信息,所以顺便整理了下这块内容,下面是涉及的一些命令。
## 查看当前连接到数据库的用户和Host ## SELECT DISTINCT USER,HOST FROM `information_schema`.`PROCESSLIST` P WHERE P.USER NOT IN('root','repl','system user');
SELECT * FROM performance_schema.hosts;
PS1: 系统表performance_schema.hosts在MySQL 5.6.3版本中引入,用来保存MySQL服务器启动后的连接情况。
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 );
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);