33 lines
755 B
JavaScript
33 lines
755 B
JavaScript
|
|
/**
|
||
|
|
* 数据库连接配置
|
||
|
|
*/
|
||
|
|
|
||
|
|
require('dotenv').config();
|
||
|
|
const mysql = require('mysql2/promise');
|
||
|
|
|
||
|
|
// 数据库连接池配置
|
||
|
|
const pool = mysql.createPool({
|
||
|
|
host: process.env.DB_HOST,
|
||
|
|
port: process.env.DB_PORT || 3306,
|
||
|
|
user: process.env.DB_USER,
|
||
|
|
password: process.env.DB_PASSWORD,
|
||
|
|
database: process.env.DB_DATABASE,
|
||
|
|
waitForConnections: true,
|
||
|
|
connectionLimit: 10,
|
||
|
|
queueLimit: 0,
|
||
|
|
enableKeepAlive: true,
|
||
|
|
keepAliveInitialDelay: 0
|
||
|
|
});
|
||
|
|
|
||
|
|
// 测试数据库连接
|
||
|
|
pool.getConnection()
|
||
|
|
.then(connection => {
|
||
|
|
console.log('✅ 数据库连接成功');
|
||
|
|
connection.release();
|
||
|
|
})
|
||
|
|
.catch(err => {
|
||
|
|
console.error('❌ 数据库连接失败:', err.message);
|
||
|
|
});
|
||
|
|
|
||
|
|
module.exports = pool;
|