mysql2 getConnection method is skipped - mysql

I have a problem in Nodejs when trying to make a connection with my db via getConnection method. I declared my db variable via require which is a createPool promise(as shown below). when i try to make a connection with that pool via getConnection it skips that code block.
const pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
const db = pool.promise(); // this is exported
my connection method:
console.log('start');
const items = [this.idplace,this.idactivity,this.iduser,this.date,this.hour,this.party_size];
db.getConnection(function(err, conn) {
if(err) {
console.log('error when connecting to db:', err);
db.releaseConnection(conn);
throw err;
}
console.log('ok');
conn.execute(`INSERT INTO reservation(place,activity,customer,date,hour,party_size)VALUES(?,?,?,'?','?',?);`, items);
db.releaseConnection(conn);
});
console.log('end');
and the log is:
start
end
(no sign of the logs inside db.getConnection)
What do you think is the reason of that?

Related

Dynamically change the name of mySql database in pool in node.js

connection.js
var mysql = require("mysql");
const { createPool } = require("mysql");
const pool = createPool({
port: process.env.DB_PORT,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.MYSQL_DB,
connectionLimit: 10,
});
module.exports = pool;
product.service.js
const pool = require("../../../config/database");
module.exports = {
viewVariate: (callBack) => {
pool.query(
`SELECT varId , varName FROM variations WHERE varIsActive='1'`,
(errors, results, fields) => {
if (errors) {
return callBack(errors);
}
return callBack(null, results);
}
);
},
}
I have to maintain more than 100 users. so each person has own database in that node app. each person database name call via API , so I need to set given database name to above pool dynamically , I referred may ways. it did not work. connection.js file is my database connection with pool. then it call to service files to make connection queries. for that pool data obtains form env file. it doesn't matter when config mentioned requirement will be done.

JS code connection issues with postman ( connection error pops up says "EER_Socket_BAD_Port")

I am getting an error when I run this code, it says Port should be >= 0 and < 65536 and i sent 123456. how can i make my port smaller?
const mysql = require('mysql');
require('dotenv').config();
var connection = mysql.createConnection({
port: process.env.DB_PORT,
port: process.env.DB_HOST,
port: process.env.DB_USERNAME,
port: process.env.DB_PASSWORD,
database: process.env.DB_NAME
})
connection.connect((err) =>{
if (!err){
console.log("Connected");
}
else{
console.log(err);
}
});
module.exports = connection;

errno: 1203, code: 'ER_TOO_MANY_USER_CONNECTIONS' in nodejs mysql code?

Hello Everyone,
I'm a beginner in Node.js Mysql. I have connected to Node.js with mysql. While starting the Node.js server, I got the error like " code: 'ER_TOO_MANY_USER_CONNECTIONS', " further I will attach the mysql db connection code below. Any type of help would be appreciated. Thanks in advance...
var db = mysql.createPool({
host: 'xxxxxxxxxxxxxxxxx',
port: 'xxx',
user: 'xxxx',
password: 'xxx',
database: 'xxx'
});
db.getConnection((err, tempConn) => {
if (err) {
console.log(err);
}
else {
tempConn.release();
console.log('Mysql Connected');
}
});
module.exports={db};
If you're creating a pool you don't need to use getConnection. There is a shortcut that allows you to use it directly. If you do use getConnection you must follow it with a query, then you may release the connection. Your example is missing a query.
Here is a helpful template for using a pool config:
// in your application initialization file such as app.js
//
// other require items here as well like express maybe?
//
const mysql = require('mysql');
const connection = mysql.createPool({
connectionLimit: 10,
host: process.env.DB_HOST || '127.0.0.1',
user: process.env.DB_USER || 'local_user',
password: process.env.DB_PASSWORD || 'local_password',
database: process.env.DB_NAME || 'local_database',
multipleStatements: true,
charset: 'utf8mb4' // necessary if you might need support for emoji characters
});
connection.on('connection', function (connection) {
// handy for testing
console.log('Pool id %d connected', connection.threadId);
});
connection.on('enqueue', function () {
// handy for testing
console.log('Waiting for available connection slot');
});
global.db = connection;
//
// other app setup stuff here like app.set, app.engine, app.use, module.exports = app and all that good stuff
//
// later…
// everywhere else in your app, use the global db variable when running queries
// ../new_users.js or similar maybe?
const _create_user = (user_payload) => {
db.query(
'INSERT INTO users SET ?', user_payload, function(error, results, fields) {
if (error) throw error;
console.log(results);
});
}
// maybe we are in a module that has access to
// the request object so we can use something
// that has come via POST
//
// here is a manual object as a placeholder…
let new_user = {
first_name: 'John',
last_name: 'Smith',
email: 'j.smith#example.com',
password: 'keyboard_cat'
}
_create_user(new_user);

NodeJS: How to use Connection Pooling with MySQL correctly? How useful it is?

I'm using connection pooling in NodeJS with MySQL. There are several connections remain in processlist in Sleep state, which results too many connection in the end even if site does not have heavy traffic.
Here is sample code as I'm using it:
var pool = mysql.createPool({
host: '127.0.0.1',
user: '***',
password: '***',
database: '****'
});
pool.getConnection(function (err, connection) {
connection.query("SELECT * from test", function (error, data) {
connection.release();
if (error) {
console.log(error);
} else {
// perform further process
}
});
});
Is connection not released at proper location? Suggest if there could be any improvement in code above.
That is what pool does, you can add connection limit...
var pool = mysql.createPool({
connectionLimit : 10,
host: '127.0.0.1',
user: '***',
password: '***',
database: '****'
});

nodejs to mysql connection error

I'm getting error :
error:Error: Handshake inactivity timeout
I connected mysql to node by like this.
Install mysql
npm install mysql
var mysql = require('mysql');
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
port: '8888', /* port on which phpmyadmin run */
password: 'root',
database: 'dbname',
socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock' //for mac and linux
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
You can use node-mysql, It very easy to use. I have used once, Here is the example :-
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
Use Connection Pool:
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'example.org',
user : 'bob',
password : 'secret'
});
pool.getConnection(function(err, connection) {
// Use the connection
connection.query( 'SELECT something FROM sometable', function(err, rows) {
// And done with the connection.
connection.release();
// Don't use the connection here, it has been returned to the pool.
});
});