2

running into some issues trying to figure out an Azure Function (node.js-based) can connect to our mysql database (also hosted on Azure). We're using mysql2 and following tutorials pretty much exactly (https://learn.microsoft.com/en-us/azure/mysql/connect-nodejs, and similar) Here's the meat of the call:

const mysql = require('mysql2');
const fs = require('fs');

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.fname || (req.body && req.body.fname)) {
        
        context.log('start');
        var config = {
            host:process.env['mysql_host'],
            user: process.env['mysql_user'],
            password: process.env['mysql_password'],
            port:3306,
            database:'database_name',
            ssl:{
            ca : fs.readFileSync(__dirname + '\\certs\\cacert.pem')
            },
            connectTimeout:5000
        };

        const conn = mysql.createConnection(config);
        /*context.log(conn);*/

        conn.connect(function (err) {
            context.log('here'); 
            if (err) { 
                context.error('error connecting: ' + err.stack);
                context.log("shit is broke");
                throw err;
            }
            console.log("Connection established.");  
            
        });

        context.log('mid');
        conn.query('SELECT 1+1',function(error,results,fields) {
            context.log('here');
            context.log(error);
            context.log(results);
            context.log(fields);
        });

Basically, running into an issue where the conn.connect(function(err)... doesn't return anything - no error message, no logs, etc. conn.query works similarly.

Everything seems set up properly, but I don't even know where to look next to resolve the issue. Has anyone come across this before or have advice on how to handle?

Thanks!! Ben

Ben
  • 21
  • 2

2 Answers2

1

I believe the link that Baskar shared covers debugging your function locally

As for your function, you can make some changes to improve performance.

  1. Create the connection to the DB outside the function code otherwise it will create a new instance and connect every time. Also, you can enable pooling to reuse connections and not cross the 300 limit that the sandbox in which Azure Functions run has.
  2. Use the Promises along with async/await

You basically can update your code to something like this

const mysql = require('mysql2/promise');
const fs = require('fs');

var config = {
  host: process.env['mysql_host'],
  user: process.env['mysql_user'],
  password: process.env['mysql_password'],
  port: 3306,
  database: 'database_name',
  ssl: {
    ca: fs.readFileSync(__dirname + '\\certs\\cacert.pem')
  },
  connectTimeout: 5000,
  connectionLimit: 250,
  queueLimit: 0
};
const pool = mysql.createPool(config);

module.exports = async function(context, req) {
  context.log('JavaScript HTTP trigger function processed a request.');

  if (req.query.fname || (req.body && req.body.fname)) {
    context.log('start');

    const conn = await pool.getConnection();

    context.log('mid');

    await conn.query('SELECT 1+1', function(error, results, fields) {
      context.log('here');
      context.log(error);
      context.log(results);
      context.log(fields);
    });

    conn.release();
  }
};

PS: I haven't test this code as such but I believe something like this should work

PramodValavala
  • 6,026
  • 1
  • 11
  • 30
1

Debugging on serverless is challenging for obvious reasons. You can try one of the hacky solutions to debug locally (like Serverless Framework), but that won't necessarily help you if your issue is to do with a connection to a DB. You might see different behaviour locally.

Another option is to see if you can step debug using Rookout, which should let you catch the full stack at different points in the code execution and give you a good sense of what's failing and why.