Saturday 14 January 2012

Killing a MySQL User Session

When you login to MySQL, you are given a connection id. In the example below, this is 40:
 
UNIX > mysql -u andrew -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.5.19 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql>
 
root can see this as the id column in the output from show processlist:
 
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
 
mysql> show processlist;
+----+--------+-----------+------+---------+------+-------+------------------+
| Id | User   | Host      | db   | Command | Time | State | Info             |
+----+--------+-----------+------+---------+------+-------+------------------+
| 40 | andrew | localhost | NULL | Sleep   | 2327 |       | NULL             |
| 41 | root   | localhost | NULL | Query   |    0 | NULL  | show processlist |
+----+--------+-----------+------+---------+------+-------+------------------+
2 rows in set (0.00 sec)
 
mysql>
 
... and root can use it to kill the session:
 
mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
 
mysql> kill 40;
Query OK, 0 rows affected (0.00 sec)
 
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 41 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
 
mysql>
 
When the user accesses his session, he is to told that it has lost its connection. He is then allowed to reconnect without supplying a password and is given a new connection id:
 
mysql> use test;
No connection. Trying to reconnect...
Connection id:    42
Current database: *** NONE ***
 
Database changed
mysql>

No comments: