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
Related
I'm a novice trying to spin up my first webapp with a combination of Fly.io, Django, and a postgres DB but I'm having some trouble and can't find an answer in walkthroughs or Q&A.
I've set up a simple "Hello world" Django app (models.py is empty so far) and I'm trying to get all the components up and running before I build it out any further.
I've successfully deployed my app on Fly.io with no errors
I've created a postgres cluster on Fly.io using the instructions here: https://fly.io/docs/postgres/
I've attached the cluster to my app, which generates a DB and sets an environment variable with the appropriate details (username, password, port, host, dbname)
I've updated my settings.py file:
DATABASES = {}
DATABASES["default"] = dj_database_url.config(conn_max_age=600, ssl_require=True)
I've added to my fly.toml:
[[services]]
internal_port = 5432 # Postgres instance
protocol = "tcp"
# Open port 10000 for plaintext connections.
[[services.ports]]
handlers = []
port = 10000
I've confirmed I can get into the psql shell with flyctl postgres connect -a MYAPP-pg
But unfortunately when I run python manage.py migrate to check that everything is working, I get the following error:
File "<my_path>\venv\lib\site-packages\django\db\backends\base\base.py", line 282, in ensure_connection
self.connect()
File "<my_path>\venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "<my_path>\venv\lib\site-packages\django\db\backends\base\base.py", line 263, in connect
self.connection = self.get_new_connection(conn_params)
File "<my_path>\venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "<my_path>\venv\lib\site-packages\django\db\backends\postgresql\base.py", line 215, in get_new_connection
connection = Database.connect(**conn_params)
File "<my_path>\venv\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not translate host name "top2.nearest.of.MYAPP-pg.internal" to address: Unknown host
Any ideas what might be happening? Any help would be very much appreciated!
I am aware that this question has been asked multiple times, but I haven't found a question that pertains to the same issue I am dealing with. I have a MySQL database hosted on Google Cloud SQL. I am running a python script out of a Google Cloud VM to connect to the database to run queries. Everything used to run fine, but now I am getting the following error on my initial connection to the server.
File "/usr/local/lib/python3.5/dist-packages/pymysql/__init__.py", line 94, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 327, in __init__
self.connect()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 597, in connect
self._get_server_information()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 966, in _get_server_information
packet = self._read_packet()
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 656, in _read_packet
packet_header = self._read_bytes(4)
File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 702, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
And here is the code I am using to connect to the server
connection = pymysql.connect(host='127.0.0.1',
user='xxxxxx',
password='xxxxxx',
database='foobar',
cursorclass=pymysql.cursors.DictCursor,
max_allowed_packet=16777216,
connect_timeout=100)
Initially, my connection code did not have the max_allowed_packet and connect_timeout, and it was working fine. After some research into my issue, these seemed to resolve others' issues, but it hasn't resolved mine.
You must add wait_timeout and start by 10000 and then see.
The other will not help, but i added iy you have another timeout that will not be catch by connection and wait timeout
interactive_timeout Number of seconds the server waits for activity on an interactive connection before closing it
wait_timeout Number of seconds the server waits for activity on a connection before closing it
It seems
use a mysql command right after the connection
SET session wait_timeout=300;
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)
I've recently installed the official MySQL extension for Python. However, when connecting to the server it asks me to select the database, but I have not made a database yet.
I don't really know what to do here. So I tried to connect using my information without the database but received errors with the following code:
import mysql.connector
cnx = mysql.connector.connect(user='ubuntulogin', password='ubuntupassword',
host='localhost')
cursor = cnx.cursor()
query = ("CREATE DATABASE database")
cursor.execute(query)
cursor.close()
cnx.close()
Please let me know any issues with my code or how to get MySQL information when I don't know mydatabase name.
Thanks
EDIT: My error message when running the code was:
File "/home/liam/sqltest.py", line 3, in <module>
cnx = mysql.connector.connect(user='ubuntulogin', password='ubuntupassword', host='localhost')
File "/usr/lib/python2.7/dist-packages/mysql/connector/__init__.py", line 162, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 129, in __init__
self.connect(**kwargs)
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 454, in connect
self._open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line 417, in _open_connection
self._socket.open_connection()
File "/usr/lib/python2.7/dist-packages/mysql/connector/network.py", line 475, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
Okay, so it turns out that I am a complete idiot. I was under the impression that MySQL was pre-installed and running since I am used to PHP. However, I just installed it and use my code above and everything seems to be working.
Thanks to #DYZ for pointing out that it looked like I hadn't started the MySQL server and my credentials were incorrect (which turned out to be both true.
You forgot something)
cursor.execute("CREATE DATABASE database;")
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.