refactor(backend): 更新数据库配置并迁移至MySQL,优化文档和技术栈描述
This commit is contained in:
67
backend/scripts/init-database.sql
Normal file
67
backend/scripts/init-database.sql
Normal file
@@ -0,0 +1,67 @@
|
||||
-- 创建数据库
|
||||
CREATE DATABASE IF NOT EXISTS jiebanke_dev CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
USE jiebanke_dev;
|
||||
|
||||
-- 创建管理员表
|
||||
CREATE TABLE IF NOT EXISTS admins (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(100),
|
||||
role ENUM('super_admin', 'admin') DEFAULT 'admin',
|
||||
status ENUM('active', 'inactive') DEFAULT 'active',
|
||||
last_login TIMESTAMP NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 创建用户表
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
email VARCHAR(100) UNIQUE,
|
||||
phone VARCHAR(20),
|
||||
real_name VARCHAR(100),
|
||||
id_card VARCHAR(20),
|
||||
status ENUM('active', 'inactive', 'frozen') DEFAULT 'active',
|
||||
balance DECIMAL(15,2) DEFAULT 0.00,
|
||||
credit_score INT DEFAULT 100,
|
||||
last_login TIMESTAMP NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 创建订单表
|
||||
CREATE TABLE IF NOT EXISTS orders (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
order_no VARCHAR(50) NOT NULL UNIQUE,
|
||||
amount DECIMAL(15,2) NOT NULL,
|
||||
status ENUM('pending', 'processing', 'completed', 'cancelled', 'failed') DEFAULT 'pending',
|
||||
type ENUM('loan', 'repayment', 'transfer') NOT NULL,
|
||||
description TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 插入默认管理员账号
|
||||
INSERT INTO admins (username, password, email, role) VALUES
|
||||
('admin', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'admin@jiebanke.com', 'super_admin'),
|
||||
('manager', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'manager@jiebanke.com', 'admin');
|
||||
|
||||
-- 插入测试用户账号
|
||||
INSERT INTO users (username, password, email, phone, real_name, id_card, balance, credit_score) VALUES
|
||||
('user1', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'user1@example.com', '13800138001', '张三', '110101199001011234', 1000.00, 95),
|
||||
('user2', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'user2@example.com', '13800138002', '李四', '110101199002022345', 500.00, 85);
|
||||
|
||||
-- 创建索引
|
||||
CREATE INDEX idx_admins_username ON admins(username);
|
||||
CREATE INDEX idx_admins_email ON admins(email);
|
||||
CREATE INDEX idx_users_username ON users(username);
|
||||
CREATE INDEX idx_users_email ON users(email);
|
||||
CREATE INDEX idx_users_phone ON users(phone);
|
||||
CREATE INDEX idx_orders_user_id ON orders(user_id);
|
||||
CREATE INDEX idx_orders_order_no ON orders(order_no);
|
||||
CREATE INDEX idx_orders_status ON orders(status);
|
||||
125
backend/scripts/init-test-data.js
Normal file
125
backend/scripts/init-test-data.js
Normal file
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 测试数据初始化脚本
|
||||
* 用于开发环境创建测试数据
|
||||
*/
|
||||
|
||||
const mysql = require('mysql2/promise');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const config = require('../config/env');
|
||||
|
||||
// 数据库配置
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: process.env.DB_PORT || 3306,
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || '',
|
||||
database: process.env.DB_NAME || 'jiebanke_dev',
|
||||
charset: process.env.DB_CHARSET || 'utf8mb4',
|
||||
timezone: process.env.DB_TIMEZONE || '+08:00'
|
||||
};
|
||||
|
||||
// 测试数据
|
||||
const testData = {
|
||||
admins: [
|
||||
{
|
||||
username: 'admin',
|
||||
password: 'admin123',
|
||||
email: 'admin@jiebanke.com',
|
||||
nickname: '超级管理员',
|
||||
role: 'super_admin',
|
||||
status: 1
|
||||
},
|
||||
{
|
||||
username: 'manager',
|
||||
password: 'manager123',
|
||||
email: 'manager@jiebanke.com',
|
||||
nickname: '运营经理',
|
||||
role: 'admin',
|
||||
status: 1
|
||||
}
|
||||
],
|
||||
users: [
|
||||
{
|
||||
username: 'user1',
|
||||
password: 'user123',
|
||||
email: 'user1@example.com',
|
||||
nickname: '旅行爱好者',
|
||||
avatar: null,
|
||||
user_type: '普通用户',
|
||||
status: 1
|
||||
},
|
||||
{
|
||||
username: 'merchant1',
|
||||
password: 'merchant123',
|
||||
email: 'merchant1@example.com',
|
||||
nickname: '农家乐老板',
|
||||
avatar: null,
|
||||
user_type: '商家',
|
||||
status: 1
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
async function initTestData() {
|
||||
let connection;
|
||||
|
||||
try {
|
||||
console.log('🚀 开始初始化测试数据...');
|
||||
|
||||
// 创建数据库连接
|
||||
connection = await mysql.createConnection(dbConfig);
|
||||
console.log('✅ 数据库连接成功');
|
||||
|
||||
// 插入管理员数据
|
||||
console.log('📝 插入管理员数据...');
|
||||
for (const admin of testData.admins) {
|
||||
const hashedPassword = await bcrypt.hash(admin.password, 10);
|
||||
const [result] = await connection.execute(
|
||||
`INSERT INTO admins (username, password, email, nickname, role, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())`,
|
||||
[admin.username, hashedPassword, admin.email, admin.nickname, admin.role, admin.status]
|
||||
);
|
||||
console.log(` 创建管理员: ${admin.username} (ID: ${result.insertId})`);
|
||||
}
|
||||
|
||||
// 插入用户数据
|
||||
console.log('👥 插入用户数据...');
|
||||
for (const user of testData.users) {
|
||||
const hashedPassword = await bcrypt.hash(user.password, 10);
|
||||
const [result] = await connection.execute(
|
||||
`INSERT INTO users (username, password, email, nickname, avatar, user_type, status, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`,
|
||||
[user.username, hashedPassword, user.email, user.nickname, user.avatar, user.user_type, user.status]
|
||||
);
|
||||
console.log(` 创建用户: ${user.username} (ID: ${result.insertId})`);
|
||||
}
|
||||
|
||||
console.log('\n🎉 测试数据初始化完成!');
|
||||
console.log('📋 可用测试账号:');
|
||||
console.log(' 管理员账号: admin / admin123');
|
||||
console.log(' 运营账号: manager / manager123');
|
||||
console.log(' 普通用户: user1 / user123');
|
||||
console.log(' 商家用户: merchant1 / merchant123');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 初始化测试数据失败:', error.message);
|
||||
if (error.code === 'ER_NO_SUCH_TABLE') {
|
||||
console.log('💡 提示: 请先运行数据库迁移脚本创建表结构');
|
||||
}
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是直接运行此文件,则执行初始化
|
||||
if (require.main === module) {
|
||||
initTestData()
|
||||
.then(() => process.exit(0))
|
||||
.catch(() => process.exit(1));
|
||||
}
|
||||
|
||||
module.exports = { initTestData };
|
||||
174
backend/scripts/test-api-endpoints.js
Normal file
174
backend/scripts/test-api-endpoints.js
Normal file
@@ -0,0 +1,174 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* API端点测试脚本
|
||||
* 用于验证后端API接口的正确性和一致性
|
||||
*/
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
// API配置
|
||||
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:3100';
|
||||
const API_VERSION = process.env.API_VERSION || '/api/v1';
|
||||
|
||||
// 测试用例
|
||||
const testCases = [
|
||||
// 管理员接口
|
||||
{
|
||||
name: '管理员登录接口',
|
||||
method: 'POST',
|
||||
path: '/admin/login',
|
||||
expectedStatus: 200,
|
||||
data: {
|
||||
username: 'admin',
|
||||
password: 'admin123'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: '获取管理员信息接口',
|
||||
method: 'GET',
|
||||
path: '/admin/profile',
|
||||
expectedStatus: 200,
|
||||
requiresAuth: true
|
||||
},
|
||||
{
|
||||
name: '获取管理员列表接口',
|
||||
method: 'GET',
|
||||
path: '/admin/list',
|
||||
expectedStatus: 200,
|
||||
requiresAuth: true
|
||||
},
|
||||
|
||||
// 用户接口
|
||||
{
|
||||
name: '用户登录接口',
|
||||
method: 'POST',
|
||||
path: '/auth/login',
|
||||
expectedStatus: 200,
|
||||
data: {
|
||||
username: 'user1',
|
||||
password: 'user123'
|
||||
}
|
||||
},
|
||||
{
|
||||
name: '获取用户信息接口',
|
||||
method: 'GET',
|
||||
path: '/users/profile',
|
||||
expectedStatus: 200,
|
||||
requiresAuth: true
|
||||
},
|
||||
|
||||
// 系统接口
|
||||
{
|
||||
name: '健康检查接口',
|
||||
method: 'GET',
|
||||
path: '/health',
|
||||
expectedStatus: 200
|
||||
},
|
||||
{
|
||||
name: '系统统计接口',
|
||||
method: 'GET',
|
||||
path: '/system-stats',
|
||||
expectedStatus: 200
|
||||
}
|
||||
];
|
||||
|
||||
// 认证令牌
|
||||
let authToken = '';
|
||||
|
||||
async function testEndpoint(testCase) {
|
||||
try {
|
||||
const url = `${API_BASE_URL}${API_VERSION}${testCase.path}`;
|
||||
const config = {
|
||||
method: testCase.method.toLowerCase(),
|
||||
url: url,
|
||||
headers: {}
|
||||
};
|
||||
|
||||
// 添加认证头
|
||||
if (testCase.requiresAuth && authToken) {
|
||||
config.headers.Authorization = `Bearer ${authToken}`;
|
||||
}
|
||||
|
||||
// 添加请求数据
|
||||
if (testCase.data) {
|
||||
config.data = testCase.data;
|
||||
}
|
||||
|
||||
const response = await axios(config);
|
||||
|
||||
// 检查状态码
|
||||
if (response.status === testCase.expectedStatus) {
|
||||
console.log(`✅ ${testCase.name} - 成功 (${response.status})`);
|
||||
|
||||
// 如果是登录接口,保存token
|
||||
if (testCase.path === '/admin/login' && response.data.data && response.data.data.token) {
|
||||
authToken = response.data.data.token;
|
||||
console.log(` 获取到认证令牌`);
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
console.log(`❌ ${testCase.name} - 状态码不匹配: 期望 ${testCase.expectedStatus}, 实际 ${response.status}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
console.log(`❌ ${testCase.name} - 错误: ${error.response.status} ${error.response.statusText}`);
|
||||
if (error.response.data) {
|
||||
console.log(` 错误信息: ${JSON.stringify(error.response.data)}`);
|
||||
}
|
||||
} else {
|
||||
console.log(`❌ ${testCase.name} - 网络错误: ${error.message}`);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function runAllTests() {
|
||||
console.log('🚀 开始API端点测试');
|
||||
console.log(`📊 测试环境: ${API_BASE_URL}`);
|
||||
console.log(`🔗 API版本: ${API_VERSION}`);
|
||||
console.log('='.repeat(60));
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const testCase of testCases) {
|
||||
const result = await testEndpoint(testCase);
|
||||
if (result) {
|
||||
passed++;
|
||||
} else {
|
||||
failed++;
|
||||
}
|
||||
|
||||
// 添加短暂延迟,避免请求过于频繁
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
}
|
||||
|
||||
console.log('='.repeat(60));
|
||||
console.log('📊 测试结果统计:');
|
||||
console.log(`✅ 通过: ${passed}`);
|
||||
console.log(`❌ 失败: ${failed}`);
|
||||
console.log(`📈 成功率: ${((passed / (passed + failed)) * 100).toFixed(1)}%`);
|
||||
|
||||
if (failed === 0) {
|
||||
console.log('🎉 所有测试用例通过!');
|
||||
process.exit(0);
|
||||
} else {
|
||||
console.log('💥 存在失败的测试用例');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果是直接运行此文件,则执行测试
|
||||
if (require.main === module) {
|
||||
runAllTests()
|
||||
.catch(error => {
|
||||
console.error('❌ 测试执行失败:', error.message);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { runAllTests };
|
||||
128
backend/scripts/test-database-connection.js
Normal file
128
backend/scripts/test-database-connection.js
Normal file
@@ -0,0 +1,128 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 数据库连接测试脚本
|
||||
* 用于验证MySQL数据库连接配置的正确性
|
||||
*/
|
||||
|
||||
const mysql = require('mysql2/promise');
|
||||
const config = require('../config/env');
|
||||
|
||||
// 数据库配置
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST || '129.211.213.226',
|
||||
port: process.env.DB_PORT || 9527,
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || 'Aiot123',
|
||||
database: process.env.DB_NAME || 'jiebandata',
|
||||
charset: process.env.DB_CHARSET || 'utf8mb4',
|
||||
timezone: process.env.DB_TIMEZONE || '+08:00',
|
||||
connectTimeout: 10000
|
||||
};
|
||||
|
||||
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(`📈 连接池限制: ${config.mysql.connectionLimit || 10}`);
|
||||
console.log(`🔤 字符集: ${config.mysql.charset}`);
|
||||
console.log(`⏰ 时区: ${config.mysql.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 };
|
||||
62
backend/scripts/test-dev-connection.js
Normal file
62
backend/scripts/test-dev-connection.js
Normal file
@@ -0,0 +1,62 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
async function testDevConnection() {
|
||||
console.log('🧪 测试开发环境数据库连接...');
|
||||
|
||||
const dbConfig = {
|
||||
host: '192.168.0.240',
|
||||
port: 3306,
|
||||
user: 'root',
|
||||
password: 'aiotAiot123!',
|
||||
database: 'jiebandata',
|
||||
connectTimeout: 10000
|
||||
};
|
||||
|
||||
try {
|
||||
console.log('🔗 尝试连接到开发服务器:', dbConfig.host + ':' + dbConfig.port);
|
||||
|
||||
const connection = await mysql.createConnection(dbConfig);
|
||||
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 = 'jiebandata'
|
||||
`);
|
||||
|
||||
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);
|
||||
});
|
||||
48
backend/scripts/test-network.js
Normal file
48
backend/scripts/test-network.js
Normal file
@@ -0,0 +1,48 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
async function testNetworkConnection() {
|
||||
console.log('🌐 测试网络连接性...');
|
||||
|
||||
const dbConfig = {
|
||||
host: '129.211.213.226',
|
||||
port: 9527,
|
||||
user: 'root',
|
||||
password: 'Aiot123',
|
||||
connectTimeout: 5000,
|
||||
acquireTimeout: 5000
|
||||
};
|
||||
|
||||
try {
|
||||
console.log('🔗 尝试连接到:', dbConfig.host + ':' + dbConfig.port);
|
||||
|
||||
const connection = await mysql.createConnection(dbConfig);
|
||||
console.log('✅ 网络连接成功!');
|
||||
|
||||
await connection.end();
|
||||
return true;
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 网络连接失败:', error.message);
|
||||
console.log('🔍 错误代码:', error.code);
|
||||
|
||||
if (error.code === 'ECONNREFUSED') {
|
||||
console.log('💡 可能原因: 端口未开放或服务未启动');
|
||||
} else if (error.code === 'ETIMEDOUT') {
|
||||
console.log('💡 可能原因: 网络超时或防火墙阻挡');
|
||||
} else if (error.code === 'ER_ACCESS_DENIED_ERROR') {
|
||||
console.log('💡 可能原因: 权限配置问题');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 执行测试
|
||||
testNetworkConnection().then(success => {
|
||||
if (success) {
|
||||
console.log('🎉 网络连接测试完成');
|
||||
} else {
|
||||
console.log('⚠️ 请检查网络配置和服务器状态');
|
||||
}
|
||||
process.exit(success ? 0 : 1);
|
||||
});
|
||||
Reference in New Issue
Block a user