简体   繁体   中英

How to connect an Azure App services to Azure Database for MySQL Flexible Server

I am working on an Azure app services in conjunction with a flexible mysql database server. I have successfully deployed my website to NodeJS v18.LTS, but my server is Throwing: SequelizeHostNotFoundError: getaddrinfo ENOTFOUND mynameserver.mysql.database.azure.com in my app services log stream. In the following question I find a possible solution by adding the ip address of the connecting host to my database instance instead of a FQDN https://stackoverflow.com/questions/25521755/errorerror-getaddrinfo-enotfound-mysql .

However, this configuration is completely discouraged.

https://techcommunity.microsoft.com/t5/azure-database-for-postgresql/dns-configuration-patterns-for-azure-database-for-postgresql/ba-p/2560287

How can I correctly set up my Flexible Server for MySQL instance to work in my production App Services environment without violating this policy?

this is my connection instance configuration:

const sequelize = new Sequelize(
process.env.DATABASE,
process.env.USER, 
process.env.MySQLPASSWORD,
{
    host: process.env.HOST, // String conection xxxx.mysql.database.azure.com
    dialect: process.env.dialect,

});
  • here I have an alternate approach of connecting to azure MySQL flexible server where I have used mysql2 npm package.

  • now here I am directly hard coding config data in the code, but you can easily read the application setting using same way you have used before just make sure that you first reading the respective setting for eg: username in a variable and then add that variable while configuring the connection to MySQL.

var username = process.env.USER
  • Here we use the create connection function to connect to the MySQL database and then use the query function to runa query.

The below is code form an express api:

app.use('/', (req,res)=>{
    const  mysql = require('mysql2');
    const  connection = mysql.createConnection({
        host:'',
        user:'',
        database:'',
        password:'',
        port:'',
    });

    connection.query("CREATE TABLE TESTTABLE ( TEST int)",(err)=>{
        console.log(err);
    });
    
    res.send("Hello World");
});

Here I have connected the database to MySQL Workbench where I created the table using the above code.

在此处输入图像描述

Here in the server I have disabled the ssl mandate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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