Error: function crashed. Details: connect ECONNREFUSED /cloudsql/<instance Connection Name>> - mysql

I am not able to connect Cloud Functions to Cloud SQL database facing the issue like that
Code which I used to connect the database:
var mysql = require('mysql');
exports.mysql = function handler(req, res) {
var c = mysql.createConnection({
socketPath: '/cloudsql/' + 'instance Connection Name',
user: 'root',
password: 'lns12345',
database: 'Sample'
});
c.connect();
c.query('SELECT * from user', req.body.user, function(e, results) {
//made reply here
if (results) {
console.log('Database succeed')
} else {
console.log('new error created')
}
console.log('my E result is :', e)
console.log('My results is :', results)
});
};
Error what I received:
Error: function crashed. Details:
connect ECONNREFUSED /cloudsql/'instance Connection Name'
Detailed Log which I got in Cloud Functions:
Error: connect ECONNREFUSED /cloudsql/'instance connection Name' at
Object.exports._errnoException (util.js:1020:11) at
exports._exceptionWithHostPort (util.js:1043:20) at
PipeConnectWrap.afterConnect [as oncomplete] (net.js:1105:14)
-------------------- at Protocol._enqueue (/user_code/node_modules/mysql/lib/protocol/Protocol.js:145:48) at
Protocol.handshake
(/user_code/node_modules/mysql/lib/protocol/Protocol.js:52:23) at
Connection.connect
(/user_code/node_modules/mysql/lib/Connection.js:130:18) at handler
(/user_code/index.js:20:4) at /var/tmp/worker/worker.js:684:7 at
/var/tmp/worker/worker.js:668:9 at _combinedTickCallback
(internal/process/next_tick.js:73:7) at process._tickDomainCallback
(internal/process/next_tick.js:128:9)

Replace 'instance Connection Name' placeholder with the actual connection name

Related

How to connect not to local MySQL db (PHPMyAdmin) by using React.js?

