Timed out in SQL Conection - python

I using a code in python to synchronize data on SQL server in Amazon RDS (Maria DB).
When I run my code in Spider (windows) it works, but when I run in a board running debian, the connection returns timeout.
import mysql.connector
sqlConnRemoto = mysql.connector.connect(user='xxxx', password='xxxx', database='xxxx', host='xxxx.crmriwcyocxl.sa-east-1.rds.amazonaws.com', port = 3306, connection_timeout = 10)
sqlCurRemoto = sqlConnRemoto.cursor()
My server data (user, password, host....) is ok, because it works on windows. But did´not work on debian. What is the problem?
File "/usr/lib/python3/dist-packages/mysql/connector/init.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 94, in init
self.connect(**kwargs)
File "/usr/lib/python3/dist-packages/mysql/connector/abstracts.py", line 722, in connect
self._open_connection()
File "/usr/lib/python3/dist-packages/mysql/connector/connection.py", line 207, in _open_connection
self._socket.open_connection()
File "/usr/lib/python3/dist-packages/mysql/connector/network.py", line 512, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'xxxx.crmriwcyocxl.sa-east-1.rds.amazonaws.com:3306' (timed out)

Related

Unable to establish connection to MySQL server 'Authentication plugin is not supported'

I'm trying to establish a connection to MySQL server from IDLE and I can't figure out why I'm getting an error.
>>> dbconfig = {'host': '127.0.0.1',
'user': 'vsearch',
'password': 'vsearchpasswd',
'database': 'vsearchlogDB'}
>>> import mysql.connector
>>> conn = mysql.connector.connect(**dbconfig)
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
conn = mysql.connector.connect(**dbconfig)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/abstracts.py", line 719, in connect
self._open_connection()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 208, in _open_connection
self._do_auth(self._user, self._password,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/connection.py", line 137, in _do_auth
packet = self._protocol.make_auth(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/protocol.py", line 99, in make_auth
packet += self._auth_response(client_flags, username, password,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/protocol.py", line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/mysql/connector/authentication.py", line 190, in get_auth_plugin
raise errors.NotSupportedError(
mysql.connector.errors.NotSupportedError: Authentication plugin 'caching_sha2_password' is not supported
I've tried these suggestions I found on stackoverflow :
shutting down mysql.server, installing mysql-connector-python and restarting mysql.server
passing an auth_plugin argument to the connect() method (as seen below)
auth_plugin='mysql_native_password'
made sure MySQL in listening to port 3306 (answer below)
localhost:mysql (LISTEN)
Versions:
mysql Ver 8.0.32 for macos12.6 on x86_64 (Homebrew)
Python 3.9.1
MacOS 12
Managed to fix issue by uninstalling MySQL connector and reinstalling it. Had issues with permissions so make sure you have permissions to read and write in connector directories.
This tutorial might help if your trying to connect to MySQL with MySQL-connector-python :
https://pynative.com/python-mysql-database-connection/

Can't connect Python program to remote MySQL database over the same network

Can someone please help me? I am trying to make a simple python - MySQL connection. the code is running on my Dell-Windows Laptop, while the server is running on my Acer-Linux Laptop, they are connected on the same network. But upon running the query it prompts me with this error : Firewall is disabled for both. I tried pinging both of them vise versa and is working. In fact, I have a program with LibreOffice Base and they are interacting properly. But when I try to translate just a simple query in PYTHON seems like it is not working, I just don't know what could be the error on my code. Thank you so much for you help.
Traceback (most recent call last):
File "C:\Users\JM Dela Cruz\Documents\pyproj\myproject\test1.py", line 3, in <module>
mydb = mysql.connector.connect(
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\pooling.py", line 287, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\connection.py", line 137, in __init__
self.connect(**kwargs)
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\abstracts.py", line 1095, in connect
self._open_connection()
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\connection.py", line 533, in _open_connection
self._socket.open_connection()
File "C:\Users\JM Dela Cruz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\mysql\connector\network.py", line 569, in open_connection
raise InterfaceError(
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '198.168.101.42: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)
My Python Code :
import mysql.connector
mydb = mysql.connector.connect(
host="198.168.101.42",
user="vsearch",
password="vsearchpasswd",
database="classicmodels",
port="3306"
)
mycursor = mydb.cursor()
_SQL="Select * From employees"
myresult = mycursor.execute(_SQL)
print(myresult.fetchall())

Mysql database not connecting to python

I am trying to connect python to mysql by this code--
(I am using the mysql.connect library)
import mysql.connector
cnx = mysql.connector.connect(user='root',
password='password',
host='127.0.0.1',
database='db')
print(cnx)
cnx.close()
But it continues to throw the error--
Traceback (most recent call last):
File "D:/ted/main.py", line 5, in <module>
database='db')
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\__init__.py",
line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 94, in __init__
self.connect(**kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\abstracts.py",
line 722, in connect
self._open_connection()
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 211, in _open_connection
self._ssl)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 141, in _do_auth
auth_plugin=self._auth_plugin)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py",
line 102, in make_auth
auth_data, ssl_enabled)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py",
line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "C:\Program Files (x86)\Python36-
32\lib\mysql\connector\authentication.py", line 191, in get_auth_plugin
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin
'caching_sha2_password' is not supported
Process finished with exit code 1
I have started the mysql sever in workbench and the also checked the status.
Also database named "db" is also there
Also checked if the host is proper.
-->Removed ssl also.
It seems that your MySQL Server version is 8.x and in that case the default MySQL connector is caching_sha2_password. In the other hand your error is maybe because your python client connector does not support this Authentication Plugin and you should explicitly change the Authentication Plugin to the old one (mysql_native_password).
cnx = mysql.connector.connect(user='root', password='password',
host='127.0.0.1', database='db',
auth_plugin='mysql_native_password')

Trouble establishing connection to server with PyMySQL [WinError 10054]

Pretty new to both Python and SQL, so please excuse my noobishness. We have a server at my company that I'm trying to connect to through some python code. I have the correct IP address and logon credentials, and I can connect to it in SQL Server Management Studio without any issues, but I can't connect to it through PyCharm's database feature or through my code. I'm running python 3.6 if that makes a difference.
This is the code that I'm running (I've omitted the server ip address and logon credentials for security):
import pymysql
db = pymysql.connect(host='server ip',
port=1433,
user='username',
passwd='password',
db='Development DB')
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print("Database version : %s" % data)
db.close()
When I run the code, after about 2 minutes of nothing happening I get this error:
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1021, in _read_bytes
data = self._rfile.read(num_bytes)
File "C:\Program Files\Python35\lib\socket.py", line 575, in readinto
return self._sock.recv_into(b)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/aubrey_s/PycharmProjects/Drawings_Converter/Drawings_Converter.py", line 5, in <module>
db = pymysql.connect(host='ip address', port=1433, user='username', passwd='password', db='Development DB')
File "C:\Program Files\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 706, in __init__
self.connect()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 931, in connect
self._get_server_information()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1245, in _get_server_information
packet = self._read_packet()
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 987, in _read_packet
packet_header = self._read_bytes(4)
File "C:\Program Files\Python35\lib\site-packages\pymysql\connections.py", line 1029, in _read_bytes
"Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([WinError 10054] An existing connection was forcibly closed by the remote host)')
From what I can understand, it looks like a connection was established, but the server forced it to close after a while. Can anyone offer me some more insight into what the problem might be? Thanks in advance!
As I wrote in another post, you may want to try using pymssql, in case your database is not a MySQL DB but a Microsoft SQL DB.

Python mysql (using pymysql) failing to connect

I am using Python Pymysql to connect to AWS RDS db.
This was working all these days but now I am not able to connect to the db.
However when I am using the same machine and same credentials it does connect through Mysql workbench, but through pycharm this does not work.,
Code :
configParser = ConfigParser.RawConfigParser()
configFilePath = r'docs/credentials.cfg'
configParser.read(configFilePath)
User = configParser.get('aws-config','user')
Pswd = configParser.get('aws-config','pswd')
Host = configParser.get('aws-config','hostdps')
Databs = configParser.get('aws-config','databs')
db = pymysql.connect(host=Host, user=User, password=Pswd, db=Databs, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor,local_infile=True)
Error :
File "/usr/local/lib/python2.7/dist-packages/pymysql/__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 688, in __init__
self.connect()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 906, in connect
self._request_authentication()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 1114, in _request_authentication
auth_packet = self._read_packet()
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 961, in _read_packet
packet_header = self._read_bytes(4)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 995, in _read_bytes
"Lost connection to MySQL server during query (%s)" % (e,))
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query ([Errno 104] Connection reset by peer)')
How to make sure that this does not happen on production? (prevent such failures in the future).
why u not use connection pool for these?
connection pool can auto manage connection,so can fix this.
U can try any python mysql pool .
pool example
for this this link
or directly use github handy code .PyMysqlPool

Categories