使用此代码进行连接,在本地工作。我甚至可以从本地计算机连接到 Cloud SQL 数据库。
options = {
host: 'cloud.sql.ip',
user: 'root',
password: 'password12345',
database: 'dbname',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
};
const pool = require('mysql2/promise').createPool(options);
部署时出现此错误:rror: connect ETIMEDOUT at PoolConnection.Connection._handleTimeoutError (/app/node_modules/mysql2/lib/connection.js:192:13) at ontimeout (timers.js:498:11) at tryOnTimeout (timers.js:323:5) at Timer.listOnTimeout (timers.js:290:5)
App.yaml 看起来像这样:
runtime: nodejs8
service: servicename
beta_settings:
cloud_sql_instances: project-id:us-central1:instance-id
beta_settings cloud_sql_instances
设置适用于 App Engine 灵活环境。
在 App Engine 标准环境中,您可以使用以下套接字路径直接访问 CloudSQL:/cloudsql/[YOUR_INSTANCE_NAME]