修復 too many connection 問題

This commit is contained in:
2025-09-21 02:46:16 +08:00
parent a36ab3c98d
commit 808d5bb52c
36 changed files with 5582 additions and 249 deletions

View File

@@ -1,50 +0,0 @@
-- =====================================================
-- 插入團隊成員測試數據
-- =====================================================
-- 首先查看現有的團隊
SELECT '=== 現有團隊 ===' as info;
SELECT id, name, leader_id FROM teams WHERE is_active = 1;
-- 查看現有用戶
SELECT '=== 現有用戶 ===' as info;
SELECT id, name, department FROM users WHERE status = 'active' LIMIT 10;
-- 為每個團隊插入成員數據
-- 團隊 1: aaa (ID: t1757702332911zcl6iafq1)
INSERT IGNORE INTO team_members (id, team_id, user_id, role, joined_at) VALUES
('tm_1_1', 't1757702332911zcl6iafq1', 'db19b491-8f63-44b5-a28a-1f8eeb4fdd3c', '隊長', NOW()),
('tm_1_2', 't1757702332911zcl6iafq1', 'db19b491-8f63-44b5-a28a-1f8eeb4fdd3c', '成員', NOW());
-- 如果有其他團隊,請按照相同格式添加
-- 團隊 2: (如果有的話)
-- INSERT IGNORE INTO team_members (id, team_id, user_id, role, joined_at) VALUES
-- ('tm_2_1', 'team_id_2', 'user_id_2', '隊長', NOW()),
-- ('tm_2_2', 'team_id_2', 'user_id_3', '成員', NOW());
-- 驗證插入結果
SELECT '=== 團隊成員插入結果 ===' as info;
SELECT
tm.id,
tm.team_id,
t.name as team_name,
tm.user_id,
u.name as user_name,
tm.role,
tm.joined_at
FROM team_members tm
LEFT JOIN teams t ON tm.team_id = t.id
LEFT JOIN users u ON tm.user_id = u.id
ORDER BY tm.team_id, tm.role;
-- 檢查團隊成員統計
SELECT '=== 團隊成員統計 ===' as info;
SELECT
t.id,
t.name as team_name,
COUNT(tm.id) as member_count
FROM teams t
LEFT JOIN team_members tm ON t.id = tm.team_id
WHERE t.is_active = 1
GROUP BY t.id, t.name
ORDER BY t.name;

View File

@@ -1,82 +0,0 @@
const mysql = require('mysql2/promise');
async function insertTeamMembers() {
const connection = await mysql.createConnection({
host: 'mysql.theaken.com',
port: 33306,
user: 'AI_Platform',
password: 'Aa123456',
database: 'db_AI_Platform',
charset: 'utf8mb4',
timezone: '+08:00'
});
try {
console.log('🔍 開始插入團隊成員測試數據...');
// 查看現有團隊
const [teams] = await connection.execute('SELECT id, name, leader_id FROM teams WHERE is_active = 1 LIMIT 5');
console.log('現有團隊:', teams);
// 查看現有用戶
const [users] = await connection.execute('SELECT id, name FROM users WHERE status = "active" LIMIT 5');
console.log('現有用戶:', users);
if (teams.length > 0 && users.length > 0) {
// 為每個團隊插入成員
for (let i = 0; i < Math.min(teams.length, 3); i++) {
const team = teams[i];
const teamId = team.id;
// 插入隊長(使用團隊的 leader_id
await connection.execute(
'INSERT IGNORE INTO team_members (id, team_id, user_id, role, joined_at) VALUES (?, ?, ?, ?, NOW())',
[`tm_${Date.now()}_${i}_1`, teamId, team.leader_id, '隊長']
);
// 插入成員(使用其他用戶)
for (let j = 1; j < Math.min(users.length, 3); j++) {
if (users[j].id !== team.leader_id) {
await connection.execute(
'INSERT IGNORE INTO team_members (id, team_id, user_id, role, joined_at) VALUES (?, ?, ?, ?, NOW())',
[`tm_${Date.now()}_${i}_${j+1}`, teamId, users[j].id, '成員']
);
}
}
console.log(`✅ 團隊 ${team.name} 成員插入成功`);
}
}
// 驗證結果
const [members] = await connection.execute(`
SELECT tm.*, t.name as team_name, u.name as user_name
FROM team_members tm
LEFT JOIN teams t ON tm.team_id = t.id
LEFT JOIN users u ON tm.user_id = u.id
ORDER BY tm.team_id
`);
console.log('📊 團隊成員統計:', members);
// 檢查團隊成員數量
const [counts] = await connection.execute(`
SELECT
t.id,
t.name as team_name,
COUNT(tm.id) as member_count
FROM teams t
LEFT JOIN team_members tm ON t.id = tm.team_id
WHERE t.is_active = 1
GROUP BY t.id, t.name
ORDER BY t.name
`);
console.log('📈 團隊成員數量:', counts);
} catch (error) {
console.error('❌ 錯誤:', error);
} finally {
await connection.end();
}
}
insertTeamMembers();

View File

@@ -1,70 +0,0 @@
const mysql = require('mysql2/promise');
async function testTeamData() {
const connection = await mysql.createConnection({
host: 'mysql.theaken.com',
port: 33306,
user: 'AI_Platform',
password: 'Aa123456',
database: 'db_AI_Platform',
charset: 'utf8mb4',
timezone: '+08:00'
});
try {
console.log('🔍 測試團隊數據查詢...');
// 使用與 getAllTeams 相同的查詢
const [results] = await connection.execute(`
SELECT t.*,
u.name as leader_name,
u.phone as leader_phone,
t.leader_id as leader,
COUNT(DISTINCT tm.id) as member_count,
COUNT(DISTINCT a.id) as app_count,
t.created_at as submissionDate
FROM teams t
LEFT JOIN users u ON t.leader_id = u.id
LEFT JOIN team_members tm ON t.id = tm.team_id
LEFT JOIN apps a ON t.id = a.team_id AND a.is_active = 1
WHERE t.is_active = 1
GROUP BY t.id, t.name, t.leader_id, t.department, t.contact_email, t.total_likes, t.is_active, t.created_at, t.updated_at, u.name, u.phone
ORDER BY t.created_at DESC
LIMIT 5
`);
console.log('📊 查詢結果:');
results.forEach((team, index) => {
console.log(`\n團隊 ${index + 1}:`);
console.log(` 名稱: ${team.name}`);
console.log(` 隊長ID: ${team.leader}`);
console.log(` 隊長姓名: ${team.leader_name || 'NULL'}`);
console.log(` 成員數量: ${team.member_count || 0}`);
console.log(` 提交日期: ${team.submissionDate || 'NULL'}`);
console.log(` 部門: ${team.department}`);
});
// 檢查特定團隊的成員
if (results.length > 0) {
const teamId = results[0].id;
const [members] = await connection.execute(`
SELECT tm.*, u.name as user_name
FROM team_members tm
LEFT JOIN users u ON tm.user_id = u.id
WHERE tm.team_id = ?
`, [teamId]);
console.log(`\n🔍 團隊 ${results[0].name} 的成員:`);
members.forEach(member => {
console.log(` - ${member.user_name} (${member.role})`);
});
}
} catch (error) {
console.error('❌ 錯誤:', error);
} finally {
await connection.end();
}
}
testTeamData();

93
scripts/ultimate-kill.js Normal file
View 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);
});