Remote access to mySQL using Python - python

I have trouble connecting to my mySQL database remotely through Python.
I use the following to connect to mySQL:
import mysql.connector
cnx = mysql.connector.connect(host='XXX.XXX.XXX.X',user='XXXX',password='XXXXXX',database='testdb')
But I get the following error:
2003: Can't connect to MySQL server on '%HOST%:3306' (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
The server is running and when I run the same code on the computer I run the server from using 'localhost'
import mysql.connector
cnx =
mysql.connector.connect(host='localhost',user='XXXX',password='XXXXXX',database='testdb')
it works and I can modify the data in the database. I'm trying to connect it remotely from another computer though.
I've tried using GRANT ALL ON *.* TO User#Host IDENTIFIED BY 'password'; but no result. I checked my firewall and allowed all incoming and outgoing connections through port 3306 which is used by default.
I'm new to mySQL and really have no clue what to do. I don't even know if I use the correct hostname :') I use the IP address of the computer I run the server from,I think that's right.

You dont need to GRANT ALL privilage to the user. You need to tell MYSQL that this user is allowed to login from a remote location.
In fact as you are allowing remote access through this user account now, you should make sure that it can access only the database(s) it needs to, and definitely cannot use GRANT
For example
CREATE USER 'myuser'#'%' IDENTIFIED BY 'mypass';
Will allow this user to connect from any ip address. To be more secure you should try to be more specific and specify a individual ip address if you can
CREATE USER 'myuser'#'11.22.33.44' IDENTIFIED BY 'mypass';
Remember, you are creating a new user account here because you already have a
myuser#localhost
Either way you need to make sure that the password is a strong one, specially is you use the % any ip option

Related

How to connect to a Lotus-Notes database with Python?

I have to extract data from a Notes database automatically for a data pipeline validation.
With HCL Notes I was able to connect to the database, so I know the access works.
I have the following information to access the database:
host (I got both hostname and ip address), domino server name, database name (.nsf filename)
I tried the noteslib library in the below way:
import noteslib
db = noteslib.Database('domino server name','db filename.nsf')
I also tried adding the host to the server parameter instead, but it does not work.
I receive this error:
Error connecting to ...Double-check the server and database file names, and make sure you have
read access to the database.
My question is how can I add the host and the domino server name as well (if it is required)?
Notes HCL authenticates me before accessing the database using the domino server name and the .nsf file. I tried adding the password parameter to the end, but also without luck. I am on company VPN, so that also should not be an issue.
In Order for noteslib to work you need an installed and configured HCL Notes Client on that machine. Only with an installed Notes Client the needed COM registrations and the dlls to connect to Domino are present.
In addition the Notes Client and the python version you are using need to be the same bitness: If Notes Client is 32Bit then python needs to be 32Bit. If Notes Client is 64Bit (only available since 12.0.2) then python needs to be 64Bit as well.
As soon as this requirement is met, you can simply use your example by adding the password parameter as a third parameter to your command:
db = noteslib.Database('domino server name','db filename.nsf', 'yourIDPassword')
If you still get an error when connecting to the server then you might need to put the server common name and its IP address into your hosts file.
So if your Domino- Servername is
YourServer/YourOrganization
and the IP address of that server is
192.168.1.20
then you put this into your hosts:
yourserver 192.168.1.20
You can connect using com on windows.
I use this python library https://pypi.org/project/pywin32/
import win32com.client
import sys
notesServer = "Servername/Domain"
notesFile = "file.nsf"
notesPass = ""
#Connect to notes database on server
notesSession = win32com.client.Dispatch('Lotus.NotesSession')
notesSession.Initialize(notesPass)
notesDatabase = notesSession.GetDatabase(notesServer,notesFile)

Issue in connecting Python with MySQL on Google Cloud Platform

I have used the following code in Python:
import mysql.connector as mysql
import sys
HOST = "34.87.95.90"
DATABASE = "CAO_db"
USER = "root"
PASSWORD = "*********"
db_connection = mysql.connect(user=USER, password=PASSWORD, host=HOST, database=DATABASE)
cur = db_connection.cursor()
When I run the above code, I get the following error messages:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
InterfaceError: 2003: Can't connect to MySQL server on '34.87.95.90:3306' (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
I am not sure of how to fix my code and/or resolve the given errors. Please ask me if you would like more details of the error messages to help with the issue. I would greatly appreciate all the help I can get towards resolving the issues.
One thing I'm not seeing here is whether or not you have configured your Cloud SQL instance to accept connections.
You can configure it to accept connections from within the GCP stratosphere using their "Private IP" internal networking magic, AND you can configure it to accept connections from other machines using a combination of Public IP and either an authorized external network (like if you were accessing your GCP Cloud SQL instance from, say, an Amazon EC2 instance), or their Cloud SQL Proxy tool (which is what I use to connect to my Cloud SQL instance from my laptop).
In the GCP Console, go to your project
From the hamburger menu, select SQL
Click on your Cloud SQL instance
In the left nav, click on Connections
If you have Private IP checked and you're running this code on a GCP Compute/GKE resource, confirm that the "Network" field is set to the network used by that resource.
If you're just trying to get a connection from your local machine and you don't have a static IP to whitelist, your best option is to use Public IP in combination with Cloud SQL Proxy.
Cloud SQL Proxy essentially creates a TCP tunnel that allows your laptop to connect to 'localhost' on a port you specify, and it then redirects your connection to the remote Cloud SQL instance.
Once you've established that your networking situation isn't the problem, you could use the same Python connection code that you wrote above, but change HOST to 127.0.0.1 and add an attribute for PORT=3308.
EDITED to add: I suggest using PORT=3308 for your cloud_sql_proxy connection so that it doesn't interfere with any existing port 3306 (MySQL default) connections that you may already be actually running on your local machine. If this isn't the case, you can either omit the PORT attribute or keep it explicit, but change it to 3306.

