Files
ExecuBoard/scripts/test-db-connection.js
2025-08-01 00:55:05 +08:00

51 lines
1.5 KiB
JavaScript

// MySQL 資料庫連接測試腳本
require('dotenv').config({ path: '.env.local' })
const mysql = require('mysql2/promise')
async function testConnection() {
const config = {
host: process.env.DB_HOST,
port: parseInt(process.env.DB_PORT),
database: process.env.DB_DATABASE,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
}
console.log('🔗 嘗試連接到 MySQL 資料庫...')
console.log(`主機: ${config.host}:${config.port}`)
console.log(`資料庫: ${config.database}`)
console.log(`用戶: ${config.user}`)
try {
const connection = await mysql.createConnection(config)
console.log('✅ MySQL 資料庫連接成功!')
// 測試基本查詢
const [rows] = await connection.execute('SELECT VERSION() as version')
console.log(`📊 MySQL 版本: ${rows[0].version}`)
// 檢查資料庫表
const [tables] = await connection.execute(`
SELECT table_name
FROM information_schema.tables
WHERE table_schema = ?
`, [config.database])
console.log(`📋 資料庫表數量: ${tables.length}`)
if (tables.length > 0) {
console.log('現有表:')
tables.forEach(table => {
console.log(` - ${table.table_name}`)
})
} else {
console.log('⚠️ 沒有找到表,您可能需要執行 schema 創建腳本')
}
await connection.end()
} catch (error) {
console.error('❌ 資料庫連接失敗:', error.message)
process.exit(1)
}
}
testConnection()