[英]How to convert a connection to a pool connection in MySQL?
I. I made the code below but I recently learned about pool connection, which according to what I read are better. I.我做了下面的代码,但我最近了解了池连接,根据我阅读的内容,它更好。 I just don't understand how to export the connection to a different file.
我只是不明白如何将连接导出到不同的文件。 So, if you could guide me on how to do it I would appreciate it.
所以,如果你能指导我如何做,我将不胜感激。 Thanks.
谢谢。
connection.js:连接.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
database.js:数据库.js:
const con = require('./connection').con;
mp.events.add('playerJoin', (player) => {
con.query('INSERT INTO BLA BLA BLA', function (err, result) {
//ETC ETC
Basically, I want to know how to export, import and make a query.基本上,我想知道如何导出、导入和进行查询。 I'm using MySQL Workbench 8.0.
我正在使用 MySQL Workbench 8.0。 Thank you very much.
非常感谢。
The code for creating a pool is very similar to the code you have right now.创建池的代码与您现在拥有的代码非常相似。 The only difference is there's the extra step of requesting a connection from the pool.
唯一的区别是从池中请求连接的额外步骤。
connection.js连接.js
const mysql = require('mysql');
const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "user",
password: "password",
database: "rpg"
});
exports.pool = pool;
database.js数据库.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.