All Cloud SQL for MySQL code samples
This page contains code samples for Cloud SQL for MySQL. To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.
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.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- JavaScript
- Node JS
MySQL—mysql connection
Run a SQL INSERT statement by opening and closing a connection to Cloud SQL for MySQL by using the Node.js mysql module.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Node JS
- JavaScript
Set connection pool and overflow limits when using SQLAlchemy
Configure the connection pool size and overflow when connecting to Cloud SQL for MySQL by using SQLAlchemy.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Python
Open or close a connection when using ADO.NET
Run a SQL INSERT statement to open and close a connection to Cloud SQL for MySQL by using the ADO.NET System.Data.Common package.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- C#
Configure SSL certificates for TCP connection by using Go
Configure SSL (Secure Sockets Layer) certificates for a TCP connection to Cloud SQL for MySQL by using Go's database/sql package.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Go
Create a source instance for cloning
This sample demonstrates how to create a source Cloud SQL for MySQL instance for cloning.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Terraform
Create a connection using the Cloud SQL Java Connector
Open a connection to Cloud SQL for MySQL by using the Cloud SQL Java Connector.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Java
Add database flags
This sample adds database flags general_log, skip_show_database and wait_timeout.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Terraform
Create a socket connection by using Go
Open a Unix socket connection to Cloud SQL for MySQL by using Go's database/sql package.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Go
Create a socket connection when using ADO.NET
Open a Unix socket connection to Cloud SQL for MySQL by using an ADO.NET Connection object.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- C#
Retry a failed connection when using HikariCP
Retry a failed connection when connecting to Cloud SQL for MySQL by using the HikariCP JDBC connection pool library.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Java
Set connection pool and overflow limits when using Go
Configure the connection pool size and overflow when connecting to Cloud SQL for MySQL by using Go's database/sql package.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Go
Create a read replica Cloud SQL for MySQL instance
This sample demonstrates how to create a read replica of the primary Cloud SQL for MySQL instance. A read replica reflects changes to the primary in almost real time, in normal circumstances.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Terraform
Set the connection duration when using SQLAlchemy
Configure the maximum number of seconds a connection can persist when connecting to Cloud SQL for MySQL by using SQLAlchemy.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Python
Create a connection using the Cloud SQL Go Connector
Open a connection to Cloud SQL for MySQL by using the Cloud SQL Go Connector.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Go
Manage connections by using ActiveRecord
Insert data using ActiveRecord to manage opening and closing a connection to Cloud SQL for MySQL.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Ruby
Create a TCP connection with SSL certificates when using Node.js
Open a TCP connection to Cloud SQL for MySQL by using the Node.js npm mysql module with SSL (Secure Sockets Layer) certificates.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Node JS
- JavaScript
MySQL PDO connection
Run a SQL INSERT statement to open and close a connection to Cloud SQL for MySQL by using the PHP Data Objects (PDO) extension.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- PHP
Create a TCP connection when using Node.js
Open a TCP connection to Cloud SQL for MySQL by using the Node.js npm mysql module.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- JavaScript
- Node JS
Set the connection timeout when using HikariCP
Configure a connection timeout when connecting to Cloud SQL for MySQL by using the HikariCP JDBC connection pool library.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Java
Clone a Cloud SQL for MySQL Enterprise Plus Instance
Create a clone of a MySQL Enterprise instance
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform
Create a connection using the Cloud SQL Python Connector
Open a connection to Cloud SQL for MySQL by using the Cloud SQL Python Connector.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Python
Set the connection timeout when using SQLAlchemy
Configure the connection timeout when connecting to Cloud SQL for MySQL by using SQLAlchemy.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Python
Enforce SSL/TLS encryption
This sample demonstrates how to enforce SSL/TLS encryption for a Cloud SQL for MySQL instance.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Terraform
MySQL SQLAlchemy opening and closing connections
Run a SQL INSERT statement to open and close a connection to Cloud SQL for MySQL by using SQLAlchemy.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Python
Set connection pool and overflow limits when using Node.js
Configure the connection pool size and overflow when connecting to Cloud SQL for MySQL using the Node.js npm mysql module.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- JavaScript
- Node JS
Set a custom location for backups
This sample sets a custom location for backups to asia-northeast1.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Terraform
Create an instance with password policy enabled
This sample creates an instance with password policy enabled.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Terraform
Create a socket connection by using Python
Open a Unix socket connection to Cloud SQL for MySQL by using Python.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Python
Set the connection duration when using ADO.NET
Configure the maximum number of seconds a connection can persist when connecting to Cloud SQL for MySQL by using the ADO.NET Connection object.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- C#
Configure SSL certificates for TCP connection by using Python
Configure SSL (Secure Sockets Layer) certificates for a TCP connection to Cloud SQL for MySQL by using Python's sqlalchemy package.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Python
Create a socket connection by using PHP
Open a Unix socket connection to Cloud SQL for MySQL by using PHP.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- PHP
Create a TCP connection when using ADO.NET
Open a TCP connection to Cloud SQL for MySQL by using an ADO.NET Connection object.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- C#
Set the connection timeout when using PHP
Configure a connection timeout when connecting to Cloud SQL for MySQL by using the PHP Data Objects (PDO) extension.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- PHP
Create a socket connection by using Java
Open a Unix socket connection to Cloud SQL for MySQL by using Java.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Java
Configure public IP for a instance
This sample enables public IP and adds an authorized address range for a instance.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform
Create a TCP connection by using Java
Open a TCP connection to Cloud SQL for MySQL by using Java
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Java
Create a primary MySQL instance for replication
This sample demonstrates how to create a primary Cloud SQL for MySQL instance for replication.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Terraform
Set the connection timeout when using Node.js
Configure a connection timeout when connecting to Cloud SQL for MySQL by using the Node.js npm mysql module.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- JavaScript
- Node JS
Set automated backup retention for an instance
This sample specifies the number of automated backups to retain for an instance.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform
Create MySQL Private IP
Sample demonstrating how to create the private IP for use with a MySQL Database.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Terraform
Clone a Cloud SQL for MySQL instance
This Terraform shows how to create a clone of a MySQL instance
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Terraform
Retry a failed connection when using Node.js
Retry a failed connection when connecting to Cloud SQL for MySQL by using the Node.js npm mysql module.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Node JS
- JavaScript
Configure authorized networks
This sample configures authorized networks to IP address range 192.0.2.0/24.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform
Set the connection timeout when using ADO.NET
Configure a connection timeout when connecting to Cloud SQL for MySQL by using the ADO.NET Connection object.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- C#
Set the connection timeout when using Go
Configure a connection timeout when connecting to Cloud SQL for MySQL by using Go's database/sql package.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Go
Create a TCP connection by using Python
Open a TCP connection to Cloud SQL for MySQL by using Python.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Python
Implement exponential backoff when using ADO.NET
Implement exponential backoff to retry failed connections when connecting to Cloud SQL for MySQL by using ADO.NET with the Polly .NET library.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- C#
Set connection pool and overflow limits when using ADO.NET
Configure the connection pool size and overflow when connecting to Cloud SQL for MySQL by using the ADO.NET Connection object.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- C#
Set the connection duration when using HikariCP
Set the maximum number of seconds a connection can persist when connecting to Cloud SQL for MySQL by using the HikariCP JDBC connection pool library.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Java
Create a user in MySQL instance
This sample demonstrates how to create a new user in a given Cloud SQL for MySQL instance.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform
Create a replica Cloud SQL for MySQL instance
This Terraform shows how to create a replica MySQL instance
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Terraform
Create a TCP connection by using PHP
Open a TCP connection to Cloud SQL for MySQL by using PHP.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- PHP
Run an SQL INSERT statement using Go
Run an SQL INSERT statement by opening and closing a connection to Cloud SQL for MySQL using Go's database/sql package.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Go
Schedule automated backups for an instance
This sample schedules automated backups for an instance starting within 4 hours from 20:55.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Terraform
Retry a failed connection when using SQLAlchemy
Retry a failed connection when connecting to Cloud SQL for MySQL by using SQLAlchemy.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Python
Create a TCP connection by using Go
Open a TCP connection to Cloud SQL for MySQL by using Go's database/sql package.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Go
MySQL servlet connection
Run a SQL INSERT statement to open and close a connection to Cloud SQL for MySQL by using the HikariCP JDBC connection pool library.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Java
Set connection pool and overflow limits when using HikariCP
Configure the connection pool size and overflow when connecting to Cloud SQL for MySQL by using the HikariCP JDBC connection pool library.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Java
Upgrade MySQL instance maintenance version
This sample demonstrates how to upgrade your Cloud SQL for MySQL instance to the target maintenance version.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Terraform
Upgrade the database major version in-place
This sample demonstrates how to upgrade the database major version by upgrading your Cloud SQL for MySQL instance in-place.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform