Tornado MySQLDB SSL Connection - python

Iā€™m using tornado and connecting to MySQL using MySQLDB. How I can connect to MySQL using SSL?

If the MySQL server is on the same host as the web server then you don't really need to do this since traffic will not leave that box. You can use an ssh tunnel to encrypt traffic if you'd like. Something like the following should do the trick:
$ ssh <mysql_port>:localhost:<mysql_port> user#mysqlserver.host
That will create a tunnel from your MySQL server to the web server. You can then connect to localhost:port instead of mysqlserver.host:port.

Related

PostgreSQL Connection issues- Python

i want to connect a python code with a database localized in a Google Cloud Platform PostgreSQL database. I've been running this code and this working correctly, but recently i chaged me server to a VPS provided by a host. Now when i try to connect to the database with python3 i receive the follow message:
Psycopg2.OperationalError: could not connect to server: Connection timed out
Is the server running on host "35.199.90.49" and accepting
TCP/IP connections on port 5432?
I have authorized the IP of the server in the Google Cloud Platform, i tried to open all IPv4 address too, but nothing is working. I changed the /XX from IPv4 for my server. I disabled the firewall of the server. Nothing is working but when i connect with the psql command at ubuntu server, the database connection is okay.

How to send HTTP GET request from remote host using SSH connection in Python?

I'm using an SSH connection with Paramiko.
My code:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=remote_host, username=remote_user, password=remote_password,
port=remote_port)
How to send HTTP GET request from connected remote host (use it like a proxy)?
I've found solution according to the answer:
with SSHTunnelForwarder(
ssh_address_or_host=(remote_host, remote_port),
ssh_username=remote_user,
ssh_password=remote_password,
remote_bind_address=("www.python.org", 80),
) as tunnel:
conn = http.client.HTTPConnection("127.0.0.1", port=tunnel.local_bind_port)
conn.request("GET", '/')
There are two options:
External tool
Use any tool available on the SSH server that can send the HTTP request. E.g. curl or wget:
curl https://www.example.com/
And execute it using Paramiko: Python Paramiko - Run command
This solution is easier, but has the dependency on the command ā€“ So it's also platform dependent.
Port forwarding
Forward a local port to the remote HTTP server port 80 and connect to the forwarded port using your local Python code.
You will find lot of examples how to forward a database port. Like this one: Enable Python to Connect to MySQL via SSH Tunnelling
In your case, you need to do the same, just instead of using a database client to connect to the forwarded port, you connect an HTTP client (like HTTPConnection).
Also in most cases, the database forwarding usually ends on the SSH server itself (localhost/127.0.0.1), while you want to connect further.
This solution is more complicated, but without external dependencies ā€“ So it's platform independent. On the other hand, the port forwarding is a special privilege, that may be restricted on the server (but usually it is not). Before trying to implement it, you can test it with your SSH client.

Cannot connect mysql through python

I did a python -m pip install mysql-connector and able to successfully run import mysql.connector through python. But when I am trying to run the below code.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
passwd="yourpassword"
)
print(mydb)
It is failing with InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
Since I did pip install for mysql.connector I am not sure of user and passwd.
I connected my database instance from Amazon RDS to mySql workbench, created a python file that looks like his and got the exact error. Everyone says you could have a system firewall problem but nothing straight forward.
The connector is just a means of communicating with a mysql database programically.
You need this, or access to a mysql server to use the connector.
https://dev.mysql.com/downloads/mysql/
The host is obviously localhost when you want to access it locally on your machine. This only works if the server is running on your machine. You can connect a remote server by changing the host to a valid IP address and providing valid credentials. If you use it remotely make sure the server has access through the firewall and that you properly forward the TCP port you decide to use. You may not have to forward, but I would as a general rule of thumb to make it one less thing to check when troubleshooting.
Good luck

Python connection to server and interact with database

I have question how can I connect to VPS server and interact with MySQL database using python on my local machine.
I would like to make program using PyQT that connect to my server and update, take sth from db.
I know that there is MySQLdb module but from what I know i cannot connect to VPS server, because in connect method it doesn't take server password argument.

sqlalchemy connect to VPS database

I am trying to connect to VPS mysql database from my PC. I use sqlalchemy framework, but I need establish SSH tunnel before connection.
Usual way, when web app run on VPS:
create_engine('mysql://user:pswd#localhost/dbname')
How can I connect to this database from another PC. Assume there are connections credentials: IP, username, password
Your MySQL server is listening to local connections only. To make it listen to outside connections:
Edit the /etc/mysl/my.cnf file
Comment out the line bind-address = 127.0.0.1
Restart mysqld

Categories