如何使用sequelize连接到Azure MySQL?
来自Azure nodejs示例:
const mysql = require('mysql2');
var config =
{
host: 'myserver4demo.mysql.database.azure.com',
user: 'myadmin@myserver4demo',
password: 'your_password',
database: 'quickstartdb',
port: 3306,
ssl: true
};
sequelize的配置是什么:
我尝试过使用ssl但没有成功:
ssl: true,
我收到了这个错误:
Unable to connect to database: SequelizeConnectionError: SSL connection is required. Please specify SSL options and retry.
我得到了这个工作:
dialectOptions: {
encrypt: true,
ssl : {
rejectUnauthorized: false
}
},
但在哪里找到证书?
答案 0 :(得分:0)
var conn = mysql.createConnection({
host: 'myserver4demo.mysql.database.azure.com',
user: 'myadmin@myserver4demo',
password: 'your_password',
database: 'quickstartdb',
port: 3306,
ssl: {
key: fs.readFileSync('./certs/client-key.pem'),
cert: fs.readFileSync('./certs/client-cert.pem')
}
});
<强> Configure SSL connectivity in your application to securely connect to Azure Database for MySQL 强>
答案 1 :(得分:0)
SSL方面缺少Sequelize文档,接受的答案并不像注释状态那样适用于Sequelize。
我也很难知道如何添加SSL选项,但在Github上发现了包含代码段的问题。
const config = {
username: process.env["DB_USER"],
password: process.env["DB_PASS"],
host: process.env["DB_HOST"],
dialect: "mysql",
database: dbs[process.env["USE_DB"]],
pool: {
max: 5,
idle: 30000,
acquire: 60000
},
dialectOptions: {
ssl: {
ca: fs.readFileSync(__dirname + '/ssl/BaltimoreCyberTrustRoot.crt.pem')
}
}
}
答案 2 :(得分:0)
对于我们来说,解决方案是针对提供商dialectOptions
,即
{
"host":<hostname>,
"port":<port>,
"user":<username>,
"password":<password>,
"port": 3306,
"dialect": "mysql",
"ssl": true,
"dialectOptions": {
"ssl": {
"require": true
}
}
}