DB2 : Python ibm_db2 connecting, which port to use?

I am attempting to connect to a remote db2 instance. I seem to be having connection port issues or protocol issues. Below is a sample connection setting. What is the default connection port using TCPIP & python? I am reaching the server but unable to create a connection to database. Database exists.
connection = ibm_db.connect("DATABASE=DATABASE_NAME;HOSTNAME=host;PORT=50000;PROTOCOL=TCPIP;UID=username;PWD=password;", "", "")
Im receiving the following error:
Exception: [IBM][CLI Driver] SQL30061N The database alias or database name "DATABASE_NAME " was not found at the remote node. SQLSTATE=08004 SQLCODE=-30061
The error message seems clear but the cause might vary. Most likely either the database-name or the port-number is incorrect.
You get that message if a Db2-server responded indicating Db2 cannot find the specified database on HOSTNAME in the Db2-instance listening on the specified port-number.
A Db2-LUW hostname might have more than one Db2-instance running concurrently (each listening on different port-numbers), according to the hardware-resources available.
A Db2-Linux/Unix/Windows instance can have many physical databases inside it, each with a distinct name and one or more aliases.
Ask your DBA or a colleague for the correct database-name and port-number per hostname.
Alternatively ssh (or remote-desktop) to that hostname, find the owner (userid) of the process listening on port 50,000 (or whatever port you are using), become that userid (for Linux/Unix: use su or sudo ) and use db2 list db directory command to show local databases in that Db2-instance. For Db2-servers on Windows: start > db2cwadmin.bat > db2 list db directory . On Linux/Unix, use ps -ef | grep db2sysc to see how many Db2-instances are running and you can use that information (along with netstat) to discover the port on which they are listening.

pyodbc: How to test whether it's possible to establish connection with SQL server without freezing up

I am writing an app with wxPython that incorporates pyodbc to access SQL Server. A user must first establish a VPN connection before they can establish a connection with the SQL server. In cases where a user forgets to establish a VPN connection or is simply not authorized to access a particular server, the app will freeze for up to 60+ seconds before it produces an error message. Often, users will get impatient and force-close the app before the error message pops up.
I wonder if there is a way to test whether it's possible to connect to the server without freezing up. I thought about using timeout, but it seems that timeout can be used only after I establish a connection
A sample connection string I use is below:
connection = pyodbc.connect(r'DRIVER={SQL Server};SERVER=ServerName;database=DatabaseName;Trusted_Connection=True;unicode_results=True')
See https://code.google.com/archive/p/pyodbc/wikis/Connection.wiki under timeout
Note: This attribute only affects queries. To set the timeout for the
actual connection process, use the timeout keyword of the
pyodbc.connect function.
So change your connection string to:
connection = pyodbc.connect(r'DRIVER={SQL Server};SERVER=ServerName;database=DatabaseName;Trusted_Connection=True;unicode_results=True', timeout=3)
should work
took a while before it threw an error message about server not existing or access being denied
Your comment conflates two very different kinds of errors:
server not existing is a network error. Either the name has no address, or the address is unreachable. No connection can be made.
access being denied is a response from the server. For the server to respond, a connection must exist. This is not to be confused with connection refused (ECONNREFUSED), which means the remote is not accepting connections on the port.
SQL Server uses TCP/IP. You can use standard network functions to determine if the network hostname of the machine running SQL Server can be found, and if the IP address is reachable. One advantage to using them to "pre-test" the connection is that any error you'll get will be much more specific than the typical there was a problem connecting to the server.
Note that not all delay-inducing errors can be avoided. For example, if the DNS server is not responding, the resolver will typically wait 30 seconds before giving up. If an IP address is valid, but there's no machine with that address, attempting a connection will take a long time to fail. There's no way for the client to know there's no such machine; it could just be taking a long time to get a response.

Connect MySQL not using local host [duplicate]

This question already has an answer here:
1045, "Access denied for user 'username'#'NOT-local' (using password: YES)"
(1 answer)
Closed 7 years ago.
I am trying to connect to mySQL DB in python, not through my local host, when I connect through localhost I do it like this
db = MySQLdb.connect(host='localhost', user='root', passwd='my-pass', db='my-database')
But now I am not sure what do I put as the host, in this line of code, everything else will be the same, but what do I put as the host, when Im not local?
Or do I need to make a new user to do this, if possible I would like to do with same user.
Thanks
Server's IP
db = MySQLdb.connect(host='mysql4.host.something', user='root', passwd='my-pass', db='my-database')
1) You have to know your hosts IP adress. You can ping your webside on command.
2) Your host parameter should be your IP adress
3) You need to create your database on your Cpanel or Plesk panel and have to write username and password parameters. You create the user while creating the database.
As a matter of practice, you should not be using user 'root' to query the database. 'root' is intended to be used strictly for database administration tasks such as starting, stopping, backing up, managing user accounts &c.
You should create a new user 'spenf10'#'%' (the #'%' part will allow you to log in from any IP address or hostname), then GRANT ALL PRIVILEGES ON *.* TO 'spenf10'#'%' to give yourself access to all the data, and use that user account for all data access. (Change 'spenf10' to whatever name you want to use.)
NOTE I am assuming you own the server where you want to do this, ie. it is the same box you connected to via localhost. If this is not the case, you will have to discuss the matter with the person who does own the box.

Categories