Connecting to Google hosted MySQL using SSL from python - python

Performing a basic test. I have a MySQL DB hosted in Google SQL. No issues using MySQL Workbench via SSL from my local computer. So can confirm that access for local IP is correctly setup.
Here is where I am having a problem. Writing a basic connection test program in Python. The program is making an SSL connection to DB and running a query that returns 6 rows. Code listed below.
CNX = mysql.connector.connect(**CONFIG)
cur = CNX.cursor()
cur.execute("select BoroId AS boro_id, BoroName AS boro_name from boro")
for (boro_id, boro_name) in cur:
print "%s %s" % (boro_id, boro_name)
cur.close()
CNX.close()
My issue is that the "for" statement iterates through 0 rows. I do not get any errors. I know the connection part is working and query execution is taking place - on inserting below line after "execute" statement sets value of result_row_count to 6.
result_row_count = cur.rowcount
Official MySQL document states I should be able to iterate through each of the six rows using "for" statement. Any help appreciated.

Related

Does pyodbc execute commands like "select columnA*2 where columnB!=0 from tableA into tableB" locally or remotely?

I am using pyodbc for generating new tables in a remote SQL server. I wrote the following code, replacing column names, table names, driver name, and server name with dummy names.
import pyodbc
query="select columnA*2 where columnB!=0 from tableA into tableB"
connection = pyodbc.connect('Driver={driverA};''Server=serverA;'
'Trusted_Connection=yes;')
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
cursor.close()
connection.close()
I wonder when I run this this code in my Jupyter notebook (I am not sure if running it in a Jupyter Notebook is relevant, but I figure it doesn't hurt to mention it), does my laptop execute some parts of the code or does my laptop send all these code to serverA and ask serverA to execute the command?
What about the following code? Does my laptop simply send the code to the server and wait for the response? Alternatively, does my laptop asks for the entire tableA and then do some of the computing locally?
cnxn = pyodbc.connect("Driver= {driverA};" "Server=serverA;" "Database=databaseA;"
"Trusted_Connection=yes;")
diagramFinal = pd.read_sql("""select columnA*2 where columnB!=0 from tableA""", cnxn)
cnxn.close()
I read through this documentation without finding relevant information. So, I wonder if someone could help me with figuring it out.

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

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.

How to use MySQL with Python in a client server setup

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()

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).

Categories

Resources