How to use MySQL with Python in a client server setup - python

I'd like to develop a python project in client - server style.
My Python code will be deployed on a client machine and the MySQL database on the server, so all connections need to flow through a network connection.
I know how to use Python with MySQL with basics such as creating tables and queries etc, however I wanted to learn how to connect my python project to the database over a network protocol.
I searched on Google or YouTube, but only found resources that use a local database connection.
Where can I find Python resources (article, blog, sample, tutorial or video) that explains how to connect to MySQL via the network?

You have server with MySQL database running on it and a python client-application, which needs data from the database?
I think usually client software never connects directly to the database (for security reasons). Instead the client communicates with a server-application (maybe over a REST-API).
How i meant it

This is seems to be a good guide.
https://pynative.com/python-mysql-tutorial/
I tried PyMySql quickly. It works. Only problem is it is in Beta. https://pymysql.readthedocs.io/en/latest/user/examples.html
So I suggest you try the MySQL Connector Python will be slightly painful to setup initially but it comes from the vendor. so you can trust it a bit better.
or alternatively https://pypi.org/project/mysqlclient/
import pymysql.cursors
import pymysql
try:
conn = pymysql.connect(host='10.1.1.1 (remote server ip or hostname)',
user='db_user_name',
password='db_password',
db='name_of_db',
cursorclass=pymysql.cursors.DictCursor)
sql = "select * from users"
with conn.cursor() as cursor:
# Read a single record
sql = "select * from users"
cursor.execute(sql)
result = cursor.fetchall()
print(result)
except Exception as inst:
print(str(inst))
finally:
conn.close()

Related

python to oracle db connection making delay

I'm using following code to connect to a remote oracle DB server, But it takes more than 10 second only to create the DB connectivity, then the DB SQL query return result instantly. Same connection made by the nodejs and it works perfectly. checked the connectivity between the source and destination, but cannot see any issue. Can you help me to diagnose this issue. I'm using;
python 3.6 and use oracle instant client libraries( basic) to connect
import cx_Oracle
dsn_tns=cx_Oracle.makedsn('dbhost','port',service_name='SERVICENAME')
conn = cx_oracle.connect(user=r'USER_NAME',password='******',dsn=dsn_tns)
c = conn.cursor()
c.execute("""SIMPLE SELECT QUERY""")
for row in c;
print (row[0],'-',row[1])
conn.close
Sometimes it may happen because of slow random number generation.
Take a look here https://learn.capstorm.com/copystorm/frequently-asked-questions/problems-solutions/how-do-i-fix-random-oracle-login-connection-timeouts/
Or here https://support.oracle.com/epmos/faces/DocContentDisplay?id=1594701.1

Connecting to Cloud SQL postgres instance with SSL in python

