修復 too many connection 問題
This commit is contained in:
93
scripts/ultimate-kill.js
Normal file
93
scripts/ultimate-kill.js
Normal file
@@ -0,0 +1,93 @@
|
||||
// =====================================================
|
||||
// 終極連線清理腳本
|
||||
// =====================================================
|
||||
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
async function ultimateKill() {
|
||||
console.log('💀 執行終極連線清理腳本...');
|
||||
|
||||
let connection = null;
|
||||
|
||||
try {
|
||||
// 建立連線
|
||||
connection = await mysql.createConnection({
|
||||
host: process.env.DB_HOST || '122.100.99.161',
|
||||
port: parseInt(process.env.DB_PORT || '43306'),
|
||||
user: process.env.DB_USER || 'A999',
|
||||
password: process.env.DB_PASSWORD || '1023',
|
||||
database: process.env.DB_NAME || 'db_AI_Platform',
|
||||
charset: 'utf8mb4',
|
||||
timezone: '+08:00',
|
||||
});
|
||||
|
||||
console.log('✅ 已連接到資料庫');
|
||||
|
||||
// 獲取所有連線
|
||||
const [connections] = await connection.execute(`
|
||||
SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE, INFO
|
||||
FROM information_schema.PROCESSLIST
|
||||
WHERE USER = ?
|
||||
ORDER BY TIME DESC
|
||||
`, [process.env.DB_USER || 'A999']);
|
||||
|
||||
console.log(`🔍 找到 ${connections.length} 個連線需要清理`);
|
||||
|
||||
// 顯示連線詳情
|
||||
connections.forEach((conn, index) => {
|
||||
console.log(`${index + 1}. ID: ${conn.ID}, 用戶: ${conn.USER}, 時間: ${conn.TIME}s, 狀態: ${conn.STATE}`);
|
||||
});
|
||||
|
||||
// 殺死所有連線(除了當前連線)
|
||||
let killedCount = 0;
|
||||
for (const conn of connections) {
|
||||
if (conn.ID !== connection.threadId) {
|
||||
try {
|
||||
await connection.execute(`KILL ${conn.ID}`);
|
||||
console.log(`💀 已殺死連線 ${conn.ID}`);
|
||||
killedCount++;
|
||||
} catch (error) {
|
||||
console.log(`⚠️ 無法殺死連線 ${conn.ID}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`✅ 已殺死 ${killedCount} 個連線`);
|
||||
|
||||
// 等待連線關閉
|
||||
console.log('⏳ 等待連線完全關閉...');
|
||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||
|
||||
// 檢查最終狀態
|
||||
const [finalConnections] = await connection.execute(`
|
||||
SELECT COUNT(*) as count FROM information_schema.PROCESSLIST
|
||||
WHERE USER = ?
|
||||
`, [process.env.DB_USER || 'A999']);
|
||||
|
||||
const remainingConnections = finalConnections[0].count;
|
||||
console.log(`📊 清理後剩餘連線: ${remainingConnections}`);
|
||||
|
||||
if (remainingConnections <= 1) {
|
||||
console.log('🎉 連線清理成功!');
|
||||
} else {
|
||||
console.warn(`⚠️ 仍有 ${remainingConnections - 1} 個連線未清理`);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 終極清理失敗:', error);
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
console.log('🔌 連線已關閉');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 執行清理
|
||||
ultimateKill().then(() => {
|
||||
console.log('✅ 腳本執行完成');
|
||||
process.exit(0);
|
||||
}).catch((error) => {
|
||||
console.error('❌ 腳本執行失敗:', error);
|
||||
process.exit(1);
|
||||
});
|
Reference in New Issue
Block a user