SQLAlchemy connect through specific network interface - python

I have a client computer (now Windows 10, soon to be Ubuntu Server) connected through a LAN Ethernet interface to a remote DB server using SQLAlchemy. The same client holds a Wifi network interface connected to Internet in order to provide external access to the client. Within the client there is a Python script that runs reading the remote DB Server and updating a local DB with records of interest.
Everything works fine while the Wifi interface is disconnected, and SQLAlchemy's engine.connect() throws a connection error when the Wifi becomes active.
The question is how to force the connection to be done through the Ethernet interface for the following commands:
engine = create_engine(url)
engine.connect()
I am expecting some sort of default routing configuration for the SQLAlchemy engine, or a workaround that does not involve SQLAlchemy.

Related

Pandas read from remote Postgresql with SSH tunnel and sqlalchemy

I can read from my local psql instance like this:
engine = create_engine('postgresql://postgres:postgres#localhost/db_name')
df = pd.read_sql("select * from table_name;", engine)
I have a remote postgresql sever which I successfully accessed with ssh tunneling both in PgAdmin4 and pycharm. I use public key file to login into remote server. Now, my question is how do I access that database with pandas. I tried:
engine = create_engine('postgresql://username:password#localhost/db_name')
Here, username and password are of remote database. I get sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: password authentication failed for user. However, with the same username and password I can access the table in PgAdmin.
From what I read, because of ssh tunneling I have to use localhost and not the remote server address, right? In pgAdmin I can see that the server is running. So, my question is how do I read the table from remote postgresql database with ssh tunneling? In examples I have seen people using different port (different than 5432) but for me the setup only works if I use port 5432. I have disconnected all other servers to avoid the port conflict but I get the same error.
The tunnel created by pgAdmin4 is intended for its own use. It does not arrange for it to listen on 5432, it picks some arbitrary high numbered port and doesn't advertise what port that is. While you can discover what port it is listening on using system tools (like netstat) and then connect to it, you would probably be better served by finding some other way to set up your tunnel. There are python libraries that can help with that.
As for why you can connect to 5432 at all, clearly there is something listening there which is either PostgreSQL or pretending to be PostgreSQL, but it doesn't seem to be the one you intend. You can use netstat -ao to find the pid for it and then look up based on that.

Cx_Oracle DB connect issue from container running on EKS

I am having an issue connecting to on-prem Oracle databases from AWS EKS. The error I get is:
"ORA-12545: Connect failed because target host or object does not exist".
The the exact same script, from the exact same docker image, works fine when run from any other platform (not EKS).
Verified the firewall rules are in place by obtaining a shell on the pod and using telnet to the target. We do get a basic TCP connection, however any attempts to connect with the Cx oracle client fail with the above error.
Below is my cx oracle code, I don't feel there is a issue with how the connection string is passed, because the code works fine on different platform. Just that EKS container seems to have problems making a connection.
dsn_tns = cx_Oracle.makedsn(host,port,service_name=service_name)
connection = cx_Oracle.connect(user,password,dsn_tns,encoding = 'utf-8')

How to get access to django database from other python program?

I have django project in which I can display records from raspberry pi device. I had mysql database and i have send records from raspberry there. I can display it via my api, but I want to work on this records.I want to change this to django database but I don't know how I can get access to django database which is on VPS server from raspberry pi device.
ALERT: THIS CAN LEAD TO SECURITY ISSUES
A Django database is no different from any other database. In this case a MySQL.
The VPS server where the MySQL is must have a public IP, the MySQL must be listening on that IP (if the VPS has a public IP but MySQL is not listening/bind on that IP, it won't work) and the port of the MySQL open (default is 3306), then you can connect to that database from any program with the required configurations params (host, port, user, password,...).
I'm not a sysadmin expert, but having a MySQL on a public IP is a security hole. So the best approach IMO is to expose the operations you want to do via API with Django.

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

Python connect to ssh mysql server

I'm writing a small python program locally as i don't have root access on the server. It basically does a lot of mysql queries using python MySQLdb module.
The thing is I cant use MySQLdb with the server, as the mysql server is hosted locally and I need to ssh into the server and then use mysql from there.
Is there any module available where I can connect to a mysql database via SSH.
At the moment I can connect to the mysql instance using SSH credentials (IP, User, Pass)
I also have the user/pass for the mysql instance and I'm pretty sure it runs on 127.0.0.1/localhost.
If you're set on Python, I would use paramiko:
https://github.com/paramiko/paramiko
This seems to be one of the most widely used Python SSH libraries. There are some other StackOverflow questions that address how to do this.
How to open an SSH tunnel using python?
SSH Tunnel for Python MySQLdb connection
The main idea is to create a tunnel with paramiko and then connect to the localhost port through which you are tunneling traffic to the remote server using the Python library MySQLdb.

Categories

Resources