I am pretty new at Node.js, and I have existing database uploaded to Dreamhost, which has database PhpMyAdmin. I have created new React application, and by using my server folder I am trying to connect to that database. I am using Windows 10, and I run at http://127.0.0.1:5000/. This is my code:
const express = require("express");
const bodyParser = require("body-parser");
const mysql = require("mysql");
const app = express()
const port = process.env.PORT || 5000
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// MySQL
const pool = mysql.createPool({
connectionLimit: 10,
user: "root",
host: '127.0.0.1',
password: "password",
database: "testdb_org"
})
// Get all info
app.get('/', (req, res) => {
pool.getConnection((err, connection) => {
res.send('TEST '+ JSON.stringify(err))
/*if (err) throw err
console.log(`Connected as id ${connection.threadId}`)
connection.query('SELECT * from users', (err, rows) => {
connection.release() // Return the connection to pool
if (!err) {
res.send(rows)
} else {
console.log(err)
}
})*/
})
})
app.listen(port, () => console.log(`Listen on port ${port}`))
res.send gives me following error:
{"code":"ER_ACCESS_DENIED_ERROR","errno":1045,"sqlMessage":"Access denied for user 'root'#'localhost' (using password: YES)","sqlState":"28000","fatal":true}
And when I open comment brackets it gives me following console error:
if (err) throw err
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (C:\Users\*\Documents\project\server\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\Users\*\Documents\project\server\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (C:\Users\*\Documents\project\server\node_modules\mysql\lib\Connection.js:116:18)
at Pool.getConnection (C:\Users\*\Documents\project\server\node_modules\mysql\lib\Pool.js:48:16)
at C:\Users\*\Documents\project\server\app.js:24:10
at Layer.handle [as handle_request] (C:\Users\*\Documents\project\server\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\*\Documents\project\server\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (C:\Users\*\Documents\project\server\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (C:\Users\*\Documents\project\server\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\*\Documents\project\server\node_modules\express\lib\router\index.js:284:15 {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
Ok, the problem that it's not possible with online database and localhost of React.js. For connection I had to download .sql database, insert and run it locally with XAMPP control panel.

connection to a remote mysql database using node

Good mornig to you guys. I want to establish a connection to a remote mysql database using node js. but i am facing this error. I do not know if I wrongly specifies the access path to the db
code
var mysql = require('mysql');
var pool = mysql.createPool({
host: "http://kamerun-it.com/mysql",
connectionLimit : 100,
database: "****",
user: "****",
password: "*****",
multipleStatements: true
});
error
throw err;
^
Error: getaddrinfo ENOTFOUND http://kamerun-it.com/mysql
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26)
--------------------
at Protocol._enqueue (F:\kamerun it\aspi-api\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (F:\kamerun it\aspi-api\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at PoolConnection.connect (F:\kamerun it\aspi-api\node_modules\mysql\lib\Connection.js:119:18)
at Pool.getConnection (F:\kamerun it\aspi-api\node_modules\mysql\lib\Pool.js:48:16)
at Object. (F:\kamerun it\aspi-api\app\model\db.js:16:8)
at Module._compile (internal/modules/cjs/loader.js:956:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loader.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:849:19) {
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'http://kamerun-it.com/mysql',
fatal: true
}
You probably have an error in your config with the host URL.
Here's a working example with a connection to a remote MySQL:
const mysql = require("mysql");
const connection = mysql.createPool({
host: "remotemysql.com",
user: "aKlLAqAfXH",
password: "PZKuFVGRQD",
database: "aKlLAqAfXH"
});
connection.query(
"SELECT hexcode FROM colours WHERE precedence = 2",
(err, result) => {
err ? console.log(err) : console.log(result[0].hexcode);
}
);
and here's one with mistaken host parameter:
const mysql = require("mysql");
const connection = mysql.createPool({
host: "WRONGremotemysql.com",
user: "aKlLAqAfXH",
password: "PZKuFVGRQD",
database: "aKlLAqAfXH"
});
connection.query(
"SELECT hexcode FROM colours WHERE precedence = 2",
(err, result) => {
err ? console.log(err) : console.log(result[0].hexcode);
}
);
The second one returns the same ENOTFOUND error.
Check if that's the correct URL, if the database can be accessed remotely and via which port you can use it.

How to provide SERVICE_NAME in the connection options in mysql npm?

I have a JDBC connection string something like this
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=OFF)(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1711))(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1712)))(CONNECT_DATA=(SERVICE_NAME=servicename)(SERVER=DEDICATED)))
I have created the connection as
var connection = mysql.createConnection({
host: "hostname",
port: "1711",
user: "user",
password: "password"
});
How do I also provide SERVICE_NAME (like the jdbc string) to the connection options?
I am getting the following error without it:
Error: connect ECONNREFUSED <some_ip>:1711
at Object.exports._errnoException (util.js:1028:11)
at exports._exceptionWithHostPort (util.js:1051:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1090:14)
--------------------
EDIT:
Changing the port to 1712 gives me a different error after taking a lot of time.
Error: Connection lost: The server closed the connection.
at Protocol.end (..\node_modules\mysql\lib\protocol\Protocol.js:113:13)
at Socket.<anonymous> (..\node_modules\mysql\lib\Connection.js:109:28)
at emitNone (events.js:91:20)
at Socket.emit (events.js:186:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
EDIT: Sorry. I didn't see the database was oracle and started trying mysql directly.
I am assuming that your mysql server is running on port 1711 not default port.
And if you are getting Connection lost error mean there are something wrong on your network try to reconnect your server after sometime.
var conn;
function handleDisconnect() {
conn = mysql.createConnection(//your config);
conn.connect(function(err) {
if(err) {
console.log('error when connecting to database:', err);
setTimeout(handleDisconnectConnection, 5000);
}
});
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnectConnection();
} else {
throw err;
}
});
}
handleDisconnectConnection();
Hope that will work for you

Node Js : MYSQL is not connecting

