Here is my node js code
var mysql = require('mysql');
exports.helloGET = (req, res) => {
console.log(req.body.message);
var con = mysql.createConnection({
//socketPath: '/cloudsql/' + 'project-id:region-name:instance',
host: 'xxx.xxx.xxx.xxx',
user: 'user',
password: 'root',
database: 'test'
});
con.connect(function(err) {
console.log("Connection Error - " + err)
if (err) throw err;
console.log("Connected to Cloud SQL");
});
con.query(`SELECT now()`, null, function (err, result) {
//made reply here
console.log("Query Error - " + err)
if (err) throw err;
console.log("Result: " + result);
});
con.end();
res.status(200).send('Acknowledgement : ' + req.body.message);
};
Error: connect ETIMEDOUT xxx.xxx.xxx.xxx:3306
I tried using socketPath instead of host -
var con = mysql.createConnection({
socketPath: '/cloudsql/' + 'project-id:region-name:instance',
//host: 'xxx.xxx.xxx.xxx',
user: 'user',
password: 'root',
database: 'test'
});
For this I am getting - Error: connect ECONNREFUSED
That feature is currently not supported. However there is a feature request open that you can follow in the issue tracker #36388165.
Like is mentioned in this comment from Jason Polites you can fill this early access form for access Cloud SQL from a Cloud Function.
Related
I have a question, Before I change my code, I got error [error: connection lost: the server closed the connection.], possibly because its idle for sometime.
This is my old code.
const dbConn = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test'
});
dbConn.connect(function(err) {
if(err) throw err;
console.log("Database Connected!");
})
module.exports = dbConn;
After searching for a while, most of it recommend using createPool intead of createConnection, so I change my code to this
const dbConn = mysql.createPool({
host : 'localhost',
user : 'root',
password : '',
database : 'test'
});
module.exports = dbConn;
Then my question is, do I have to release the connection everytime we complete a query? This is how I do query.
dbConn.query("SELECT * FROM spesialisasi s ORDER BY s.nama_spesialisasi ASC ",
function(err, res) {
if(err) {
console.log("error: ", err);
result(err, null);
} else {
result(null, res);
}
}
)
From my knowledge, pool.query() will automatically release the connection when the query completes. Here's the section on connection pooling from the MySQL NPM docs
I want to connect my database with nodejs but it's not connecting and giving an Error: connect ETIMEDOUT.
My code is...
var con = mysql.createConnection({
host: hostname,
user: user,
password: pass,
database: database_name,
});
con.connect(function (error) {
if (error) console.log("Not Connected: " + error);
else console.log("connected");
});
const mysql = require('mysql');
const dbPool = mysql.createPool({
connectionLimit : 10,
port : 'port of your Db',
host : 'host of your Db,
user : 'user of your Db,
password : 'password of your Db,
database : 'database name,
dateStrings : true,
debug : false
});
I make a connection to mysql using node js,
connection is successful, event if i put a wrong port,
but query didn't return any results.
mysql version for node js is : "mysql": "^2.18.1"
data base version : 5.5.47
(port : number or string : same result)
var express = require('express');
var mysql = require('mysql');
var app = express();
//
var connection = mysql.createConnection({
host: 'localhost',
database: 'mydb',
user : 'root',
password: 'pwd',
port : '3306'
});
connection.connect(function(error){
if(!error){
console.log(error)
} else{
console.log('Connected')
}
});
connection.query("SELECT * FROM members", function(error, rows){
if(!error){
console.log("Error in the query")
console.log(error)
}else{
console.log("successfull")
console.log(rows)
}
})
app.listen(4000)
Following logic is incorrect. it says if not error then log error so you will never get your rows.
if(!error){
console.log("Error in the query")
console.log(error)
}else{
console.log("successfull")
console.log(rows)
}
Remove the !
I recently started learning Nodejs to connect to MySQL and i'm having connection issues. I tried using putty to connect and i'm able to do so without any issues
this is my example.route.js file
const express = require('express'),
router = express.Router(),
mysql = require('mysql');
var connection = mysql.createConnection({
host: <ipaddress>,
port: <default Port>,
user: 'root',
password: <given password>,
database: <db name>,
connectionTimeout: 30000
});
connection.connect((error) => {
if (error) {
console.log('error connecting: ' + error);
} else {
console.log('Connected to server');
}
});
connection.query('SELECT * FROM test', function(err, results, fields) {
console.log('results in table: ' + results);
console.log('fields in table: ' + fields); // -1
connection.release();
if (err) throw error;
});
connection.end();
module.exports = router;
i get the following error => Error: connect ECONNREFUSED (Will update with complete error in a few hours)
As i mentioned before i used PUTTY in order to make sure there was an issue when connecting but i was able to connect to the given database name, host with the same user and password.
Not sure if this helps is an ubuntu server with MySQL
Anyone has an idea of why i'm getting the connection error? I would appreciate it the help
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.
});
});