PyMySQL Connecting to a Remote Database - python

I'm learning Python and attempting a tutorial to connect to a remote database.
The problem that I'm having is that I'm not sure what to replace localhost with, I've tried domains, IP address etc. but keep getting the following error.
OperationalError: (2003, "Can't connect to MySQL server on 'remotehost
name' (timed out)")
# Open database connection
db = pymysql.connect("localhost","username","password","dbname" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Drop table if it already exist using execute() method.
cursor.execute("DROP TABLE IF EXISTS rsstracker")
# Create table as per requirement
sql = """CREATE TABLE rsstracker (
article_title varchar(255),
article_url varchar(1000),
article_summary varchar(1000)
summary )"""
cursor.execute(sql)
# disconnect from server
db.close()

You should replace localhost with the server's IP address or host name. If that server is in your LAN you will need its internal address. If it is outside your network you will need its external address.
Either way you will need to make sure the port you are using is well forwarded and not blocked/filtered by routers / firewalls along the way, including on / by the remote server's operating system.
This question may be more appropriate on superuser exchange.

Related

Python: Using 'Null' for mysql.connector's port argument

I have a mysql database and I fetch it via a domain like www.mydomain-database.com. this domain is given by a company for accessing my database by phpmyadmin. When I browse this domain, it fetches phpmyadmin login page.
I try to connect to this database by the following code:
db = mysql.connector.connect(
host = "www.mydomain-database.com",
user = "root",
passwd = "**",
database = "database",
charset = 'utf8', use_unicode=True
)
When I run this, I get the following exept:
Can't connect to MySQL server on 'https://www.mydomain-database.com:3306' (-2 Name or service not known)
As you can see, connector adds port 3306 to my host; but the url with this port is not valid & it doesn't fetch the phpmyadmin!
So, for canceling that change, I added the port = "" as an argument for my connection but I got another error that mentioned the port must be integer!
Now the question is, how can I remove that port number when connector tries to connect the host?
You have to supply a port. By default MySQL uses port 3306. If your MySQL instance is using a different port, then you can specify that port in the settings.
Do you have access to the MySQL instance?
If so you can try and run:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
to get your port number.
However, your error message refers to server https://
that is not normal, there should not be any reference to https://
Can you check your code in your app and make sure that
host = "www.mydomain-database.com"
and not
host = "https://www.mydomain-database.com"

How do I overcome this sql connectivity error with python?

I am not able to connect to MySQL sever using python it gives and error which says
MySQLdb._exceptions.OperationalError: (1130, "Host 'LAPTOP-0HDEGFV9' is not allowed to connect to this MySQL server")
The code I'm using:
import MySQLdb
db = MySQLdb.connect(host="LAPTOP-0HDEGFV9", # your host, usually localhost
user="root", # your username
passwd="abcd13de",
db="testing") # name of the data base
cur = db.cursor()
cur.execute("SELECT * Employee")
for row in cur.fetchall():
print(row[0])
db.close()
This is an authorization problem not a connectivity problem. Is the db running locally? If not, confirm with the admin where it is hosted. If so, try changing the host parameter to 127.0.0.1?
As described here the admin can get the hostname by running:
select ##hostname;
show variables where Variable_name like '%host%';
If the connection was timing out you could try setting the connect_timeout kwarg but that's already None by default.

Connecting to CloudSQL from Dataflow in Python

I'm trying to connect to CloudSQL with a python pipeline.
Actual situation
I can do it without any problem using DirectRunner
I can not connect using DataflowRunner
Connection function
def cloudSQL(input):
import pymysql
connection = pymysql.connect(host='<server ip>',
user='...',
password='...',
db='...')
cursor = connection.cursor()
cursor.execute("select ...")
connection.close()
result = cursor.fetchone()
if not (result is None):
yield input
The error
This is the error message using DataflowRunner
OperationalError: (2003, "Can't connect to MySQL server on '<server ip>' (timed out)")
CloudSQL
I have publicIP (to test from local with directrunner) and I have also trying to activating private IP to see if this could be the problem to connect with DataflowRunner
Option2
I have also tried with
connection = pymysql.connect((unix_socket='/cloudsql/' + <INSTANCE_CONNECTION_NAME>,
user='...',
password='...',
db='...')
With the error:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)")
Take a look at the Cloud SQL Proxy. It will create a local entrypoint (Unix socket or TCP port depending on what you configure) that will proxy and authenticate connections to your Cloud SQL instance.
You would have to mimic the implementation of JdbcIO.read() in Python as explained in this StackOverflow answer
With this solution I was able to access to CloudSQL.
For testing purpose you can add 0.0.0.0/0 to CloudSQL publicIP without using certificates
I created a example using Cloud SQL Proxy inside the Dataflow worker container, connection from the Python pipeline using Unix Sockets without need for SSL or IP authorization.
So the pipeline is able to connect to multiple Cloud SQL instances.
https://github.com/jccatrinck/dataflow-cloud-sql-python
There is a screenshot showing the log output showing the database tables as example.

python pyodbc : how to connect to a specific instance

Am trying to connect to a specific instance of SQL Server and get some data from system tables. Am connecting using this code snippet:
connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102;DATABASE=master;INSTANCE=instance1;UID=sql2008;PWD=password123;Trusted_Connection=yes')
...
cursorObj.execute("select * from sys.dm_os_sys_info")
row = cursorObj.fetchone()
print("rows from table ",row)
however am getting the values for the default instance only, but not able to get the value for 'instance1'. So, giving instance name in 'INSTANCE=instance1' really seems to have no effect. Even without it (tried giving 'PORT=1443', the instance's port number), am getting the values only for the default SQL Server instance. How to force it to get the values for the specific instance?
Authentication
First, you're providing both uid/pwd (SQL Server authentication) and trusted_connection (Windows authentication). Pick one, you can't use both. I'll assume SQL Server authentication for the following examples.
Connection strings
Connecting to named instance instance1 using the instance name:
connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102\instance1;DATABASE=master;UID=sql2008;PWD=password123')
Connecting to named instance using TCP/IP using the port number 1443:
connSqlServer = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=192.106.0.102,1443;DATABASE=master;UID=sql2008;PWD=password123')
Keyword alternative
pyodbc.connect() supports keywords, I think these are easier to read and you don't have to do any string formatting if you're using variables for connection string attributes:
Named instance:
connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
server='192.106.0.102\instance1',
database='master',
uid='sql2008',pwd='password123')
TCP/IP port:
connSqlServer = pyodbc.connect(driver='{SQL Server Native Client 10.0}',
server='192.106.0.102,1443',
database='master',
uid='sql2008',pwd='password123')

Connect on remote MySQL database through Python

I have tried the following script but unfortunately doesn't work. I am using a free MySQL database provider. Any ideas?
import MySQLdb
myDB = MySQLdb.connect(host="208.11.220.249",port=3306,user="XXXXX",passwd="XXXXX",db="XXXXX")
cHandler = myDB.cursor()
cHandler.execute("SHOW DATABASES")
results = cHandler.fetchall()
for items in results:
print items[0]
Currently, I am getting the following error:
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1044, "Access denied for user 'XXXXX'#'%' to database 'XXXXX'")
GRANT ALL
ON *.*
TO user#192.168.39.17 -- client ip address
IDENTIFIED BY 'pwd';
Edit
This is SQL that you'd run on the database in order to ensure that the user has access to everything. pwd is the user's password.
Basically, this answer assumes that the connection issue is a credentials issue.
This is what I would do
See if the port is actually open on that machine.
On the machine you are connecting from, open console/cmd/terminal and see if you can connect using mysql -u XXXX -h 208.11.220.249 -p. If your mysql client can not connect, then there is no way you can connect using python
You don't have permission for connecting to database with this user.
Follow these steps:
1.try connecting to mysql DB:
mysql -h208.11.220.249 -uXXXXX -pXXXXX XXXXX
2.If you don't have permission for connecting to DB ,try creating user that has remote permission
GRANT ALL
ON DB.* -- Database name
TO user#ip -- client ip address
IDENTIFIED BY 'pwd';
3.on the last check my.cnf . "bind-address" must be 0.0.0.0 If you want to connect all remote addresses.
open your database in mysql and type the following :
mysql> GRANT ALL ON *.* TO root#'x' IDENTIFIED BY 'Your-password'
where x is the ip address of the user that wants to access your db.
use this link for more information:
How to Allow MySQL Client to Connect to Remote MySQL server

Categories