diff --git a/server/src/config/db.config.ts b/server/src/config/db.config.ts new file mode 100644 index 0000000..811c19d --- /dev/null +++ b/server/src/config/db.config.ts @@ -0,0 +1,17 @@ +export const cdkDbConfig = { + host: '192.168.25.110', // MySQL服务器地址 + user: 'root', // 数据库用户名 + password: 'mysql_tr2Few', // 数据库密码 + database: 'cdk', // 数据库名称 + port: 23306, // MySQL端口号 + connectionLimit: 10 // 连接池大小 +}; + +export const gameDbConfig = { + host: '192.168.25.110', // MySQL服务器地址 + user: 'root', // 数据库用户名 + password: 'mysql_tr2Few', // 数据库密码 + database: 'tafang_game_zjy', // 数据库名称 + port: 23306, // MySQL端口号 + connectionLimit: 10 // 连接池大小 +}; \ No newline at end of file diff --git a/server/src/utils/db.util.ts b/server/src/utils/db.util.ts new file mode 100644 index 0000000..68cc070 --- /dev/null +++ b/server/src/utils/db.util.ts @@ -0,0 +1,33 @@ +import mysql from 'mysql'; +import { dbConfig } from '../config/db.config'; + +// 创建连接池 +const pool = mysql.createPool(dbConfig); + +// 获取数据库连接 +export const getConnection = (): Promise => { + return new Promise((resolve, reject) => { + pool.getConnection((err, connection) => { + if (err) { + reject(err); + } else { + resolve(connection); + } + }); + }); +}; + +// 执行查询 +export const query = async (sql: string, values?: any[]): Promise => { + const connection = await getConnection(); + return new Promise((resolve, reject) => { + connection.query(sql, values, (err, results) => { + connection.release(); // 释放连接 + if (err) { + reject(err); + } else { + resolve(results); + } + }); + }); +}; \ No newline at end of file