I am new to node.js, so i am stuck at this point. I have tried to learn it from these sites Code Mentor and Github
Till now i have done this :
var mysql = require('mysql');
var app = express();
// Database operations
var connection = mysql.createConnection({
host : 'localhost',
port : '8888',
user : 'sharad',
password : 'teks',
dbName : 'FirstDataBase'
});
connection.connect(function (error) {
if (error){
console.error('error connecting :' + error.stack);
return;
}
console.log('Connected as id :'+ connection.threadId);
});
app.get("/",function(request,response){
console.log(' saving');
var post = {from:'me', to:'you', msg:'hi'};
connection.query('INSERT INTO FirstDataBase SET ?', post, function(err, result) {
if (err) throw err;
});
});
app.get("/",function(request,response){
connection.query('SELECT * from FirstDataBase', function(err, rows, fields) {
connection.end();
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
});
app.listen(8888);
I can not see any log i am printing in my code. Any help would be appreciated. Thanks
Edits :
Error i get if i remove last line app.listen(8888);
error connecting :Error: connect ECONNREFUSED 127.0.0.1:8888
at Object.exports._errnoException (util.js:907:11)
at exports._exceptionWithHostPort (util.js:930:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1077:14)
--------------------
at Protocol._enqueue (/Users/Admin/IdeaProjects/FirstNodeJS/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Users/Admin/IdeaProjects/FirstNodeJS/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Users/Admin/IdeaProjects/FirstNodeJS/node_modules/mysql/lib/Connection.js:136:18)
at Object.<anonymous> (/Users/Admin/IdeaProjects/FirstNodeJS/app.js:40:12)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
I resolve this issue 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.');
});
I resolved my problem by installing MAMP and using fields provided by MAMP.
try this
var connection = mysql.createConnection({
host : 'localhost',
user : 'xxxxx',
password : 'xxxxx',
database : 'xxx'
});
connection.connect();

error connecting/querying mysql node.js and postreSQL

the mysql/node.js appears to connect then i "think" there may be a problem with the query event logic. Here's the code and error. I also have a separate/similar problem with postreSQL. I install npmed both in the same directory.
ryan#\Ryan:~/Desktop/node/datastores$ node MySQL
connection::connected
/home/ryan/Desktop/node/datastores/MySQL.js:20
if (err) throw err;
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
--------------------
at Protocol._enqueue (/home/ryan/Desktop/node/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/home/ryan/Desktop/node/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/home/ryan/Desktop/node/node_modules/mysql/lib/Connection.js:123:18)
at Object.<anonymous> (/home/ryan/Desktop/node/datastores/MySQL.js:15:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
this is my server/mysql logic
var mysql = require('mysql');
var connectionConfig = {
host: 'localhost',
user: 'root',
password: '',
database: 'sakila'
};
var connection = mysql.createConnection( connectionConfig);
connection.connect(function(err) {
console.log('connection::connected');
});
connection.query('SELECT * FROM actor', function(err, rows, fields) {
if (err) throw err;
rows.forEach(function(row) {
console.log(row.first_name, row.last_name);
});
});
var actor = { first_name: 'Wil', last_name: 'Wheaton' };
connection.query('INSERT INTO actor SET ?', actor, function(err, results) {
if (err) throw err;
console.log(results);
});
connection.end(function(err) {
console.log('connection::end');
});
heres postre logic
var pg = require('pg');
var connectionString = 'postgres://ryan#localhost/postgres';
var client = new pg.Client(connectionString);
client.connect(function(err) {
if (err) throw err;
client.query('SELECT NOW() AS "THE TIME"', function( err, result ) {
if (err) throw err;
console.log(result.rows[0]);
client.end();
});
});
heres error
ryan#\Ryan:~/Desktop/node/datastores$ node postreSQL
/home/ryan/Desktop/node/datastores/postreSQL.js:8
if (err) throw err;
^
Error: connect ECONNREFUSED
at errnoException (net.js:901:11)
at Object.afterConnect [as oncomplete] (net.js:892:19)
Your password seems to be empty in both connections.
You need to get the connection details for the database from your devOps / SysAdmin / whoever created it.
Ask for the hostname, port, user and password, and schema name; then put those in your init object:
// MySQL:
var connectionConfig = {
host: "", // Hostname + ":" + port (no need for :port if 3360)
user: "", // User
password: "", // Password
database: "" // Schema
};
// PostgreSQL:
var connectionString = 'postgres://user:password#hostname:port';