2026-03-02 12:36:41 +00:00
|
|
|
|
const { Sequelize } = require('sequelize');
|
|
|
|
|
|
const mysql = require('mysql2/promise');
|
|
|
|
|
|
|
2026-03-05 15:26:13 +00:00
|
|
|
|
// 环境配置
|
2026-03-05 15:33:32 +00:00
|
|
|
|
const NODE_ENV = 'production';
|
2026-03-05 15:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
// 数据库配置
|
|
|
|
|
|
const dbConfig = {
|
|
|
|
|
|
development: {
|
|
|
|
|
|
host: 'localhost',
|
|
|
|
|
|
user: 'root',
|
|
|
|
|
|
password: '123456',
|
|
|
|
|
|
database: 'rentease'
|
|
|
|
|
|
},
|
|
|
|
|
|
production: {
|
|
|
|
|
|
host: '8.152.207.41',
|
|
|
|
|
|
user: 'rentease',
|
|
|
|
|
|
password: 'Wxx@123!',
|
|
|
|
|
|
database: 'rentease'
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前环境的配置
|
|
|
|
|
|
const currentConfig = dbConfig[NODE_ENV];
|
|
|
|
|
|
|
2026-03-02 12:36:41 +00:00
|
|
|
|
// 先创建数据库
|
|
|
|
|
|
const createDatabase = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 连接到MySQL服务器
|
|
|
|
|
|
const connection = await mysql.createConnection({
|
2026-03-05 15:26:13 +00:00
|
|
|
|
host: currentConfig.host,
|
|
|
|
|
|
user: currentConfig.user,
|
|
|
|
|
|
password: currentConfig.password
|
2026-03-02 12:36:41 +00:00
|
|
|
|
});
|
2026-03-03 06:23:00 +00:00
|
|
|
|
|
2026-03-02 12:36:41 +00:00
|
|
|
|
// 创建数据库
|
2026-03-05 15:26:13 +00:00
|
|
|
|
await connection.query(`CREATE DATABASE IF NOT EXISTS ${currentConfig.database}`);
|
2026-03-02 12:36:41 +00:00
|
|
|
|
console.log('数据库创建成功');
|
|
|
|
|
|
await connection.end();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('创建数据库失败:', error);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 创建数据库连接 - 使用MySQL
|
|
|
|
|
|
const sequelize = new Sequelize(
|
2026-03-05 15:26:13 +00:00
|
|
|
|
currentConfig.database, // 数据库名称
|
|
|
|
|
|
currentConfig.user, // 用户名
|
|
|
|
|
|
currentConfig.password, // 密码
|
2026-03-02 12:36:41 +00:00
|
|
|
|
{
|
2026-03-05 15:26:13 +00:00
|
|
|
|
host: currentConfig.host,
|
2026-03-02 12:36:41 +00:00
|
|
|
|
dialect: 'mysql',
|
|
|
|
|
|
logging: false,
|
|
|
|
|
|
timezone: '+08:00'
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 测试数据库连接
|
|
|
|
|
|
const testConnection = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await createDatabase();
|
|
|
|
|
|
// 重新创建sequelize实例,确保连接管理器是活跃的
|
|
|
|
|
|
const newSequelize = new Sequelize(
|
2026-03-05 15:26:13 +00:00
|
|
|
|
currentConfig.database, // 数据库名称
|
|
|
|
|
|
currentConfig.user, // 用户名
|
|
|
|
|
|
currentConfig.password, // 密码
|
2026-03-02 12:36:41 +00:00
|
|
|
|
{
|
2026-03-05 15:26:13 +00:00
|
|
|
|
host: currentConfig.host,
|
2026-03-02 12:36:41 +00:00
|
|
|
|
dialect: 'mysql',
|
|
|
|
|
|
logging: false,
|
|
|
|
|
|
timezone: '+08:00'
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
|
|
|
|
|
await newSequelize.authenticate();
|
|
|
|
|
|
console.log('数据库连接成功');
|
|
|
|
|
|
return newSequelize;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('数据库连接失败:', error);
|
|
|
|
|
|
return sequelize;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 导出sequelize实例
|
2026-03-03 06:23:00 +00:00
|
|
|
|
module.exports = sequelize;
|