feat: 修改env,使用启动前数据库检查
This commit is contained in:
parent
8b5d7ed6dc
commit
54486e8074
4
.env
4
.env
|
|
@ -1,8 +1,8 @@
|
|||
JWT_SECRET=5270e53a05600cb1f19b287a915b1c792d7552cbaae261afb6dcaf988d0db10f
|
||||
SERVER_HOST='192.144.32.178'
|
||||
SERVER_PORT=3306
|
||||
SERVER_USER='root'
|
||||
PASSWORD='lichao1314'
|
||||
SERVER_USER='golc'
|
||||
PASSWORD='Lichao1314!'
|
||||
DB_NAME='auth_db'
|
||||
|
||||
# github
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
const mysql = require('mysql2/promise');
|
||||
|
||||
async function checkSchema() {
|
||||
try {
|
||||
const connection = await mysql.createConnection({
|
||||
host: '192.144.32.178',
|
||||
port: 3306,
|
||||
user: 'root',
|
||||
password: 'lichao1314',
|
||||
database: 'auth_db'
|
||||
});
|
||||
|
||||
console.log('🔍 检查当前 user 表结构:');
|
||||
const [rows] = await connection.execute('DESCRIBE user');
|
||||
console.table(rows);
|
||||
|
||||
await connection.end();
|
||||
} catch (error) {
|
||||
console.error('❌ 数据库连接错误:', error.message);
|
||||
}
|
||||
}
|
||||
|
||||
checkSchema();
|
||||
41
src/main.ts
41
src/main.ts
|
|
@ -9,20 +9,39 @@ import * as bodyParser from 'body-parser';
|
|||
|
||||
dotenv.config();
|
||||
|
||||
async function bootstrap() {
|
||||
// 启动 Nest 应用
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const configService = app.get(ConfigService); // 获取 ConfigService 实例
|
||||
// 连接 MySQL
|
||||
// 创建数据库的函数
|
||||
async function createDatabaseIfNotExists() {
|
||||
try {
|
||||
// 连接 MySQL(不指定数据库)
|
||||
const connection = await mysql.createConnection({
|
||||
host: configService.get('SERVER_HOST'),
|
||||
user: configService.get('SERVER_USER'),
|
||||
password: configService.get('PASSWORD'),
|
||||
host: process.env.SERVER_HOST,
|
||||
user: process.env.SERVER_USER,
|
||||
password: process.env.PASSWORD,
|
||||
});
|
||||
|
||||
console.log('🔌 连接到 MySQL 服务器成功');
|
||||
|
||||
// 创建数据库(如果不存在)
|
||||
await connection.query('CREATE DATABASE IF NOT EXISTS auth_db');
|
||||
console.log('✅ 数据库 auth_db 创建成功或已存在');
|
||||
|
||||
await connection.end();
|
||||
console.log('🔌 关闭数据库连接');
|
||||
} catch (error) {
|
||||
console.error('❌ 创建数据库失败:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function bootstrap() {
|
||||
try {
|
||||
// 1. 首先创建数据库
|
||||
await createDatabaseIfNotExists();
|
||||
|
||||
// 2. 然后启动 Nest 应用(此时数据库已存在)
|
||||
console.log('🚀 启动 NestJS 应用...');
|
||||
const app = await NestFactory.create(AppModule);
|
||||
const configService = app.get(ConfigService); // 获取 ConfigService 实例
|
||||
|
||||
app.enableCors({
|
||||
origin: ['http://108.171.193.155:8000', 'http://localhost:8000'], // 指定允许的前端地址
|
||||
|
|
@ -42,5 +61,11 @@ async function bootstrap() {
|
|||
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
|
||||
|
||||
await app.listen(3030);
|
||||
console.log('✅ 应用启动成功,监听端口 3030');
|
||||
} catch (error) {
|
||||
console.error('❌ 应用启动失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
bootstrap();
|
||||
|
|
|
|||
Loading…
Reference in New Issue