58 lines
1.8 KiB
JavaScript
58 lines
1.8 KiB
JavaScript
const mysql = require('mysql2/promise');
|
||
|
||
// 引入database.js配置
|
||
const dbConfig = require('../src/config/database').pool.config;
|
||
|
||
async function testDevConnection() {
|
||
console.log('🧪 测试开发环境数据库连接...');
|
||
// 使用从database.js导入的配置
|
||
const config = dbConfig;
|
||
|
||
try {
|
||
console.log('🔗 尝试连接到开发服务器:', config.host + ':' + config.port);
|
||
|
||
const connection = await mysql.createConnection(config);
|
||
console.log('✅ 开发环境连接成功!');
|
||
|
||
// 测试基本查询
|
||
const [rows] = await connection.execute('SELECT 1 as test');
|
||
console.log('✅ 基本查询测试通过');
|
||
|
||
// 检查表结构
|
||
const [tables] = await connection.execute(`
|
||
SELECT TABLE_NAME
|
||
FROM INFORMATION_SCHEMA.TABLES
|
||
WHERE TABLE_SCHEMA = ?
|
||
`, [config.database]);
|
||
|
||
console.log('📊 数据库中的表:', tables.map(t => t.TABLE_NAME).join(', ') || '暂无表');
|
||
|
||
await connection.end();
|
||
console.log('🎉 开发环境测试完成');
|
||
return true;
|
||
|
||
} catch (error) {
|
||
console.error('❌ 开发环境连接失败:', error.message);
|
||
console.log('🔍 错误代码:', error.code);
|
||
|
||
if (error.code === 'ECONNREFUSED') {
|
||
console.log('💡 可能原因: 开发服务器未启动或网络不可达');
|
||
} else if (error.code === 'ER_ACCESS_DENIED_ERROR') {
|
||
console.log('💡 可能原因: 用户名或密码错误');
|
||
} else if (error.code === 'ER_BAD_DB_ERROR') {
|
||
console.log('💡 可能原因: 数据库不存在');
|
||
}
|
||
|
||
return false;
|
||
}
|
||
}
|
||
|
||
// 执行测试
|
||
testDevConnection().then(success => {
|
||
if (success) {
|
||
console.log('\n✅ 所有测试通过!开发环境配置正确');
|
||
} else {
|
||
console.log('\n⚠️ 开发环境配置需要检查');
|
||
}
|
||
process.exit(success ? 0 : 1);
|
||
}); |