55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
|
|
async function testDbConnection() {
|
|
console.log('🧪 測試資料庫連接...\n');
|
|
|
|
try {
|
|
// 資料庫配置
|
|
const dbConfig = {
|
|
host: process.env.DB_HOST || 'mysql.theaken.com',
|
|
port: parseInt(process.env.DB_PORT || '33306'),
|
|
user: process.env.DB_USER || 'AI_Platform',
|
|
password: process.env.DB_PASSWORD || 'Aa123456',
|
|
database: process.env.DB_NAME || 'db_AI_Platform',
|
|
charset: 'utf8mb4',
|
|
timezone: '+08:00'
|
|
};
|
|
|
|
console.log('連接配置:', {
|
|
host: dbConfig.host,
|
|
port: dbConfig.port,
|
|
user: dbConfig.user,
|
|
database: dbConfig.database
|
|
});
|
|
|
|
// 創建連接
|
|
const connection = await mysql.createConnection(dbConfig);
|
|
console.log('✅ 資料庫連接成功');
|
|
|
|
// 測試查詢
|
|
const [rows] = await connection.execute('SELECT COUNT(*) as count FROM users WHERE is_active = TRUE');
|
|
console.log('✅ 查詢成功,用戶數量:', rows[0].count);
|
|
|
|
// 測試用戶列表查詢
|
|
const [users] = await connection.execute(`
|
|
SELECT
|
|
id, name, email, avatar, department, role, join_date,
|
|
total_likes, total_views, is_active, last_login, created_at, updated_at
|
|
FROM users
|
|
WHERE is_active = TRUE
|
|
ORDER BY created_at DESC
|
|
LIMIT 10
|
|
`);
|
|
console.log('✅ 用戶列表查詢成功,返回用戶數:', users.length);
|
|
|
|
await connection.end();
|
|
console.log('✅ 連接已關閉');
|
|
|
|
} catch (error) {
|
|
console.error('❌ 資料庫連接失敗:', error.message);
|
|
console.error('詳細錯誤:', error);
|
|
}
|
|
}
|
|
|
|
testDbConnection();
|