Create a Cloud SQL connection

Add credentials to connect BigQuery to Cloud SQL.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Java

Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

importcom.google.cloud.bigquery.connection.v1.CloudSqlCredential;importcom.google.cloud.bigquery.connection.v1.CloudSqlProperties;importcom.google.cloud.bigquery.connection.v1.Connection;importcom.google.cloud.bigquery.connection.v1.CreateConnectionRequest;importcom.google.cloud.bigquery.connection.v1.LocationName;importcom.google.cloud.bigqueryconnection.v1.ConnectionServiceClient;importjava.io.IOException;// Sample to create a connection with cloud MySql databasepublicclassCreateConnection{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.StringprojectId="MY_PROJECT_ID";Stringlocation="MY_LOCATION";StringconnectionId="MY_CONNECTION_ID";Stringdatabase="MY_DATABASE";Stringinstance="MY_INSTANCE";StringinstanceLocation="MY_INSTANCE_LOCATION";Stringusername="MY_USERNAME";Stringpassword="MY_PASSWORD";StringinstanceId=String.format("%s:%s:%s",projectId,instanceLocation,instance);CloudSqlCredentialcloudSqlCredential=CloudSqlCredential.newBuilder().setUsername(username).setPassword(password).build();CloudSqlPropertiescloudSqlProperties=CloudSqlProperties.newBuilder().setType(CloudSqlProperties.DatabaseType.MYSQL).setDatabase(database).setInstanceId(instanceId).setCredential(cloudSqlCredential).build();Connectionconnection=Connection.newBuilder().setCloudSql(cloudSqlProperties).build();createConnection(projectId,location,connectionId,connection);}staticvoidcreateConnection(StringprojectId,Stringlocation,StringconnectionId,Connectionconnection)throwsIOException{try(ConnectionServiceClientclient=ConnectionServiceClient.create()){LocationNameparent=LocationName.of(projectId,location);CreateConnectionRequestrequest=CreateConnectionRequest.newBuilder().setParent(parent.toString()).setConnection(connection).setConnectionId(connectionId).build();Connectionresponse=client.createConnection(request);System.out.println("Connection created successfully :"+response.getName());}}}

Python

Before trying this sample, follow thePython setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPython API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

fromgoogle.cloudimportbigquery_connection_v1asbq_connection"""This sample shows how to create a BigQuery connection with a Cloud SQL for MySQL database"""defmain()->None:# TODO(developer): Set all variables for your Cloud SQL for MySQL connection.project_id="your-project-id"# set project_idlocation="US"# set location# See: https://cloud.google.com/bigquery/docs/locations for a list of# available locations.database="my-database"# set database nameusername="my-username"# set database usernamepassword="my-password"# set database passwordcloud_sql_conn_name=""# set the name of your connectiontransport="grpc"# Set the transport to either "grpc" or "rest"connection_id="my-sample-connection"cloud_sql_credential=bq_connection.CloudSqlCredential({"username":username,"password":password,})cloud_sql_properties=bq_connection.CloudSqlProperties({"type_":bq_connection.CloudSqlProperties.DatabaseType.MYSQL,"database":database,"instance_id":cloud_sql_conn_name,"credential":cloud_sql_credential,})create_mysql_connection(connection_id,project_id,location,cloud_sql_properties,transport)defcreate_mysql_connection(connection_id:str,project_id:str,location:str,cloud_sql_properties:bq_connection.CloudSqlProperties,transport:str,)->None:connection=bq_connection.types.Connection({"cloud_sql":cloud_sql_properties})client=bq_connection.ConnectionServiceClient(transport=transport)parent=client.common_location_path(project_id,location)request=bq_connection.CreateConnectionRequest({"parent":parent,"connection":connection,# connection_id is optional, but can be useful to identify# connections by name. If not supplied, one is randomly# generated."connection_id":connection_id,})response=client.create_connection(request)print(f"Created connection successfully:{response.name}")

What's next

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

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.