I am new to programming and Google service. I have read this document and set up Cloud SQL access by IP address.
However for access from Python, the documentation is only for App Engine and using Proxy instead. How can I access from Python in Google Compute Engine? Thanks
Credit #Mangu
Depending upon the application and the requirements, you can configure to connect to the Cloud SQL from python in compute engine using external IP of the Cloud SQL instances or use Cloud SQL Proxy with python (The Cloud SQL Proxy is available only for Second Generation instances.)
Here is another complete example tutorial of using Cloud SQL with Python Bookshelf application.
Related
I am using terraform to set up a simple application that has a postgres db via Cloud SQL in google cloud platform (GCP). I set up a GCP Cloud SQL Auth proxy for my postgresql db using this guide. I set up the proxy as a sidecar to my main kubernetes application. I also set up a GCP service account to be used for authentication in the cloud proxy. In other words, I set the service_account_name in the kubernetes_deployment resource in my terraform file to be a gcp service account with the necessary roles to connect to the database.
Now, I'd like to use python and sql alchemy to connect to this postgresql db through the Cloud SQL proxy. Everything I found online (like this documentation) suggest that I need to add a username and password like this to connect to the cloud proxy: mysql+pymysql://<db_user>:<db_pass>#<db_host>:<db_port>/<db_name>. However, my google service account doesn't have a username and password.
My question: is there a way to connect to the google cloud auth proxy without a password using my gcp service account?
The Cloud SQL Python Connector is a Python package that makes connecting to Cloud SQL both easy and secure for all three supported database engines (Postgres, MySQL, and SQL Server), from anywhere (local machine, Cloud Run, App Engine, Cloud Functions, etc.). (source: gcp blogs)
This connector uses IAM permissions and TLS certificates for getting connected to the cloud sql instances. This source code is available in github and there are versions available for java and go languages as well.
I have a Service principal with a client id and client secret. It has permission to the Azure SQL DB. I want to use python to generate an access token and use it to authenticate to my sql server. Could someone guide me.
I am new with python and would appreciate if someone could specify if I need any supporting libraries for this to work.
I want to use python to generate an access token.
The Azure Active Directory Authentication library for python can be used to access SQL Server in Azure.
Refer this GitHub repository to know more details and how to implement the same.
Currently I am accessing my Google Cloud SQL locally through a proxy, as in the following.
conn = pymysql.connect(unix_socket='cloudsql/'+project_name, user=user, password=password, db=db)
Now I have set up my cluster and whitelisted its External IP. I need to change the line above to connect to Cloud SQL from Kubernetes. (I don't think I can keep using the proxy, as it cannot be dockerized) I am unsure how to change my pymysql.connect() call.
Any advice will be greatly appreciated.
There's an official Cloud SQL proxy docker image that you can use as a sidecar container next to your app container, in the same pod. This is actually the recommended way to connect to Cloud SQL from GKE.
You'll find a detailed deployment configuration here (this example is for wordpress but the idea is the same). Your app will use 127.0.0.1:3306 as the host address to access your database
I want to create SQL Server and SQL Database using python sdk on Azure.
Does Azure Provides Support for that?
Azure support two ways for creating Azure SQL Database.
Using Azure Service Management(ASM) to create a classic SQL Database (REST API).
Using Azure Resource Manage(ARM) to create a SQL Database (REST API).
For the process of creating Azure SQL Database, you can refer to the document for C# SDK https://azure.microsoft.com/en-us/documentation/articles/sql-database-client-library/.
To create SQL Database using Python SDK, according to the api document, it seems to only support ASM mode, please see the sql database managementservice module at http://azure-sdk-for-python.readthedocs.org/en/latest/ref/azure.servicemanagement.sqldatabasemanagementservice.html?highlight=sql%20database.
If you want to create sql database via ARM mode, I think you can try to use the REST API Create or Update Database that need to be authenticated by using Python SDK for Resource Management Authentication.
You can check the python sdk for azure, I find that the azure.servicemanagement.sqldatabasemanagementservice module which provides the function to create a new SQL Server and SQL Database, please check the detail in this article. So the answer to your question would be yes. Hope this help you.
I use
python 2.7
pyodbc module
google app engine 1.7.1
I can use pydobc with python but the Google App Engine can't load the module. I get a no module named pydobc error.
How can I fix this error or how can use MS-SQL database with my local Google App Engine.
The Google App Engine does not support access to your own SQL server, and does not support loading C-API libraries of your own.
You can use the Google Cloud SQL storage, which is implemented with MySQL databases, which you can access via their Cloud SQL API for Python.
Note that the rdbms module Google provides implements the PEP 249 Python database API 2.0 specification, just like the pyodbc module, so you should have no problems using it:
from google.appengine.api import rdbms
conn = rdbms.connect(instance=INSTANCE_NAME, database=DATABASE)
cursor = conn.cursor()
cursor.execute('SELECT * FROM sometable')
You could, at least in theory, replicate your data from the MS-SQL to the Google Cloud SQL database. It is possible create triggers in the MS-SQL database so that every transaction is reflected on your App Engine application via a REST API you will have to build.