在 npm/mysql 中连接到 Azure Database for MySQL [英] Connecting to Azure Database for MySQL in npm/mysql

查看:44
本文介绍了在 npm/mysql 中连接到 Azure Database for MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 mysql npm 模块(版本 2.13.0)连接到 Azure Database for MySQL 时出现错误:

I am getting an error when connecting to Azure Database for MySQL with the mysql npm module (version 2.13.0):

错误:

/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)

代码:

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();

通过 MySQL Workbench 连接到数据库工作正常.

Connecting to the database via MySQL Workbench works fine.

推荐答案

这是一个已知问题,因为我们的工程师在 https://social.msdn.microsoft.com/Forums/en-US/c8fedbcc-909d-41ce-8c72-0374f76fdf82/cannot-connect-from-nodejs?forum=AzureDatabaseforMySQL.

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.

目前,我已经在 无法将 Azure Web 应用程序 - NodeJS 连接到 Azure Mysql.我们也可以在 https://github.com/mysqljs/mysql/commit/e3929a6c1417d911573.

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.

目前,请将package.json修改为:

"dependencies": {
    "mysql": "git://github.com/mysqljs/mysql#e3e123e9af7c0829a6c1417d911572a75b4a5f95"
  },

然后,运行 npm install 来安装这个版本.

then, run npm install to install this version.

它应该按预期工作.

另外,请等待维护者合并此PR.

Additionally, please wait for the maintainer merges this PR.

这篇关于在 npm/mysql 中连接到 Azure Database for MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