Create a socket connection when using Node.js

Open a Unix socket connection to Cloud SQL for MySQL by using the Node.js npm mysql module.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


const createUnixSocketPool = async config => {
  const dbSocketPath = process.env.DB_SOCKET_PATH || '/cloudsql';

  // Establish a connection to the database
  return mysql.createPool({
    user: process.env.DB_USER, // e.g. 'my-db-user'
    password: process.env.DB_PASS, // e.g. 'my-db-password'
    database: process.env.DB_NAME, // e.g. 'my-database'
    // If connecting via unix domain socket, specify the path
    socketPath: `${dbSocketPath}/${process.env.INSTANCE_CONNECTION_NAME}`,
    // Specify additional properties here.

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.