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

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

Related

Facing issue with mysql database with node js

i have created express app with mysql database.
when i call mysql query it works fine and then i call my view. loading view take few minute(2-3 min) and app crashed with bellow error.
events.js:287
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\Connection.js:423:8)
at Protocol.emit (events.js:310:20)
at Protocol.EventEmitter.emit (domain.js:482:12)
at Protocol._delegateError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.handleNetworkError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\protocol\Protocol.js:371:10)
at Connection._handleNetworkError (C:\Users\AMW\Desktop\dishmize\dishmize\node_modules\mysql\lib\Connection.js:418:18)
at Socket.emit (events.js:310:20)
at Socket.EventEmitter.emit (domain.js:482:12)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
[nodemon] app crashed - waiting for file changes before starting...
i already spend 8- 10 hours.
please help me to resolve this issue.
thanks
Update:
const options = {
user: config.get('MYSQL_USER'),
password: config.get('MYSQL_PASSWORD'),
database:config.get('DATABASE'),
host: config.get('HOST'),
port: 3306
}
const connection = mysql.createConnection(options);
connection.connect( function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:"); throw err;
} else {
console.log("Connection established.");
}
});
use below
const options = { connectionLimit :10, user:config.get('MYSQL_USER'), password: config.get('MYSQL_PASSWORD'), database:config.get('DATABASE'), host: config.get('HOST'), port: 3306 }
const connection_pool = mysql.createPool(options);
connection_pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
provide connectionLimit according to you use this is pool size of connection

The mysql server disconnects every night, how do I resolve it?

I leave my computer on 24 hours, but I still lose connection with the MySQL server. My WiFi runs smoothly and is never disconnected because I'm still able to use the internet along with my other electronics. The error message says
The server closed the connection.
Is this inferring a problem on my computer/connection or with MySQL? Or is it my codes? Please advise.
Error messages:
events.js:174
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Protocol.end (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\protocol\Protocol.js:112:13)
at Socket.<anonymous> (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:97:28)
at Socket.<anonymous> (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:525:10)
at Socket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at Connection._handleProtocolError (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:426:8)
at Protocol.emit (events.js:198:13)
at Protocol._delegateError (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Protocol.end (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\protocol\Protocol.js:116:8)
at Socket.<anonymous> (C:\Users\Nick\Desktop\MyBot\node_modules\mysql\lib\Connection.js:97:28)
[... lines matching original stack trace ...]
at process._tickCallback (internal/process/next_tick.js:63:19)
My codes:
let con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: 'db',
dateStrings: 'date',
});
const handleDisconnect = () => {
con.connect(err => {
if (err) throw err;
console.log("Connected!");
});
con.on('err', err => {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();

Connection lost The server closed the connection - Cannot enqueue Query after fatal error

I got an error once that the TCP connection is shut down by the server. The error looks the same as this question has
Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/node-v0.10.20-linux-x64/IM/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
So to solve this problem I used the answer posted in this question. I did this
var mysql = require('mysql');
var db_config = {
host : 'localhost',
user : 'xxxx',
password : 'xxxx',
database : 'xxxx'
}
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else {
console.log('error is err:', err); // connnection idle timeout (the wait_timeout
// throw err; // server variable configures this)
}
});
}
handleDisconnect();
module.exports = connection;
My program never crashes until today when I face some issue like user was not getting the notifications. since it has to do with the database. So when I checked the log files. this was the error
sql error isError: ER_QUERY_INTERRUPTED: Query execution was interrupted
db error { Error: Connection lost: The server closed the connection.
at Protocol.end (/var/www/html/mysite/node/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/var/www/html/mysite/node/node_modules/mysql/lib/Connection.js:97:28)
at Socket.<anonymous> (/var/www/html/mysite/node/node_modules/mysql/lib/Connection.js:502:10)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9) fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
error when connecting to db: { Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
and then it keeps printing this above error in my log files and along with this as well
Cannot enqueue Query after fatal error.
Please help me to solve this problem. I have to do a restart the server to make it work

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

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

Node and MySQL connection to database

I need t o provide the MySQL connection for modules, so I provide the code like this:
var express = require('express');
var mysql = require('mysql')
var app = express();
var connection = mysql.createConnection({
host: '149.xxx.xx.x',
user: 'User',
password: 'Password',
database: 'DataBase',
port: 1443,
});
connection.connect(
function(error) {
console.log(error.code); // 'ECONNREFUSED'
console.log(error.fatal); // true
console.log(error.sql);
console.log(error.sqlMessage);
}
);
and after some time (about 1-2 minutes) I received an error from those console.logs:
ECONNRESET
true
undefined
undefined
I think maybe it's the timeout because of my "nonactivity", but when I'm trying to do the query the errors are the same.
Have any idea what is wrong? I checked it from DataGrip and the data are correct.
Ok, I found one small bug here. The database is MS SQL Server (microsoft). I'm trying now using the library called mssql to connect to that database, provide some code:
var config = {
server: "149.xxx.xx.x",
database: "Database",
user: "User",
password: "Password",
connectionTimeout: 300000,
requestTimeout: 300000,
pool: {
idleTimeoutMillis: 300000,
max: 100
}
};
function getEmp() {
var connection = new sql.Connection(config);
var req = new sql.Request(connection);
connection.connect(function (error) {
if(error) {
console.log(error);
return;
}
req.query('Procedure', function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
connection.close();
});
});
}
getEmp();
and I receive the error:
{ ConnectionError: Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433
at Connection.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/mssql/lib/tedious.js:353:25)
at Connection.g (events.js:292:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:791:12)
at Socket.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:33:15)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
name: 'ConnectionError',
message: 'Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433',
code: 'ESOCKET' }
For sure the data are correct- DataGrip is connecting fine here.
I found at google the similar problem, the problem was Disabled TCP/IP. I checked this one and it's Enabled with this port 1443.