60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
|
|
// 数据库配置
|
|
const dbConfig = {
|
|
host: '129.211.213.226',
|
|
port: 9527,
|
|
user: 'root',
|
|
password: 'aiotAiot123!',
|
|
database: 'jiebandata',
|
|
connectionLimit: 10,
|
|
charset: 'utf8mb4',
|
|
timezone: '+08:00',
|
|
waitForConnections: true,
|
|
queueLimit: 0,
|
|
// 添加连接超时配置
|
|
connectTimeout: 10000, // 10秒连接超时
|
|
acquireTimeout: 10000, // 10秒获取连接超时
|
|
timeout: 10000 // 10秒查询超时
|
|
};
|
|
|
|
async function testConnection() {
|
|
let connection;
|
|
try {
|
|
console.log('尝试连接数据库...');
|
|
connection = await mysql.createConnection(dbConfig);
|
|
console.log('✅ 数据库连接成功');
|
|
|
|
// 测试查询
|
|
const [rows] = await connection.execute('SELECT 1 as test');
|
|
console.log('✅ 数据库查询测试成功:', rows);
|
|
|
|
// 查询管理员表
|
|
const [admins] = await connection.execute('SELECT id, username, role FROM admins LIMIT 3');
|
|
console.log('✅ 管理员表查询成功:');
|
|
console.table(admins);
|
|
|
|
await connection.end();
|
|
console.log('✅ 数据库连接已关闭');
|
|
} catch (error) {
|
|
console.error('❌ 数据库连接失败:', error.message);
|
|
if (error.code) {
|
|
console.error('错误代码:', error.code);
|
|
}
|
|
if (error.errno) {
|
|
console.error('错误编号:', error.errno);
|
|
}
|
|
if (error.syscall) {
|
|
console.error('系统调用:', error.syscall);
|
|
}
|
|
if (connection) {
|
|
try {
|
|
await connection.end();
|
|
} catch (closeError) {
|
|
console.error('关闭连接时出错:', closeError.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
testConnection(); |