I am trying to connect to a postgres instance I have in cloud sql. I have everything set up and am able to connect to it if ssl encryption is turned off. But now that I have it on I am trying to connect but running into some error.
def run():
connector = Connector()
def getconn():
conn = connector.connect(
os.getenv("CONNECTION_NAME"),
"pg8000",
user = os.getenv('DB_USERNAME'),
password = os.getenv("DB_PASSWORD"),
db=os.getenv('DB_NAME'),
ip_type= IPTypes.PRIVATE
)
return conn
pool = sqlalchemy.create_engine(
"postgresql+pg8000://",
creator=getconn,
pool.execute("CREATE TABLE........;")
All the certs are stored in secret manager as strings so I am using env variables to grab them, which is why I used cadata for example. But running into this error cadata does not contain a certificate why is this error coming up?
I'd recommend using the Cloud SQL Python Connector to connect to Cloud SQL from Python as it will generate the SSL context for you, meaning no need to manage SSL certificates! It also has additional benefits of not needing to authorize networks etc.
You can find a code sample for the Python Connector similar to the one you are using for establishing a TCP connection.
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.

How to set mysql rows to variable in Python

I am new to programming and working on a self-motivated password analyzer project. Part of my program searches User's password through the common password list. Originally I had this folder as .txt file however I wanted to put the list in mySQL server. I was able to connect to my server and create a table with 3 rows that had passwords. However I can't figure out how to tell python to search through the sql server and the rows.
Thank you for the help!
So, since you're trying to fetch some data from MySQL, you need to initiate the connection with the MySQL server in the first place. Then you can perform your queries on the DB through Python.
To initiate connection
pip install mysql-connector-python. See Install MySQL Connector
Then you can import the connector in your script import mysql.connector.
Example
Updated as per the comments discussion.
from mysql.connector import (connection)
cnx = connection.MySQLConnection(user='scott', password='password',
host='127.0.0.1', # Or 'localhost'
database='employees')
sql_select_query = "select * from passw"
cursor = cnx.cursor()
cursor.execute(sql_select_query)
records = cursor.fetchall()
for record in records:
password = record[0]
cnx.close()
Note: Consider using environment variables to securely use sensitive data (MySQL connection data).
More details with examples can be found in Connecting to MySQL Using Connector/Python.

Extract data from client database

I have to send multiple data through Python from a DB allocated in the client to the Main DB in the server, whats the best solution to this? i currently have my web server up and functioning, i can fill my DB locally but i dont really know how to do it remotely, im using Python in my hardware, here is what i have so far in the client:
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pass', host='url?', database='db') #im able to enter with this
cursor = cnx.cursor()
query = ("INSERT INTO IGNORE table " "(id,date,son) " "VALUES( (%s,%s,%s)")
for row in data: #ive already extracted data from the other DB
cursor.execute(query, (row[0],row[1],row[2]))
wich yealds an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the righ syntax to use near 'IGNORE table(id,date,son) VALUES (number, '2017-11-09 14:33:15', 18.987)' at line 1
One possibility - Make sure your mysqld service is binding to your external ip address (set in your mysql config file on the server). By default I believe it binds to 127.0.0.1 or localhost. If it is binding to localhost, your db will never respond to external requests.
I fixed the issue with the MySQL port in the following way: I opened the port internally from my VM instead of from my Virtual Network (I'm using the Google Cloud Platform).

Can`t connect MySQL database in phpMyAdmin by python

I need to use python to connect database in phpMyAdmin.
import MySQLdb
db = MySQLdb.connect(host="10.0.0.140",port=80,user="root",passwd="password")
cursor=db.cursor()
cursor.execute("SHOW DATABASES")
results=cursor.fetchall()
for result in results:
print row
and I get this error
2013,"Lost connection to MySQL server at'waiting for initial communication packet',system error:0"
I can access the database in chrome, so I don`t think it is a problem of remote access.
=======================update=======================
The real reason is that I am in a limited net segment.(maybe)
There is something wrong with SQL connection out of my code.
If changed code like this:(port is not needed)
MySQLdb.connect(host="10.0.0.140",user="root",passwd="password")
People in public net segment can connect the phpMyAdmin but I can`t.
I can not change my net segment so I can`t confirm it is the real reason.
But it is the only difference between me and others.
Check the connection, are you sure you run your mysql at port 80?
As I wrote in the comments, I doubt it runs port 80, this is the url to your phpMyAdmin and not the core mysql database server. Normally the web and database run on different machines, different IP addresses etc. Is this a hosted environment? Or do you run this in your local machine.
If it is your local machine then changing to port 3306 should work.
If it is a hosted environment by a hosting partner then you need to check the JDBC url from them. As you wrote it is XXXX hosted partner. Then check your XXX and it will provide you a XXX jdbc url for your mysql, and be sure to grant access to the user so you can logon to the mysql from your workstation remotly. The mysql url/host could be something like this NNNN-aaaa-bbbb-cccc-dddd.xxx.domain.xx
Also did you try the PyMYSQL
import pymysql
db = pymysql.connect(host='10.0.0.140',user='root',passwd='password')
cursor = db.cursor()
query = ("SHOW DATABASES")
cursor.execute(query)
for r in cursor:
print r
This is not the way you should do it. phpMyAdmin serves HTML to the user and should be used in a Web browser. You should connect directly to the MySQL host which is usually listening on port :3306. Also, you should keep in mind that, in production, MySQL servers are ordinarily not listening on public interfaces.

Categories

Resources