使用MySQL提供的内置工具可以方便地查看到当前所有正在执行的进程和线程,并且能够查询和检视每个进程或线程的详细信息,也就是说可以很容易的找到占用表的进程。具体的操作如下:
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 8.0.27 MySQL Community Server - GPL mysql>show processlist; +------+-----------------+------------------+---------------+---------+------+----------------------+ | Id | User | Host | db | Command | Time | State | +------+-----------------+------------------+---------------+---------+------+----------------------+ | 2815 | system user | | NULL | Daemon | NULL | Waiting for next activation | | 2814 | system user | | NULL | Daemon | NULL | Waiting for next activation | | 2813 | system user | | NULL | Daemon | NULL | Waiting for next activation | | 2812 | system user | | NULL | Daemon | NULL | Waiting for next activation | | 2811 | system user | | NULL | Daemon | NULL | Waiting for next activation | | 2736 | root | localhost | employees | Query | 0 | executing | | 2836 | root | localhost | | Query | 0 | starting | | 2837 | root | localhost | | Query | 0 | Sleep | +------+-----------------+------------------+---------------+---------+------+----------------------+ 8 rows in set (0.00 sec)
可以看到,执行了show processlist命令之后,大家就能够看到当前所有正在执行的进程和线程了。其中,db显示该进程当前正在操作的数据库,而State则显示进程当前的状态。如果一个进程的状态一直为“executing”,则说明该进程一直在占用某张表。
如果要查看某个正在占用的进程或者线程的详细信息,可以使用如下命令:
mysql>select * from information_schema.processlist where id=进程id;
其中,进程id为查看到的某个进程的ID,会显示该进程执行的具体SQL语句等详细信息。