Announcements
This site is in read only until July 22 as we migrate to a new platform; refer to this community post for more details.
Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Connect from Google Cloud Functions to external MySQL database (third party MySQL DB)

Is it possible to have a cloud function fetch data from an external MySQL DB?

I googled for this topic but couldn’t find any answer.

I have a Nuxt 3 app in Firebase Hosting and I need to access this remote MySQL DB that is on a CPanel shared hosting. To test, I have allowed all hosts (%.%.%.%) in CPanel > Manage Access Hosts.

Everything works fine with Firebase emulators from my localhost, but not from Firebase hosting / functions. Other Nuxt server endpoints that are accessing external resources as:
https://jsonplaceholder.typicode.com/users are working.
Seems like there is a firewall rule in GCP that is blocking outgoing access for mysql protocol...

I also tried with an "allow-all" rule in VPC firewall rules (https://console.cloud.google.com/net-security/firewall-manager/firewall-policies/list) with no success.

The connection to the db gives me this error (GCP Logs Explorer):

Error: connect ETIMEDOUT at Object.createConnection
(/workspace/node_modules/mysql2/promise.js:253:31) at Object.handler
(file:///workspace/chunks/routes/api/hello.mjs:18:30) at Object.handler
(file:///workspace/chunks/_/nitro.mjs:2805:24) at Object.handler
(file:///workspace/chunks/_/nitro.mjs:3115:34) at Object.handler
(file:///workspace/chunks/_/nitro.mjs:2876:31) at process.processTicksAndRejections
(node:internal/process/task_queues:95:5) at async toNodeHandle
(file:///workspace/chunks/_/nitro.mjs:3147:7) { code: 'ETIMEDOUT', errno: undefined, sqlState: undefined }

Here is the code that I’m using:

// file hello.ts in my Nuxt 3 /server/api  folder
import mysql from "mysql2/promise";
import { User } from "~/server/models/User";

export default defineEventHandler(async (event) => {

    try {

// code throwing the error:

        const conn = await mysql.createConnection({

            host: <remote host>,
            user: <username>,
            database: <db name>,
            password: <db password>,

        });
//

 

        const [rows] = await conn.query("SELECT id FROM users");

        console.log(">>> users: ", rows);

        return rows as User[];

    } catch (error) {

        console.error(">>> Error:", error);

        return error;

    }

});

Any idea how to solve this issue?



0 2 434
2 REPLIES 2

-Rhett
Former Googler

Hi @xand,

Welcome to the Google Cloud Community!

Just a heads up, Firebase has key differences with Google Cloud, so my support is limited to Cloud Functions on Firebase and basic troubleshooting. If you want to get more help specifically for Firebase, Nuxt, or cPanel, I’d recommend the following channels:

In the meantime, we can try the following:

If none of these resolved the issue, feel free to contact Firebase support for more assistance.

I hope this helps!

Hi -Rhett,
Thanks for your answer.


 
 
 
 
 
 
 
 
 
 
 
 
Top Labels in this Space
Top Solution Authors