Home > Mobile >  Azure Function not connecting to mysql after deploy NodeJS
Azure Function not connecting to mysql after deploy NodeJS

Time:01-13

I've made a simple Azure function that when executed will run an insert query to a MySQL database. The MySQL database lives on a HostGator server (shared plan). The Azure function I wrote is able to insert to the DB table as expected when running locally, but after I deploy to Azure the function appears to run fine, but then no new records show in the database table.

Below is the function code:

const mysql = require('mysql');

module.exports = async function (context, req) {
    var connection = mysql.createConnection({
        host: '*****************',
        user: '*************',
        password: '*************',
        database: '***************'
    });

    connection.connect();

    const insertQuery = `INSERT into emails (email_address, first_name, last_name, preferred_game, date_joined) VALUES ('[email protected]', 'Bob', 'Jones', 'both', NOW());`;
    await connection.query(insertQuery, function (error, results, fields) {
        if (error) throw error;
      });

    connection.end();
    context.res = {
        // status: 200, /* Defaults to 200 */
        body: '200'
    };
}

The strange thing is that I don't see any errors in the logs anywhere, so it appears to work, but when I look in the DB no new records have been inserted. I thought at first it might be an IP whitelist issue, but after adding the Azure Function App IP it still failed to insert a record to the DB (Virtual IP as shown under app properties in the Azure portal).

I can't think of why else this wouldn't be able to insert records to the DB from the deployed Azure function.

CodePudding user response:

What you've whitelisted, sounds like the "Inbound" IP address. It may be because you haven't whitelisted your "outbound" IP address(es).

Get the outbound IPs with az CLI

az functionapp show --resource-group <GROUP_NAME> --name <APP_NAME> --query outboundIpAddresses --output tsv

Or get the outbound IPs in the portal properties (where you found Virtual IP).

enter image description here

See here for more details

  • Related