121 lines
3.8 KiB
JavaScript
121 lines
3.8 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* 数据库连接测试脚本
|
|
* 用于验证MySQL数据库连接配置的正确性
|
|
*/
|
|
|
|
const mysql = require('mysql2/promise');
|
|
const config = require('../config/env');
|
|
|
|
// 引入database.js配置
|
|
const dbConfig = require('../src/config/database').pool.config;
|
|
|
|
// 数据库配置已从database.js导入
|
|
|
|
async function testDatabaseConnection() {
|
|
let connection;
|
|
|
|
try {
|
|
console.log('🚀 开始数据库连接测试...');
|
|
console.log(`📊 环境: ${process.env.NODE_ENV || 'development'}`);
|
|
console.log(`🔗 连接信息: ${dbConfig.host}:${dbConfig.port}/${dbConfig.database}`);
|
|
console.log('='.repeat(50));
|
|
|
|
// 测试连接
|
|
console.log('🔍 测试数据库连接...');
|
|
connection = await mysql.createConnection(dbConfig);
|
|
console.log('✅ 数据库连接成功');
|
|
|
|
// 测试查询
|
|
console.log('🔍 测试基本查询...');
|
|
const [rows] = await connection.execute('SELECT 1 + 1 AS result');
|
|
console.log(`✅ 查询测试成功: ${rows[0].result}`);
|
|
|
|
// 检查表结构
|
|
console.log('🔍 检查核心表结构...');
|
|
const tablesToCheck = ['admins', 'users', 'merchants', 'orders'];
|
|
|
|
for (const table of tablesToCheck) {
|
|
try {
|
|
const [tableInfo] = await connection.execute(
|
|
`SELECT COUNT(*) as count FROM information_schema.tables
|
|
WHERE table_schema = ? AND table_name = ?`,
|
|
[dbConfig.database, table]
|
|
);
|
|
|
|
if (tableInfo[0].count > 0) {
|
|
console.log(`✅ 表存在: ${table}`);
|
|
} else {
|
|
console.log(`⚠️ 表不存在: ${table}`);
|
|
}
|
|
} catch (error) {
|
|
console.log(`❌ 检查表失败: ${table} - ${error.message}`);
|
|
}
|
|
}
|
|
|
|
// 检查管理员表数据
|
|
console.log('🔍 检查管理员数据...');
|
|
try {
|
|
const [adminCount] = await connection.execute('SELECT COUNT(*) as count FROM admins');
|
|
console.log(`📊 管理员记录数: ${adminCount[0].count}`);
|
|
|
|
if (adminCount[0].count > 0) {
|
|
const [admins] = await connection.execute('SELECT username, role, status FROM admins LIMIT 5');
|
|
console.log('👥 管理员样例:');
|
|
admins.forEach(admin => {
|
|
console.log(` - ${admin.username} (${admin.role}, 状态: ${admin.status})`);
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 检查管理员数据失败:', error.message);
|
|
}
|
|
|
|
// 检查连接池配置
|
|
console.log('🔍 检查连接池配置...');
|
|
console.log(`📈 连接池限制: ${dbConfig.connectionLimit || 10}`);
|
|
console.log(`🔤 字符集: ${dbConfig.charset}`);
|
|
console.log(`⏰ 时区: ${dbConfig.timezone}`);
|
|
|
|
console.log('\n🎉 数据库连接测试完成!');
|
|
console.log('✅ 所有配置检查通过');
|
|
|
|
} catch (error) {
|
|
console.error('❌ 数据库连接测试失败:', error.message);
|
|
console.error('💡 可能的原因:');
|
|
console.error(' - 数据库服务未启动');
|
|
console.error(' - 连接配置错误');
|
|
console.error(' - 网络连接问题');
|
|
console.error(' - 数据库权限不足');
|
|
console.error(' - 防火墙限制');
|
|
console.error(' - IP地址未授权');
|
|
|
|
if (error.code) {
|
|
console.error(`🔍 错误代码: ${error.code}`);
|
|
}
|
|
|
|
console.error('🔍 连接详情:', {
|
|
host: dbConfig.host,
|
|
port: dbConfig.port,
|
|
user: dbConfig.user,
|
|
database: dbConfig.database
|
|
});
|
|
|
|
process.exit(1);
|
|
|
|
} finally {
|
|
if (connection) {
|
|
await connection.end();
|
|
console.log('🔒 数据库连接已关闭');
|
|
}
|
|
}
|
|
}
|
|
|
|
// 如果是直接运行此文件,则执行测试
|
|
if (require.main === module) {
|
|
testDatabaseConnection()
|
|
.then(() => process.exit(0))
|
|
.catch(() => process.exit(1));
|
|
}
|
|
|
|
module.exports = { testDatabaseConnection }; |