[英]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.