Extract data from client database - python

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

Related

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

Python using mysql.connector.connect yields an Interface Error

When I execute the code
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="user",
passwd="password"
)
I get:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)
I've tried using an example with "pymysql' and the error stays.
I looked around the internet and a lot of people say it could be a firewall inbound problem. Yet there is no fire wall stopping 3306. The security group on Amazon RDS allows all connections. I connected the RDS instance to my local mySql Workbench (so I can make tables and stuff from there). Interestingly enough when I run the code and the error persists there is additional client connections that pop up. Anyone else deal with this? Thank you very much I'm trying to learn this part of AWS well.
I figured it out. My host name should NOT be "localhost" it should be my endpoint on Amazon RDS. It seems clear now considering when I connected my RDS database to mySql workbench I put in "databasename.xxxxxxx.us-east-1.rds.amazonaws.com" for my host name. Now I can connect and read/write to the database.

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.

OpenShift mysql connection issues in python

I previously wrote my app using local development servers, and now that I have moved it onto an openshift small gear almost all works except for mysql connections.
In my code I have the line:
self.db = MySQLdb.connect(host, username, password, dbname)
When I review the openshift error log, the following error is reported:
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
I think that python is trying to connect using a UNIX socket as opposed to an INET one, but I'm not sure how to change this behavior. Any help is much appreciated.
Not specific to MySQLdb: if you use localhost as hostname, a MySQL client using the MySQL C libraries will try to connect using UNIX socket (or named pipe on Windows). There are 2 ways around this, but you'll need to grant extra permissions to make it work for both:
Use IP address 127.0.0.1
Use IP address 127.0.0.1 instead of the localhost hostname. This will make MySQL client connect using TCP/IP.
Use option files
The other way is to force the protocol using using option files. For example, in your ~/.my.cnf (or any file you want), add the following:
[python]
protocol=tcp
Now use the connection arguments to read the option file and group:
import MySQLdb
cnx = MySQLdb.connect(host='localhost', user='scott', passwd='tiger',
read_default_file='~/.my.cnf',
read_default_group='python')
The group name does not need to be python, but it is good not to use mysql or client as it might interfere with other MySQL tools (unless you want that of course).
For setting up permissions, you'll need to use the IP address of localhost, something like:
mysql> GRANT SELECT TO yourdb.* TO 'scott'#'127.0.0.1' IDENTIFIED BY ...;
(Site note: MySQL database drivers such as MySQL Connector/Python do not consider localhost to be special and connect through TCP/IP right away and you have to explicitly use the unix_socket.)
As I later discovered, while the database server runs on localhost, it runs on a very specific localhost bind address. In my case it was an address that I would never have though to try if I hadn't noticed how phpmyadmin was connecting.

Categories

Resources