Issue Connecting to Mssql Instance From Google Cloud Function

Will Volino

I am trying to connect to a mssql instance in cloud sql in a cloud function. I have gone through the necessary steps of setting up a private IP, serverless VPC connector, and connecting my function to the VPC. I have been able to connect to the instance in nodejs but python suits my current needs more. The error I'm getting in the logs is:

pyodbc.Error: ('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server'

From all the examples I have read it does not appear that you need to import them or anything. This is my process of connecting and executing a simple request.

import sqlalchemy
import pyodbc

def hello_world(request):
        # connect_simple()
        db = connect_tcp_socket()
        a = execute_request(db)
        return a
    
def connect_tcp_socket() -> sqlalchemy.engine.base.Engine:
    db_host = 'my_private_ip'  
    db_user = 'my_db_user'
    db_pass = 'my_db_pass'
    db_name = 'my_db_name'
    db_port = 'my_db_port' 

    connection_string = 'DRIVER={ODBC Driver 17 for SQL Server};SERVER='+db_host+';PORT='+db_port+'DATABASE='+db_name+';UID='+db_user+';PWD='+ db_pass+';Encrypt=no'
    connection_url = sqlalchemy.engine.url.URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
    engine = sqlalchemy.create_engine(
        connection_url
    )

def execute_request(db: sqlalchemy.engine.base.Engine):
        print('ok')
        with db.connect() as conn:
            result = conn.execute('SELECT @@VERSION')
            barray= []
            for row in result:
                barray.append(row)
        return barray
Jack Wotherspoon

I'd recommend using the Cloud SQL Python Connector to connect to Cloud SQL from Python as it will not require the ODBC driver and is much easier to use within Cloud Functions/Cloud Run.

Just replace your connect_tcp_socket with the below connect_with_connector function.

from google.cloud.sql.connector import Connector, IPTypes
import pytds

import sqlalchemy

def connect_with_connector() -> sqlalchemy.engine.base.Engine:
    def getconn() -> pytds.Connection:
        with Connector() as connector:
            conn = connector.connect(
                "project-id:region:instance-name",  # Cloud SQL connection name
                "pytds",
                user="my-user",
                password="my-password",
                db="my-database",
                ip_type=IPTypes.PRIVATE
            )
            return conn

    engine = sqlalchemy.create_engine(
        "mssql+pytds://localhost",
        creator=getconn,
    )
    return engine

You can find a code sample for the Python Connector similar to the one you are using for establishing a TCP connection.

Note: Pytds driver is not super great with error handling. If you see the OSError: [Errno 9] Bad file descriptor error it usually means your database user is missing proper permissions and should grant them the necessary grants from a root user.

Your requirements.txt should include the following:

cloud-sql-python-connector
SQLAlchemy
python-tds
sqlalchemy-pytds

There is also an interactive getting started Colab Notebook that will walk you through using the Python Connector without you needing to change a single line of code!

It makes connecting to Cloud SQL both easy and secure from Cloud Functions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Connecting to Cloud SQL from Google Cloud Function using Python and SQLAlchemy

From Dev

Connecting to a Google Cloud SQL instance from a Google Cloud Run docker image

From Dev

Connecting to Google Cloud SQL from a Google Cloud Function python3.7

From Dev

Connecting to Google Cloud SQL instance on private IP from a VM with both private and public IPs fails

From Dev

Error connecting to Stripe from Firebase Cloud Function

From Dev

Connecting to Google Cloud SQL Postgres instance using psycopg2

From Dev

Issue connecting to Database during Google Cloud Build process

From Dev

How can I use a Google Cloud Function to push a file from a Cloud Storage bucket into an instance?

From Dev

What is a boilerplate code for connecting to Cloud Firestore from within Cloud Function?

From Dev

nanodbc connecting to MSSQL from Linux

From Dev

Connecting to Debian MSSQL from Windows

From Dev

Google Cloud SQL not connecting

From Dev

google cloud function price calculation issue

From Dev

Cloud Function not connecting with Cloud SQL

From Dev

Connecting a Google Cloud App Engine app to its Cloud SQL Instance-allowing database access

From Dev

Google Cloud - Downloading data from bucket to instance

From Dev

download file from google cloud compute instance

From Dev

Connecting to Google Cloud SQL from App Engine: access denied

From

Trouble connecting to Google Cloud SQL server from deployed app

From Dev

ENOENT when connecting to Google Cloud SQL from App Engine

From Dev

How to have a google cloud function communicate with google compute engine instance?

From Dev

MSSQL - Nodejs - Undefined is not a function issue

From Dev

Google Cloud Function Deploying Function OCR-Extract Issue

From Dev

Connecting an AWS EC2 to a Google Cloud SQL instance locally using VPN Gateway

From Dev

Connecting to An External (outside of Google Cloud Platform) PostgreSQL db from Google Cloud Run

From Dev

Deploying Google Cloud function from a Google Build

From Dev

From local Python function to Google Cloud Function

From Dev

Connecting Sequelize to Google Cloud SQL

From Dev

Error connecting to Google Cloud Datastore

Related Related

  1. 1

    Connecting to Cloud SQL from Google Cloud Function using Python and SQLAlchemy

  2. 2

    Connecting to a Google Cloud SQL instance from a Google Cloud Run docker image

  3. 3

    Connecting to Google Cloud SQL from a Google Cloud Function python3.7

  4. 4

    Connecting to Google Cloud SQL instance on private IP from a VM with both private and public IPs fails

  5. 5

    Error connecting to Stripe from Firebase Cloud Function

  6. 6

    Connecting to Google Cloud SQL Postgres instance using psycopg2

  7. 7

    Issue connecting to Database during Google Cloud Build process

  8. 8

    How can I use a Google Cloud Function to push a file from a Cloud Storage bucket into an instance?

  9. 9

    What is a boilerplate code for connecting to Cloud Firestore from within Cloud Function?

  10. 10

    nanodbc connecting to MSSQL from Linux

  11. 11

    Connecting to Debian MSSQL from Windows

  12. 12

    Google Cloud SQL not connecting

  13. 13

    google cloud function price calculation issue

  14. 14

    Cloud Function not connecting with Cloud SQL

  15. 15

    Connecting a Google Cloud App Engine app to its Cloud SQL Instance-allowing database access

  16. 16

    Google Cloud - Downloading data from bucket to instance

  17. 17

    download file from google cloud compute instance

  18. 18

    Connecting to Google Cloud SQL from App Engine: access denied

  19. 19

    Trouble connecting to Google Cloud SQL server from deployed app

  20. 20

    ENOENT when connecting to Google Cloud SQL from App Engine

  21. 21

    How to have a google cloud function communicate with google compute engine instance?

  22. 22

    MSSQL - Nodejs - Undefined is not a function issue

  23. 23

    Google Cloud Function Deploying Function OCR-Extract Issue

  24. 24

    Connecting an AWS EC2 to a Google Cloud SQL instance locally using VPN Gateway

  25. 25

    Connecting to An External (outside of Google Cloud Platform) PostgreSQL db from Google Cloud Run

  26. 26

    Deploying Google Cloud function from a Google Build

  27. 27

    From local Python function to Google Cloud Function

  28. 28

    Connecting Sequelize to Google Cloud SQL

  29. 29

    Error connecting to Google Cloud Datastore

HotTag

Archive