Bad handshake or ECONNRESET Azure Mysql Nodejs - mysql

Similar issues have been reported, but I can't work around with there solutions.
I have an express.js app using KNEX to connect to a mysql database hosted in Azure.
I can connect to the DB perfectly from console command or mysql workbench.
However, no way to achieve the same from my node app.
My connection object is the same though:
config.knex = require('knex')({
client: 'mysql',
connection: {
host: 'wineserver.mysql.database.azure.com',
user: 'BurgerAndGreenBeans#wineserver',
password: 'FromageDeChevre',
database: 'gdpr',
insecureAuth: true
},
pool: {
min: 0
},
debug: ['ComQueryPacket']
});
Everytime I start the app, I get alternatively either one error or another one:
#1
Unhandled rejection Error: read ECONNRESET
at exports._errnoException (util.js:1050:11)
at TCP.onread (net.js:582:26)
--------------------
at Protocol._enqueue (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Projects\gdpr\node_modules\mysql\lib\Connection.js:130:18)
at C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:106:18
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
at Client_MySQL.acquireRawConnection (C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:104:12)
at Object.create (C:\Projects\gdpr\node_modules\knex\lib\client.js:239:16)
at Pool._createResource (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:354:17)
at Pool.dispense [as _dispense] (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:314:10)
at Pool.acquire (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:436:8)
at C:\Projects\gdpr\node_modules\knex\lib\client.js:289:19
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
Process finished with exit code 0
or
#2
Unhandled rejection Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (C:\Projects\gdpr\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (C:\Projects\gdpr\node_modules\mysql\lib\protocol\sequences\Handshake.js:103:18)
at Protocol._parsePacket (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:280:23)
at Parser.write (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Parser.js:75:12)
at Protocol.write (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Projects\gdpr\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:561:20)
--------------------
at Protocol._enqueue (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Projects\gdpr\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Projects\gdpr\node_modules\mysql\lib\Connection.js:130:18)
at C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:106:18
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
at Client_MySQL.acquireRawConnection (C:\Projects\gdpr\node_modules\knex\lib\dialects\mysql\index.js:104:12)
at Object.create (C:\Projects\gdpr\node_modules\knex\lib\client.js:239:16)
at Pool._createResource (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:354:17)
at Pool.dispense [as _dispense] (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:314:10)
at Pool.acquire (C:\Projects\gdpr\node_modules\generic-pool\lib\generic-pool.js:436:8)
at C:\Projects\gdpr\node_modules\knex\lib\client.js:289:19
at Promise._execute (C:\Projects\gdpr\node_modules\bluebird\js\release\debuggability.js:300:9)
at Promise._resolveFromExecutor (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:483:18)
at new Promise (C:\Projects\gdpr\node_modules\bluebird\js\release\promise.js:79:10)
Process finished with exit code 0
It seems to be a kind of incompatibility between Node and Azure's mysql server.
Did anyone experienced the same ?

The similar question: Connecting to Azure Database for MySQL in npm/mysql. As #Gary Liu pointed out, you may use pull 1730 for a workaround.
Please modify the package.json as:
"dependencies": {
"knex": "^0.13.0",
"mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95"
}
then, run npm install to install this version.

Related

Error: Packets out of order. Got: 0 Expected: 3

I'm using the MySQL module for NodeJS, however I keep getting the following error after approximately 10 requests to the database. I'm using the following code to connect to the DB:
var con = mysql.createConnection({
host: "10.37.100.15",
user: process.env.MYSQLUSR,
password: process.env.MYSQLPASSWD,
database: process.env.MYSQLDB
});
Then I use con.query(sqlStatement, function (err, result, fields) {<function>}). I already tried changing the max_allowed_packet to 500M on the server but this didn't fix the issue. This is the full error:
events.js:377
throw er; // Unhandled 'error' event
^
Error: Packets out of order. Got: 0 Expected: 3
at Parser._tryReadPacketHeader (/app/node_modules/mysql/lib/protocol/Parser.js:470:15)
at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:33:29)
at Protocol.write (/app/node_modules/mysql/lib/protocol/Protocol.js:38:16)
at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:88:28)
at Socket.<anonymous> (/app/node_modules/mysql/lib/Connection.js:526:10)
at Socket.emit (events.js:400:28)
at addChunk (internal/streams/readable.js:293:12)
at readableAddChunk (internal/streams/readable.js:267:9)
at Socket.Readable.push (internal/streams/readable.js:206:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
Emitted 'error' event on Connection instance at:
at Connection._handleProtocolError (/app/node_modules/mysql/lib/Connection.js:423:8)
at Protocol.emit (events.js:400:28)
at Protocol._delegateError (/app/node_modules/mysql/lib/protocol/Protocol.js:398:10)
at Protocol.handleParserError (/app/node_modules/mysql/lib/protocol/Protocol.js:380:10)
at Parser._tryReadPacketHeader (/app/node_modules/mysql/lib/protocol/Parser.js:478:10)
at Parser.write (/app/node_modules/mysql/lib/protocol/Parser.js:33:29)
[... lines matching original stack trace ...]
at Socket.emit (events.js:400:28) {
code: 'PROTOCOL_PACKETS_OUT_OF_ORDER',
fatal: true
}
default wait_timeout is 8 hrs.
I'd suspect the mysql-nodejs version isn't resetting the expected packet count on re-connection.
In a test environment you can reduce the wait_timeout.
It quite possibly is:
https://github.com/mysqljs/mysql/issues/2534
https://github.com/mysqljs/mysql/issues/2451
Last comment on #2534 is "I change my connection from mysql.createConnection to mysql.createPool, the error never happen again"

I cannot connect to mysql database using node.js

I am total beginner to database.
I am using localhost(127.0.0.1) with port 9090.
I successfully created mysql database, ran/used it in windows 10 command prompt and MySQL Workbench.
But when I try to run it with node.js, I fail to connect to server.
Below is index.js file I tried to run in windows command line.
var mysql = require('mysql');
var client = mysql.createConnection({
user:'root',
password:'password',
});
client.query('USE mydb');
client.query('SELECT * FROM PRODUCTS',function(error, result, fields){
if(error){
console.log(error);
} else{
console.log(result);
}
});
And it results in this error.
D:\Coding\VSCode\nodejs>node index.js
Error: connect ECONNREFUSED 127.0.0.1:3306
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
}
I am at the stage not knowing what I am not knowing.
I tried to change port in createConnection() function like this
var client = mysql.createConnection({
user:'root',
password:'password',
port: '9090'
});
Then I get this error.
D:\Coding\VSCode\nodejs>node index.js
Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by
server; consider upgrading MySQL client
at Handshake.Sequence._packetToError
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
at Handshake.ErrorPacket
(D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
at Protocol._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:291:23)
at Parser._parsePacket (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:433:10)
at Parser.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Parser.js:43:10)
at Protocol.write (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:38:16)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:88:28)
at Socket.<anonymous> (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:526:10)
at Socket.emit (events.js:315:20)
at addChunk (_stream_readable.js:309:12)
--------------------
at Protocol._enqueue (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:116:18)
at Connection._implyConnect (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:454:10)
at Connection.query (D:\Coding\VSCode\nodejs\node_modules\mysql\lib\Connection.js:196:8)
at Object.<anonymous> (D:\Coding\VSCode\nodejs\index.js:10:8)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading
MySQL client',
sqlState: '08004',
fatal: true
}
1.Open your windows terminal and login :
mysql -uroot -p
( then type you password )
use mysql;
update user set authentication_string=password(''), plugin='mysql_native_password' where user='root';
4 Restart you mysql db : sudo service mysql restart
Doing this in MySQL Workbench solved the problem.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password
BY 'YourRootPassword';
FLUSH PRIVILEGES;

