cloud Sql connect from Local Python IDE - python

How to connect Google Cloud Sql from Local Python code?I am using Pycharm IDE.So I need the detail process to establish connection with google cloud mysql

You can find the detailed process here :
Connecting to Cloud SQL - MySQL
1.If you haven't already, set up a Python Development Environment by following the python setup guide and create a project.
2.Create a 2nd Gen Cloud SQL Instance by following these instructions. Note the connection string, database user, and database password that
you create.
3.Create a database for your application by following these instructions. Note the database name.
4.Create a service account with the 'Cloud SQL Client' permissions by following these instructions. Download a JSON key to use to
authenticate your connection.
5.Running locally : Launch proxy with TCP or Launch proxy with Unix Domain Socket

Related

How to authenticate using Azure Active Directory to connect with azure sql using pymssql?

I am trying to access azure sql instance from airflow and for that I am using pymssql library and active directory authentication. Here is the sample code for the same to establish the connection.
pymssql.connect("MyHostname", "user#xyz.com", "Password", "DbName")
This process is leading me to the below mentioned error.
40532, 'Cannot open server 'xyz.com' requested by the login. The login failed.DB-Lib error message 20018.
Can anyone please suggest me what am I doing wrong here and is there any alternative for the same?

Unable to connect remotely to mysql database hosted online

I am trying to remotely connect to a mysql database from my company network, using the mysql.connector module in python. The db is hosted online (siteground.com).
Error: Error while connecting to MYSQL 2003: Can't connect to MySQL server on.... (10061 no connection could be made because the target machine actively refused it)*
I have the correct hostname, username, password etc.., and have added my company's public IP address as an allowed remote access host.
I have been able to successfully connect using the exact same procedure from a python notebook hosted on google colaboratory (in google drive).
Any ideas on what the issue might be?
Thank you

issues while connecting to RDS-aurora-mysql database

I have a RDS-Aurora(Mysql) database and I want to create tables inside this DB.
MyRequirment:
I want to create tables inside RDS db using python.
I found one python-library to do this task:PyMySQL. PyMySQL helped me to create a table inside the RDS-DB.
if my RDS-DB is accessibly publicly, i can use PYMYSQL to connect to DB and create tables.
ISSUE
My RDS-db is not publicly accessiable
How Can i connect to DB and create a table inside the RDS?
How Can i connect to DB and create a table inside the RDS?
There are three generally used solutions for that:
Run your code from an instance in the same VPC as your private RDS.
Use VPN (or Direct Connect if you have) to setup a connection between your home/work network and the VPC.
Use ssh tunnel for ad-hoc connections between home/work workstation and the private RDS. For that you would need to have some jump host in a public subnet through which you can setup the tunnel.
The ssh tunnel is the easiest to setup and use for development and testing purposes. There are many tutorials explaining how to set it up:
How can I use an SSH tunnel and MySQL Workbench to connect to a private Amazon RDS MySQL DB instance that uses a public EC2 instance?
How can I connect to my Amazon RDS DB instance using a bastion host from my Linux/macOS machine?
Connecting to an AWS RDS database through an SSH tunnel

Cannot connect to PostgresSQL through Google-Cloud-Proxy

I'm running a test Python script that uploads a small amount of data to a PostgreSQL database using SQL Alchemy. Apache Airflow (hosted by Google Cloud Composer) is running this script on a schedule. The script always runs totally fine whenever I run it on Airflow locally and connect directly to the DB.
When I run it on Cloud Composer however, I need to connect through Google's Cloud SQL Proxy for security reasons.
Here's the weird part - the script runs fine about 50% of the time when connecting through Google's cloud-sql-proxy. The other 50% of the time I get the error (psycopg2.OperationalError) FATAL: password authentication failed for user "XXXX. And then usually it retries and it connects just fine.
I'm assuming it's probably something to do with how I am managing my connection (how it's being opened, pooled, or closed) so here is how I'm doing that. I'm using SQLAlchemy in my Python script like so:
from sqlalchemy import create_engine
engine = create_engine(f"postgresql://{USER}:{PASS}#{HOST}/{DB_NAME}")
conn = engine.connect()
# Upload a bit of data here
conn.close()
engine.dispose()
I've always used this way of uploading data locally so I'm a bit confused why that wouldn't work with Google Cloud Proxy / Google Cloud Composer (hosted Airflow).

Connecting through Python to a MySQL database hosted on a remote server with no direct access

I wish to connect to a database server through my local machine at work, but I do not have direct access to the database server (due to security reasons). The database server is accessible through another intermediary server which I can connect to.
I understand I can connect to the database if I run my script on the intermediary server, but is there any way through which I can connect to the database server directly through my local machine?
I am trying to do this in a Python script as I wish to read the data into a pandas dataframe (I can do this part once I can set up the connection).
If you have SSH access to that intermediary server you can connect via an SSH tunnel. This post describes how to do that: Enable Python to Connect to MySQL via SSH Tunnelling

Categories

Resources