Connection fields
Open MySQL > Connection and copy Host, Port, Username, Password and Database/Schema. Use the schema shown in Console. Replace all example values, including the port, with actual service details.
Products in the same DLEZ organization communicate through private endpoints, including products in different projects. Use the destination service's private host and port. Your laptop and browser are external clients even when you are signed in to Console; use a public endpoint for external access. Keep credentials on the server and follow the service's TLS requirements.
MYSQL_HOST="copy-host-from-console"
MYSQL_PORT="copy-port-from-console"
MYSQL_USER="copy-from-console"
MYSQL_PASSWORD="copy-from-console"
MYSQL_DATABASE="copy-from-console"Node.js
Install mysql2 with npm install mysql2. Use the following in server-side code; configure TLS if required by your endpoint. A successful SELECT 1 verifies a query can run. Use an appropriately sized connection pool for a long-running application.
import mysql from "mysql2/promise";
const db = await mysql.createConnection({
host: process.env.MYSQL_HOST,
port: Number(process.env.MYSQL_PORT),
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
});
await db.query("SELECT 1");
await db.end();Go
Install github.com/go-sql-driver/mysql. This excerpt builds a DSN from environment variables; use it with database/sql, import the driver and check connection/query errors. Configure TLS according to your endpoint and do not log the DSN.
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s",
os.Getenv("MYSQL_USER"),
os.Getenv("MYSQL_PASSWORD"),
os.Getenv("MYSQL_HOST"),
os.Getenv("MYSQL_PORT"),
os.Getenv("MYSQL_DATABASE"),
)