Git Product home page Git Product logo

cloud-sql-python-connector's Introduction

Cloud SQL Connector for Python Drivers

Warning: This project is experimental, and is not an officially supported Google product.

The Cloud SQL Python Connector library is a library for MySQL/Postgres Python drivers that allows users with sufficient permissions to connect to a Cloud SQL database without having to manually whitelist IPs or manage SSL certificates.

Currently only supports MySQL through the pymysql driver.

Supported Python Versions

Currently Python versions >= 3.6 are supported.

Authentication

This library uses the Application Default Credentials to authenticate the connection to the Cloud SQL server. For more details, see the previously mentioned link.

To activate credentials locally, use the following gcloud command:

gcloud auth application-default login

How to install this connector

Clone this repo, cd into the cloud-sql-python-connector directory then run the following command to install the package:

pip install .

Conversely, install straight from Github using pip:

pip install git+https://github.com/GoogleCloudPlatform/cloud-sql-python-connector

How to use this connector

To use the connector: import the connector by including the following statement at the top of your Python file:

from google.cloud.sql.connector import connector

Use the connector to create a connection object by calling the connect method. Input your connection string as the first positional argument and “mysql-connector” for the second positional argument. Insert the rest of your connection keyword arguments like user, password and database.

connector.connect(
    "your:connection:string:", 
    "pymysql",
    user="root",
    password="shhh",
    db="your-db-name"
... insert other kwargs ...
)

Setup for development

Tests can be run with nox. Change directory into the cloud-sql-python-connector and just run nox to run the tests.

  1. Create a MySQL instance on Google Cloud SQL. Make sure to note your root password when creating the MySQL instance.
  2. When the MySQL instance has finished creating, go to the overview page and set the instance’s connection string to the environment variable INSTANCE_CONNECTION_NAME using the following command:
export INSTANCE_CONNECTION_NAME=your:connection:string
  1. Enable SSL for your Cloud SQL instance by following these instructions.
  2. Create a service account with Cloud SQL Admin and Cloud SQL Client roles, then download the key and save it in a safe location. Set the path to the json file to the environment variable GOOGLE_APPLICATION_CREDENTIALS using the following command:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/auth/./json
  1. Enable the SQL Admin API.
  2. Clone the repository.
  3. Create a virtual environment and change directory into the cloud-sql-python-connector folder.
  4. Install the package by running the following command:
pip install .

cloud-sql-python-connector's People

Contributors

crwilcox avatar kurtisvg avatar shivaal-scio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.