0

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the workaround. This works for me. I am now waiting for the release.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.