Packets out of order. Got: 80 Expected: 0 node.js

Here is my (very simple) code :
var connection = mysql.createConnection(infosDB);
connection.connect()
connection.query(`SELECT * FROM action`, function(err, data){
console.log(data);
})
The action table is not empty... I think my database is ok.
This code works very well for me on localhost.
However, I bought A2 Hosting to launch node.js (v6.10.3 on localhost and server) project and I get that error when I try to launch this simple code.
{ Error: Packets out of order. Got: 80 Expected: 0
at Parser.write (/home/hello1234/public_html/myapp/node_modules/mysql/lib/protocol/Parser.js:42:19)
at Protocol.write (/home/hello1234/public_html/myapp/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/hello1234/public_html/myapp/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:547:20)
--------------------
at Protocol._enqueue (/home/hello1234/public_html/myapp/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/hello1234/public_html/myapp/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/hello1234/public_html/myapp/node_modules/mysql/lib/Connection.js:130:18)
at update_infos_all_users (/home/hello1234/public_html/myapp/app.js:570:9)
at Job.job (/home/westudec/public_html/myapp/app.js:1566:3)
at Job.invoke (/home/westudec/public_html/myapp/node_modules/node-schedule/lib/schedule.js:172:10)
at Timeout._onTimeout (/home/hello1234/public_html/myapp/node_modules/node-schedule/lib/schedule.js:542:11)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5) code: 'PROTOCOL_PACKETS_OUT_OF_ORDER', fatal: true }
The weird thing is that all work good on my computer.
Does someone have an idea ?
Thanks

