67 lines
1.9 KiB
JavaScript
67 lines
1.9 KiB
JavaScript
|
|
/**
|
||
|
|
* 快速批量终止锁定的MySQL进程
|
||
|
|
* 无需确认 - 直接终止所有锁定的进程
|
||
|
|
*/
|
||
|
|
|
||
|
|
require('dotenv').config();
|
||
|
|
const mysql = require('mysql2/promise');
|
||
|
|
|
||
|
|
async function quickKillAll() {
|
||
|
|
let connection;
|
||
|
|
|
||
|
|
try {
|
||
|
|
connection = await mysql.createConnection({
|
||
|
|
host: process.env.DB_HOST,
|
||
|
|
port: process.env.DB_PORT || 3306,
|
||
|
|
user: process.env.DB_USER,
|
||
|
|
password: process.env.DB_PASSWORD,
|
||
|
|
database: process.env.DB_DATABASE
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('✅ 已连接到数据库\n');
|
||
|
|
|
||
|
|
// 查找并终止所有锁定的进程
|
||
|
|
const [processes] = await connection.query(`
|
||
|
|
SELECT Id, Command, State, Time
|
||
|
|
FROM INFORMATION_SCHEMA.PROCESSLIST
|
||
|
|
WHERE (
|
||
|
|
State LIKE '%waiting for handler commit%'
|
||
|
|
OR State LIKE '%Locked%'
|
||
|
|
OR State LIKE '%lock%'
|
||
|
|
OR (Command = 'Sleep' AND Time > 300)
|
||
|
|
)
|
||
|
|
AND Id != CONNECTION_ID()
|
||
|
|
`);
|
||
|
|
|
||
|
|
if (processes.length === 0) {
|
||
|
|
console.log('✅ 没有找到锁定的进程\n');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`🔪 找到 ${processes.length} 个锁定的进程,正在终止...\n`);
|
||
|
|
|
||
|
|
let killed = 0;
|
||
|
|
for (const p of processes) {
|
||
|
|
try {
|
||
|
|
await connection.query(`KILL ${p.Id}`);
|
||
|
|
console.log(`✅ 已终止进程 ${p.Id}`);
|
||
|
|
killed++;
|
||
|
|
} catch (err) {
|
||
|
|
console.log(`⚠️ 进程 ${p.Id} 终止失败: ${err.message}`);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`\n✅ 成功终止 ${killed}/${processes.length} 个进程\n`);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ 操作失败:', error.message);
|
||
|
|
process.exit(1);
|
||
|
|
} finally {
|
||
|
|
if (connection) {
|
||
|
|
await connection.end();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
quickKillAll();
|