[英]How to convert a connection to a pool connection in MySQL?
I.我做了下面的代码,但我最近了解了池连接,根据我阅读的内容,它更好。 我只是不明白如何将连接导出到不同的文件。 所以,如果你能指导我如何做,我将不胜感激。 谢谢。
连接.js:
var mysql = require('mysql');
const con = mysql.createConnection({
host:'127.0.0.1', // host of server
user:'root', // MySQL user
password:'BLABLABLA', // MySQL password
database: "rpg"
});
exports.con = con
数据库.js:
const con = require('./connection').con;
mp.events.add('playerJoin', (player) => {
con.query('INSERT INTO BLA BLA BLA', function (err, result) {
//ETC ETC
基本上,我想知道如何导出、导入和进行查询。 我正在使用 MySQL Workbench 8.0。 非常感谢。
创建池的代码与您现在拥有的代码非常相似。 唯一的区别是从池中请求连接的额外步骤。
连接.js
const mysql = require('mysql');
const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "user",
password: "password",
database: "rpg"
});
exports.pool = pool;
数据库.js
const pool = require("./connection").pool;
// Ask the pool for a connection
pool.getConnection((err, conn) => {
if(err){
// Do something with the error
} else {
// Do something with the connection and release it after you're done
conn.query('INSERT INTO BLA BLA BLA', (err, rows) => {
// Do something with the result
// release the connection after you're done so it can be reused
conn.release();
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.