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.
Configure authorized networks
This sample configures authorized networks to IP address range 192.0.2.0/24.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Terraform
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.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- C#
Create MySQL Private IP
Sample demonstrating how to create the private IP for use with a MySQL Database.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Terraform
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 for MySQL
- Cloud SQL
- Google Cloud
- Python
Create a Cloud SQL for MSSQL instance with a password validation policy
Creates a Cloud SQL for MSSQL instance with a password validation policy
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Terraform
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 for MySQL
- Cloud SQL
- Go
Create a replica Cloud SQL for MySQL instance
This Terraform shows how to create a replica MySQL 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
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
- Google Cloud
- Cloud SQL for MySQL
- Node JS
- JavaScript
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.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Java
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.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Go
Set automated backup retention for an instance
This sample specifies the number of automated backups to retain for an instance.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Terraform
Create a socket connection by using PHP
Open a Unix socket connection to Cloud SQL for MySQL by using PHP.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- PHP
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
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.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- JavaScript
- Node JS
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 for MySQL
- Cloud SQL
- Python
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.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Java
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
- Google Cloud
- Cloud SQL for MySQL
- Go
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 for MySQL
- Google Cloud
- Cloud SQL
- Go
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.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- C#
Create a TCP connection by using Python
Open a TCP connection to Cloud SQL for MySQL by using Python.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Python
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 for MySQL
- Cloud SQL
- Google Cloud
- Java
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
- Cloud SQL for MySQL
- Google Cloud
- Python
Create a primary MySQL instance for replication
This sample demonstrates how to create a primary Cloud SQL for MySQL instance for replication.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Terraform
Create a TCP connection by using Java
Open a TCP connection to Cloud SQL for MySQL by using Java
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- Java
Schedule automated backups for an instance
This sample schedules automated backups for an instance starting within 4 hours from 20:55.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform
Create a source instance for cloning
This sample demonstrates how to create a source Cloud SQL for MySQL instance for cloning.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- Terraform
Clone a Cloud SQL for MySQL instance
This Terraform shows how to create a clone of a MySQL instance
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Terraform
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
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
- Cloud SQL for MySQL
- Google Cloud
- Terraform
Add database flags
This sample adds database flags general_log, skip_show_database and wait_timeout.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- Terraform
Clone a Cloud SQL for MySQL Enterprise Plus Instance
Create a clone of a MySQL Enterprise instance
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- 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 for MySQL
- Google Cloud
- Cloud SQL
- Java
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
- Cloud SQL for MySQL
- Google Cloud
- Java
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
- Google Cloud
- Cloud SQL
- Python
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
- Cloud SQL
- Google Cloud
- C#
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
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 for MySQL
- Cloud SQL
- Google Cloud
- Node JS
- JavaScript
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 for MySQL
- Cloud SQL
- Google Cloud
- Node JS
- JavaScript
Create a socket connection by using Python
Open a Unix socket connection to Cloud SQL for MySQL by using Python.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Python
Create an instance with password policy enabled
This sample creates an instance with password policy enabled.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- 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
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.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- PHP
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.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Go
Create a socket connection by using Java
Open a Unix socket connection to Cloud SQL for MySQL by using Java.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Java
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.
- Cloud SQL
- Google Cloud
- Cloud SQL for MySQL
- JavaScript
- Node JS
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
- Google Cloud
- Cloud SQL for MySQL
- Terraform
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
- Cloud SQL
- Google Cloud
- Terraform
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
- Google Cloud
- Cloud SQL
- Java
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 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
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
- Cloud SQL for MySQL
- Go
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 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.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- PHP
Create a TCP connection by using PHP
Open a TCP connection to Cloud SQL for MySQL by using PHP.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- PHP
Set a custom location for backups
This sample sets a custom location for backups to asia-northeast1.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Terraform
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 for MySQL
- Google Cloud
- Cloud SQL
- C#
Upgrade MySQL instance maintenance version
This sample demonstrates how to upgrade your Cloud SQL for MySQL instance to the target maintenance version.
- Cloud SQL for MySQL
- Cloud SQL
- Google Cloud
- Terraform
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.
- Google Cloud
- Cloud SQL for MySQL
- Cloud SQL
- Node JS
- JavaScript
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.
- Cloud SQL
- Cloud SQL for MySQL
- Google Cloud
- C#
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.
- Google Cloud
- Cloud SQL
- Cloud SQL for MySQL
- C#
Enforce SSL/TLS encryption
This sample demonstrates how to enforce SSL/TLS encryption for a Cloud SQL for MySQL instance.
- Cloud SQL for MySQL
- Google Cloud
- Cloud SQL
- Terraform