简体   繁体   English

无法将 Azure 应用服务中的 NodeJS 应用连接到 Azure MySQL 数据库

[英]Unable to connect NodeJS app in Azure App Services to Azure MySQL Database

I have deployed an NodeJS (with ExpressJS, Sequelize) to Azure App Services.我已将 NodeJS(使用 ExpressJS、Sequelize)部署到 Azure 应用服务。 The simple APIs with no database connection work, however when I use the part of the app where I load the data from my Azure MySQL Database, it's having problem with certificate.没有数据库连接的简单 API 可以工作,但是当我使用从 Azure MySQL 数据库加载数据的应用程序部分时,证书出现问题。 I get this log message from App Service Log Stream:我从应用服务日志 Stream 中收到此日志消息:

Unhandled rejection SequelizeConnectionError: unable to get local issuer certificate

在此处输入图像描述

I have followed the steps from here on how to enable firewall and use the certificate (BaltimoreCyberTrustRoot.crt.pem).我已按照此处关于如何启用防火墙和使用证书 (BaltimoreCyberTrustRoot.crt.pem) 的步骤进行操作。

Using the same instruction, from my laptop, I can connect to the remote Azure MySQL database, using this CLI:使用相同的指令,从我的笔记本电脑,我可以使用这个 CLI 连接到远程 Azure MySQL 数据库:

$ mysql -h <my-db>.mysql.database.azure.com -u <my-user> --ssl-mode=REQUIRED --ssl-ca=.\BaltimoreCyberTrustRoot.crt.pem -p

I have followed other StackOverflow / Github questions related to this and I followed their configurations like this:我已经关注了与此相关的其他 StackOverflow / Github 问题,并且我遵循了他们的配置,如下所示:

const mysql = require('mysql2');
...
var sequelize = new Sequelize(config.db, config.username, config.password, {
    host:    "<my-db>.mysql.database.azure.com",
    port:    3306,
    dialect: 'mysql',
    dialectOptions: {
        ssl: {
            ca: fs.readFileSync(__dirname + "/ssl/BaltimoreCyberTrustRoot.crt.pem")
        }
    }
});

Do I need to set additional key/cert under ssl ?我是否需要在ssl下设置额外的密钥/证书?

ssl: {
    key: fs.readFileSync(__dirname + "./certs/client-key.pem"),
    cert: fs.readFileSync(__dirname + "./certs/client-cert.pem"),
    ca: fs.readFileSync(__dirname + "/ssl/BaltimoreCyberTrustRoot.crt.pem")
}

I am using Node 12 (Node 12.13 in Azure App Service) and here's my package.json dependencies:我正在使用节点 12(Azure 应用服务中的节点 12.13),这是我的package.json依赖项:

"dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "faker": "^4.1.0",
    "mysql2": "^2.1.0",
    "sequelize": "^5.21.5"
}

According my test, I can connect to mysql.根据我的测试,我可以连接到 mysql。 You can add additional key/cert yes or not.您可以添加额外的密钥/证书是与否。

If you add, you can see my code like below,如果你添加,你可以看到我的代码,如下所示,

/*configuration*/
const sequelizeInstance  = new Sequelize("***db", "azure_root@***mysql", "Ja***.****20", {
host: "***mysql.mysql.database.azure.com",
dialect: 'mysql',
dialectOptions: {
    ssl: {
        ca: fs.readFileSync(path.resolve(__dirname, 'BaltimoreCyberTrustRoot.crt.pem'));
    }
}
});

/*test connection*/
try
{
    sequelizeInstance.authenticate();
    console.log('Connection has been established successfully.');
} 
catch (error) {
    console.error('Unable to connect to the database:', error);
}

If not, you just set ssl=true like the document .如果没有,您只需像文档一样设置ssl=true

Hope it's useful to u.希望它对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM