如何在 MySQL 中将连接转换为池连接?
How to convert a connection to a pool connection in MySQL?
我。我编写了下面的代码,但我最近了解了池连接,根据我阅读的内容,它更好。我只是不明白如何将连接导出到不同的文件。所以,如果你能指导我如何去做,我将不胜感激。谢谢
connection.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:
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。非常感谢。
创建池的代码与您现在拥有的代码非常相似。唯一的区别是有一个从池中请求连接的额外步骤。
connection.js
const mysql = require('mysql');
const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "user",
password: "password",
database: "rpg"
});
exports.pool = pool;
database.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();
});
}
});
我。我编写了下面的代码,但我最近了解了池连接,根据我阅读的内容,它更好。我只是不明白如何将连接导出到不同的文件。所以,如果你能指导我如何去做,我将不胜感激。谢谢
connection.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:
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。非常感谢。
创建池的代码与您现在拥有的代码非常相似。唯一的区别是有一个从池中请求连接的额外步骤。
connection.js
const mysql = require('mysql');
const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "user",
password: "password",
database: "rpg"
});
exports.pool = pool;
database.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();
});
}
});