I have a Nuxt 3 app deployed to Firebase Hosting and from this app I need to access a remote MySQL DB that is on a CPanel shared hosting. The Nuxt 3 app is client-server and uses the default Nitro server provided by Nuxt.
As you probably know, when using the Nitro server or Firebase Functions, there is a Function created in Cloud Run which is the actual server.
To test the app-to-database connection, I have allowed all hosts: (%.%.%.%) under CPanel → Manage Access Hosts.
Everything works fine directly from localhost, or through Firebase emulators, but not when deployed on Firebase hosting.
Also, HTTP requests from my Nuxt server endpoints to test resources such 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.
Can anyone confirm this?
I also tried to set an allow-all rule in VPC firewall rules (https://console.cloud.google.com/net-security/firewall-manager/firewall-policies/list) but with no success.
This is the error that I'm getting in 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 }
In the browser, I'm getting back:
FetchError: [GET] "/api/hello": 500
And 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 about how to solve this issue would be much appreciated.