"What could be causing the 'Error: connect EACCES ::1:3306' when executing the line 'const connection = await mysql.createConnection' in my Node.js function within Azure Function App? I've verified that the credentials for the MySQL connection and token a

Ajal Jose 0 Reputation points
2024-02-07T03:53:38.1766667+00:00

Q)The Below is my node js function using azure function app .But when the line “const connection =await mysql.createConnection” starts executing i am getting an Error mentioned below “Error: connect EACCES ::1:3306     at Object.createConnection (C:\home\site\wwwroot\node_modules\mysql2\promise.js:253:31)     at handler (C:\home\site\wwwroot\src\functions\testfn.js:28:37)     at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {   code: 'EACCES',   errno: -4092,   sqlState: undefined }”. I have checked the credentials of mysql connection and token also , all those values are correct. //code  const { app } = require('@azure/functions'); const mysql = require('mysql2/promise'); const { DefaultAzureCredential,ClientSecretCredential } = require('@azure/identity'); app.http('testfn', {   methods: ['GET', 'POST'],   authLevel: 'anonymous',   handler: async (request, context) => {     try {       const credential = new DefaultAzureCredential();       var accessToken = await credential.getToken('https://ossrdbms-aad.database.windows.net/.default');       context.log('<__Begining>',accessToken);       const connection =await mysql.createConnection({         host: process.env.AZURE_MYSQL_HOST,         user: process.env.AZURE_MYSQL_USER,         password: accessToken.token,         database: process.env.AZURE_MYSQL_DATABASE,         port: process.env.AZURE_MYSQL_PORT       });       context.log('<__MID>');       connection.connect((err) => {         if (err) {           context.error('Error connecting to MySQL database: ' + err.stack);           return;         }         context.log('Connected to MySQL database');         return { body: 'Connection Success' };       });     } catch (err) {       context.log('Logging--------->', err);       let res = { status: 500, body: 'Internal Server Error',      };       return { body: JSON.stringify(res) };    }  } });

Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
617 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,298 questions
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
714 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
871 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MayankBargali-MSFT 68,641 Reputation points
    2024-02-12T06:08:53.4866667+00:00

    @Ajal Jose Thanks for reaching out.

    Your code looks good to me expect please validate if the values are retrieved correctly or not for the AZURE_MYSQL_USER, AZURE_MYSQL_DATABASE etc. and other values that you have defined.

    In case if you have already validated the above then another reason for this error is that the IP address of the client is not authorized to connect to the MySQL server. You can check if the IP address of the client is authorized to connect to the MySQL server by checking the firewall rules of the MySQL server.

    Another reason for this error is that the MySQL server is not configured to accept connections over SSL. You can check if the MySQL server is configured to accept connections over SSL by checking the SSL configuration of the MySQL server.

    In case if you have already validated it then to know more details on the 500 error you can validate the application insights logs or Diagnose and solve problems blade on your function app to know more details on the error and the steps to mitigate the issue. In you still not able to figure out the reason then please let me know.

    0 comments No comments