后端版本服务器部署成功
This commit is contained in:
@@ -9,16 +9,10 @@ const mysql = require('mysql2/promise');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const config = require('../config/env');
|
||||
|
||||
// 数据库配置
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST || 'mysql.jiebanke.com',
|
||||
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'
|
||||
};
|
||||
// 引入database.js配置
|
||||
const dbConfig = require('../src/config/database').pool.config;
|
||||
|
||||
// 数据库配置已从database.js导入
|
||||
|
||||
// 测试数据
|
||||
const testData = {
|
||||
|
||||
37
backend/scripts/simple-server.js
Normal file
37
backend/scripts/simple-server.js
Normal file
@@ -0,0 +1,37 @@
|
||||
// 简化版服务器入口文件
|
||||
require('dotenv').config();
|
||||
const express = require('express');
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3202;
|
||||
const HOST = process.env.HOST || '0.0.0.0';
|
||||
|
||||
// 健康检查路由
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
timestamp: new Date().toISOString(),
|
||||
version: '1.0.0',
|
||||
noDbMode: process.env.NO_DB_MODE === 'true'
|
||||
});
|
||||
});
|
||||
|
||||
// 根路由
|
||||
app.get('/', (req, res) => {
|
||||
res.send('简化版服务器运行中...');
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
console.log(`🔍 准备启动服务器在 http://${HOST}:${PORT}`);
|
||||
const server = app.listen(PORT, HOST, () => {
|
||||
console.log(`✅ 服务器启动成功!`);
|
||||
console.log(`🚀 访问地址: http://${HOST}:${PORT}`);
|
||||
console.log(`🔍 NODE_ENV: ${process.env.NODE_ENV}`);
|
||||
console.log(`🔍 NO_DB_MODE: ${process.env.NO_DB_MODE}`);
|
||||
});
|
||||
|
||||
// 错误处理
|
||||
server.on('error', (error) => {
|
||||
console.error('❌ 服务器启动错误:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -8,17 +8,10 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
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',
|
||||
connectTimeout: 10000
|
||||
};
|
||||
// 引入database.js配置
|
||||
const dbConfig = require('../src/config/database').pool.config;
|
||||
|
||||
// 数据库配置已从database.js导入
|
||||
|
||||
async function testDatabaseConnection() {
|
||||
let connection;
|
||||
@@ -80,9 +73,9 @@ async function testDatabaseConnection() {
|
||||
|
||||
// 检查连接池配置
|
||||
console.log('🔍 检查连接池配置...');
|
||||
console.log(`📈 连接池限制: ${config.mysql.connectionLimit || 10}`);
|
||||
console.log(`🔤 字符集: ${config.mysql.charset}`);
|
||||
console.log(`⏰ 时区: ${config.mysql.timezone}`);
|
||||
console.log(`📈 连接池限制: ${dbConfig.connectionLimit || 10}`);
|
||||
console.log(`🔤 字符集: ${dbConfig.charset}`);
|
||||
console.log(`⏰ 时区: ${dbConfig.timezone}`);
|
||||
|
||||
console.log('\n🎉 数据库连接测试完成!');
|
||||
console.log('✅ 所有配置检查通过');
|
||||
|
||||
@@ -1,21 +1,17 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
// 引入database.js配置
|
||||
const dbConfig = require('../src/config/database').pool.config;
|
||||
|
||||
async function testDevConnection() {
|
||||
console.log('🧪 测试开发环境数据库连接...');
|
||||
|
||||
const dbConfig = {
|
||||
host: '192.168.0.240',
|
||||
port: 3306,
|
||||
user: 'root',
|
||||
password: 'aiotAiot123!',
|
||||
database: 'jiebandata',
|
||||
connectTimeout: 10000
|
||||
};
|
||||
// 使用从database.js导入的配置
|
||||
const config = dbConfig;
|
||||
|
||||
try {
|
||||
console.log('🔗 尝试连接到开发服务器:', dbConfig.host + ':' + dbConfig.port);
|
||||
console.log('🔗 尝试连接到开发服务器:', config.host + ':' + config.port);
|
||||
|
||||
const connection = await mysql.createConnection(dbConfig);
|
||||
const connection = await mysql.createConnection(config);
|
||||
console.log('✅ 开发环境连接成功!');
|
||||
|
||||
// 测试基本查询
|
||||
@@ -24,10 +20,10 @@ async function testDevConnection() {
|
||||
|
||||
// 检查表结构
|
||||
const [tables] = await connection.execute(`
|
||||
SELECT TABLE_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_SCHEMA = 'jiebandata'
|
||||
`);
|
||||
SELECT TABLE_NAME
|
||||
FROM INFORMATION_SCHEMA.TABLES
|
||||
WHERE TABLE_SCHEMA = ?
|
||||
`, [config.database]);
|
||||
|
||||
console.log('📊 数据库中的表:', tables.map(t => t.TABLE_NAME).join(', ') || '暂无表');
|
||||
|
||||
|
||||
89
backend/scripts/test-mysql-connection.js
Normal file
89
backend/scripts/test-mysql-connection.js
Normal file
@@ -0,0 +1,89 @@
|
||||
import mysql from 'mysql2/promise';
|
||||
import 'dotenv/config';
|
||||
|
||||
// 引入database.js配置
|
||||
import dbConfig from '../src/config/database';
|
||||
|
||||
// 尝试多种连接配置
|
||||
async function testMySQLConnections() {
|
||||
console.log('🔍 测试MySQL连接配置...\n');
|
||||
|
||||
// 使用database.js中的配置
|
||||
const config1 = {
|
||||
host: dbConfig.pool.config.host,
|
||||
port: dbConfig.pool.config.port,
|
||||
user: dbConfig.pool.config.user,
|
||||
password: dbConfig.pool.config.password,
|
||||
database: dbConfig.pool.config.database,
|
||||
connectTimeout: 5000
|
||||
};
|
||||
|
||||
// 配置2: 不指定数据库名称
|
||||
const config2 = {
|
||||
...config1,
|
||||
database: undefined
|
||||
};
|
||||
|
||||
const configurations = [
|
||||
{ name: 'database.js配置', config: config1 },
|
||||
{ name: 'database.js配置(无数据库)', config: config2 }
|
||||
];
|
||||
|
||||
for (const { name, config } of configurations) {
|
||||
console.log(`\n🎯 尝试连接: ${name}`);
|
||||
console.log(`📌 配置:`, {
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
user: config.user,
|
||||
database: config.database || '不指定'
|
||||
});
|
||||
|
||||
try {
|
||||
const connection = await mysql.createConnection(config);
|
||||
console.log('✅ 连接成功!');
|
||||
|
||||
// 测试简单查询
|
||||
const [rows] = await connection.execute('SELECT 1 as test');
|
||||
console.log('✅ 查询测试通过:', rows);
|
||||
|
||||
// 如果指定了数据库,检查数据库是否存在
|
||||
if (config.database) {
|
||||
const [dbCheck] = await connection.execute(`SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?`, [config.database]);
|
||||
if (dbCheck.length > 0) {
|
||||
console.log('✅ 数据库存在');
|
||||
} else {
|
||||
console.warn('⚠️ 指定的数据库不存在');
|
||||
}
|
||||
}
|
||||
|
||||
await connection.end();
|
||||
return { success: true, config };
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ 连接失败:', error.message);
|
||||
console.error('🔍 错误代码:', error.code);
|
||||
|
||||
if (error.code === 'ECONNREFUSED') {
|
||||
console.error('💡 可能原因: MySQL服务器未启动或网络不可达');
|
||||
} else if (error.code === 'ER_ACCESS_DENIED_ERROR') {
|
||||
console.error('💡 可能原因: 用户名或密码错误');
|
||||
} else if (error.code === 'ER_BAD_DB_ERROR') {
|
||||
console.error('💡 可能原因: 数据库不存在');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log('\n❌ 所有连接配置都失败了');
|
||||
console.log('💡 建议:');
|
||||
console.log('1. 检查MySQL服务器是否已启动');
|
||||
console.log('2. 确认用户名和密码正确');
|
||||
console.log('3. 确认数据库已创建');
|
||||
console.log('4. 检查网络连接');
|
||||
|
||||
return { success: false };
|
||||
}
|
||||
|
||||
// 执行测试
|
||||
testMySQLConnections().then(result => {
|
||||
process.exit(result.success ? 0 : 1);
|
||||
});
|
||||
@@ -1,21 +1,18 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
// 引入database.js配置
|
||||
const dbConfig = require('../src/config/database').pool.config;
|
||||
|
||||
async function testNetworkConnection() {
|
||||
console.log('🌐 测试网络连接性...');
|
||||
|
||||
const dbConfig = {
|
||||
host: '129.211.213.226',
|
||||
port: 9527,
|
||||
user: 'root',
|
||||
password: 'Aiot123',
|
||||
connectTimeout: 5000,
|
||||
acquireTimeout: 5000
|
||||
};
|
||||
// 使用从database.js导入的配置
|
||||
const config = dbConfig;
|
||||
|
||||
try {
|
||||
console.log('🔗 尝试连接到:', dbConfig.host + ':' + dbConfig.port);
|
||||
console.log('🔗 尝试连接到:', config.host + ':' + config.port);
|
||||
|
||||
const connection = await mysql.createConnection(dbConfig);
|
||||
const connection = await mysql.createConnection(config);
|
||||
console.log('✅ 网络连接成功!');
|
||||
|
||||
await connection.end();
|
||||
|
||||
34
backend/scripts/test-server.js
Normal file
34
backend/scripts/test-server.js
Normal file
@@ -0,0 +1,34 @@
|
||||
// 简单的服务器测试脚本
|
||||
import express from 'express';
|
||||
|
||||
const app = express();
|
||||
const PORT = 3201;
|
||||
const HOST = '0.0.0.0';
|
||||
|
||||
// 基本路由
|
||||
app.get('/', (req, res) => {
|
||||
res.send('服务器测试成功!');
|
||||
});
|
||||
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
timestamp: new Date().toISOString(),
|
||||
version: '1.0.0'
|
||||
});
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
const server = app.listen(PORT, HOST, () => {
|
||||
console.log(`✅ 测试服务器启动成功!`);
|
||||
console.log(`🚀 访问地址: http://${HOST}:${PORT}`);
|
||||
});
|
||||
|
||||
// 处理退出信号
|
||||
process.on('SIGINT', () => {
|
||||
console.log('🛑 收到退出信号,关闭测试服务器...');
|
||||
server.close(() => {
|
||||
console.log('✅ 测试服务器已关闭');
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user