Connecting to Azure Database for MySQL in npm/mysql

I am getting an error when connecting to Azure Database for MySQL with the mysql npm module (version 2.13.0):
Error:
/Users/sascha/Desktop/azure-mysql/index.js:19
if (error) throw error;
^
Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/sequences/Handshake.js:103:18)
at Protocol._parsePacket (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Parser.js:75:12)
at Protocol.write (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at TLSSocket.ondata (_stream_readable.js:528:20)
at emitOne (events.js:77:13)
at TLSSocket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:146:16)
at TLSSocket.Readable.push (_stream_readable.js:110:10)
--------------------
at Protocol._enqueue (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:141:48)
at Protocol.handshake (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/Users/sascha/Desktop/azure-mysql/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/Users/sascha/Desktop/azure-mysql/index.js:16:12)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.runMain [as _onTimeout] (module.js:467:10)
at Timer.listOnTimeout (timers.js:92:15)
Code:
var mysql = require("mysql");
var fs = require("fs");
var connection = mysql.createConnection({
host: "host",
user: "user#db",
password: "pw",
database: "scheme",
port: 3306,
ssl: {
ca: fs.readFileSync(__dirname + "/cert.pem")
},
insecureAuth: true
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
Connecting to the database via MySQL Workbench works fine.
This is a known issue as our engineers have a disscusion at https://social.msdn.microsoft.com/Forums/en-US/c8fedbcc-909d-41ce-8c72-0374f76fdf82/cannot-connect-from-nodejs?forum=AzureDatabaseforMySQL.
And currently, I have test the solution provided by #Bradley Grainger at Cannot connect Azure Web App - NodeJS to Azure Mysql. We also can see the gist at https://github.com/mysqljs/mysql/commit/e3e123e9af7c0829a6c1417d911572a75b4a5f95.
Currently, please modify the package.json as:
"dependencies": {
"mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95"
},
then, run npm install to install this version.
It should work as expected.
Additionally, please wait for the maintainer merges this PR.

node-webkit MySQL connection Error - ER_HANDSHAKE_ERROR: Bad handshake

I am using Node-Webkit for creating desktop application using NodeJS.
In this i need to get data from MySQL database. For this am using node-mysql module and the following code is used to connect MySQL server using node-mysql.
var mysql_con = require('mysql');
var connection = mysql_con.createConnection({
host : input.host,
user : input.username,
password : input.password,
database : input.database
});
connection.connect(function(err) {
if (err) {
console.log('Error : '+err.stack);
}
console.log('Success');
});
if i run in the command line of my Windows machine, connection has been established and Success has been printed in console. But if i pack and run the code in the node-webkit's nw.exe its giving the following error in web console
Error : Error: ER_HANDSHAKE_ERROR: Bad handshake
at Handshake.Sequence._packetToError (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14)
at Handshake.ErrorPacket (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\sequences\Handshake.js:101:18)
at Protocol._parsePacket (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:270:23)
at Parser.write (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Parser.js:77:12)
at Protocol.write (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\Connection.js:82:28)
at Socket.EventEmitter.emit (events.js:104:17)
at readableAddChunk (_stream_readable.js:156:16)
at Socket.Readable.push (_stream_readable.js:123:10)
at TCP.onread (net.js:520:20)
--------------------
at Protocol._enqueue (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:135:48)
at Protocol.handshake (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\DOCUME~1\admin\LOCALS~1\Temp\nw4480_11607\node_modules\mysql\lib\Connection.js:108:18)
at Object.mysql.testConnection
Kindly suggest me solution to establish the MySQL connection through node-webkit.
i had the same problem... and downgrading node webkit to the version 0.8.6 did the trick...
i hope this help you...