node-mysql

NodeJS MySQL: measure query execution time

安稳与你 提交于 2019-12-05 22:07:02
I'm on a shared hosting platform and would like to throttle the queries in my app so that if the total execution time exceeds a certain amount over a variable time period then I can make the app cool off and then resume later on. To do this I would like to find out how long each of my queries take in real time and manage it within the app and not by profiling it externally. I've seen examples in PHP where the time is recorded before and after the query ( even phpMyAdmin does this ), but this won't work in NodeJS or anything that runs the query asynchronously. So the question is: how would I go

insert multiple rows into mysql through node.js

你。 提交于 2019-12-05 21:20:37
I want to insert multiple rows into mysql thru node.js mysql module. The data I have is var data = [{'test':'test1'},{'test':'test2'}]; I am using pool pool.getConnection(function(err, connection) { connection.query('INSERT INTO '+TABLE+' SET ?', data, function(err, result) { if (err) throw err; else { console.log('successfully added to DB'); connection.release(); } }); }); } which fails. Is there a way for me to have a bulk insertion and call a function when all insertion finishes? Regards Hammer Ben You can insert multiple rows into mysql using nested arrays. You can see the answer from this

Trouble connecting Node-mysql

跟風遠走 提交于 2019-12-05 16:58:26
Im trying to connect to my mysql database with node-mysql. When i try the following code, I get this error: ERROR: Error: connect ETIMEDOUT does anyone know whats wrong? The database is up and running btw. var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); var mysql = require('mysql'); var connection = mysql.createConnection({ host: "correct_host", user: "correct_user", password: "correct_password", database:

'ER_PARSE_ERROR' on node mysql when inserting multiple values

雨燕双飞 提交于 2019-12-05 01:33:35
I'm trying to put in multiple values into database using node-mysql in node. my source code engine.files.forEach(function(file) { torrentpath = file.path.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """).replace(/'/g, "&apos;").replace(/\\/g, "/"); torrentFiles.push([ result.insertId, torrentpath, file.length ]); }); console.log(torrentFiles); app.mysql.query('INSERT INTO `files` (`torrentid`, `filename`, `filesize`) VALUES ?', torrentFiles, function(err, result) { if (err) throw err; }); The error I get is [ [ 11, 'ubuntu-14.04-desktop-amd64.iso', 1010827264 ] ]

Combining node-mysql result rows into single JSON return for node.js

允我心安 提交于 2019-12-04 17:41:06
I want to know the best way to return a bunch of JSON that is the result of some dependent mysql queries. app.get('/viewing/:id', function (req, res){ if(!req.cookies.user) { res.end('Requires Authenticated User'); } else { connection.query('SELECT blah blah where userId='+req.params.id, function (error, rows, fields) { Now we have a bunch of rows -- lets say 5. I need to go through each one and make another mysql query based on the data I just got. So I end up needing to repeat call (do I loop?) connection.query('SELECT id, firstName, lastName from users where id='+AN_ID_FROM_PRIOR_QUERY,

Error connecting to Amazon RDS (MySQL) from node-mysql

眉间皱痕 提交于 2019-12-04 15:42:55
I am trying to connect to my Amazon RDS (MySQL) instance from a nodejs code hosted in Lambda using the "felixge/node-mysql" package. I need help to find out what I am doing wrong. I am getting "connect ETIMEDOUT" error. My code is hosted in Lambda and both Lamda and RDS are of the same account and region. I have also attached the RDSFullAccess Policy to the IAM role. RDS instance is also accessible from all IPs(no whitelist/blacklisted IPs). At the same time, I am able to connect from my local MySQL Workbench with same credentials. Here is my code: var connection = mysql.createConnection({

Node MySQL execute multiple queries the fastest possible

允我心安 提交于 2019-12-04 13:12:52
Which is the fastest method gets the query to MYSQL, and then comes back to output: console.log('queries finished', results)" Is there an even better method? Please explain your answer! Thanks! Method 1: var connection = mysql.createConnection({multipleStatements: true}); connection.query('SELECT ?; SELECT ?', [1, 2], function(err, results) { if (err) throw err; console.log('queries done', results); }); Method 2: const Db = mysql.createPool({ connectionLimit: 7, dateStrings: true, multipleStatements: true }); Db.getConnection(function(err, connection) { if(err) console.log(err); connection

Node-Mysql throwing connection timeout

断了今生、忘了曾经 提交于 2019-12-04 04:06:05
My node.js app gives 5xx due to connection timeouts at random times. Here's how I connect and query: var mysql = require('mysql'); var config = { host: '172.10.1.1', port: 3306, user: 'user', password: 'pwd', database: 'mydb', connectionLimit: 15, queueLimit: 30 } var poolCluster = mysql.createPool(config); var queryDB = function(query, cb) { poolCluster.getConnection(function(err, connection) { if(err) { cb(err, null); } else { connection.query(query, function (err, rows) { connection.release(); cb(err, rows); }); } }); }; Also, in another alternate version, with connection pooling disabled,

Expressjs response as JSON and Xml

痴心易碎 提交于 2019-12-03 21:25:26
问题 What i am doing:: I am trying to generate json and xml output for a dataset from database Express Code :: Here i am trying for JSON response var express = require('express') , async = require('async') , http = require('http') , mysql = require('mysql'); var xml = require('xml'); var app = express(); var connection = mysql.createConnection({ host: 'localhost', user: 'root', database: 'MyDatabase' }); connection.connect(); // all environments app.set('port', process.env.PORT || 3007); app.use

Difference between using getConnection() and using pool directly in node.js with node-mysql module?

不问归期 提交于 2019-12-03 08:58:55
问题 The documentation states that you can either use the pool directly with: pool.query(); or get a connection manually and then run a query: 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. }); }); The second option is a lot of code that has to be repeated every time you need to