MySQL connector - connection forcibly closed by host - python

I'm connecting to 2 databases via mysql.connect
oldCnx = mysql.connector.connect(user='root', password='root', host='127.0.0.1', database='testdb', connect_timeout=10000)
Connection to remote db set in the same way (just with other credentials). After a 20 (or 60) minutes I get error:mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at '127.0.0.1:3306', system error: 10054. An existing connection was forcibly closed by the remote host.
What to do? Do I need to expand timeout somehow or reconnect when connection is forcibly closed? If so, how would I do it?

If you want to modify the timeout you can probably use the connection_timeout argument in connect()
Reconnecting is also possible using cnx.reconnect(attempts=1, delay=0) in a try/except block.

Related

What does 'DPY-6005: cannot connect to database. Connection failed with "[Errno 61] Connection refused"' mean with python-oracledb

On macOS with Python 3.9.6 the Python code using Oracle's python-oracledb driver:
import oracledb
import os
un = os.environ.get("PYTHON_USERNAME")
pw = os.environ.get("PYTHON_PASSWORD")
cs = "localhost/orclpdb1"
c = oracledb.connect(user=un, password=pw, dsn=cs)
gives the error:
DPY-6005: cannot connect to database. Connection failed with "[Errno 61] Connection refused"
on Linux the error is like:
DPY-6005: cannot connect to database. Connection failed with "[Errno 111] Connection refused"
What do these mean?
[Update: in python-oracledb 1.0.1 the error is wrapped with DPY-6005. In 1.0.0 just the lower level Python part of the error was shown.]
One scenario is that the database port you used (or the default port 1521) is
not correct. Find the correct port and use that instead. For example if your
database listener is listening on port 1530 instead of the default port 1521, then you could try the connection string:
cs = "localhost:1530/orclpdb1"
Make sure the hostname is correct: you may have connected to the wrong machine.
In my experience, "connection refused" often means that the connection was actively denied, which could mean that the database is protected by a firewall. If you have already confirmed the hostname and port are correct and are still getting this error then determine if there is a firewall, either on the database server itself or elsewhere on the network, and have a rule created to allow access or disable it entirely (assuming it is safe to do so).

Can not connect from AWS Lambda to RDS Postgres but can establish socket connection

RDS without access to the internet, the same VPC as Lambda. I can connect to RDS from EC2 in the same VPC. I tried to establish a socket connection from Lambda to RDS successfully:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(s.connect(('xxx.cmebgbnbfkax.eu-west-1.rds.amazonaws.com', 5432)))
In logs I see None so connection was successful, but I get timeout on connection through psycopg2:
psycopg2.connect(
host='xxx.cmebgbnbfkax.eu-west-1.rds.amazonaws.com',
dbname=<my db name>,
user=<my usernamr>,
password=<my password>)
I set maximum timeout on Lambda - 60 seconds. Security group allow access to the port 5432. As I understand if I can establish socket connection to the database - it is mean that all preferences about networking is ok? So where can be a problem?
My solution: I migrated to the DynamoDB - now I do not need compiled psycopg2 in deployment archive with statically linked libraries.

Python psycopg2 timeout

I have a huge problem:
There seems to be some hardware problems on the router of the server my python software runs on. The connection to the database only is successfull about every third time. So a psycopg2.connect() can take up to 5 minutes before I get an timeout exception.
2014-12-23 15:03:12,461 - ERROR - could not connect to server: Connection timed out
Is the server running on host "172.20.19.1" and accepting
That's the code I'm using.
# Connection to the DB
try:
db = psycopg2.connect(host=dhost, database=ddatabase,
user=duser, password=dpassword)
cursor = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
except psycopg2.DatabaseError, err:
print(str(err))
logging.error(str(err))
logging.info('program terminated')
sys.exit(1)
I tried some timeout additions for the query, but that didn't helped, since the connection didn't got established at all.
Is there a way, I can stop the program immediately, when the connection couldn't be established?
When using the keyword arguments syntax to the connect function it is possible to use any of the libpd supported connection parameters. Among those there is connect_timeout in seconds:
db = psycopg2.connect (
host=dhost, database=ddatabase,
user=duser, password=dpassword,
connect_timeout=3
)
http://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-PARAMKEYWORDS
http://initd.org/psycopg/docs/module.html
A connection time out raises an OperationalError exception.

pymsql not connecting to a database

I am trying to make software that works on a large number of people's computers by connecting to a login server. I have set up a MSQL server using 24hosting and added a database. I then tried to access the database using python, but it gives me the error "No connection could be made because the target machine actively refused it".
I need everyone who downloads this program to be able to connect, not just this computer.
This is probably something to do with my server, and not to do with code, but I will post the code below anyway.
from os import getenv
import pymysql
server = getenv("31.220.17.13")
user = getenv("shutdow1_user")
password = getenv("DSAEWQ321")
conn = pymysql.connect(server, user , password, "tempdp")
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
cur.close()
conn.close()
Try to run tcpdump or tshark host 31.220.17.13 to see what exactly happens.
Most likely TCP connection to mysql port is filtered (you would see RST replies on SYN send), or if you see that connection is closed by remote party after TCP session is established - that would be a sign of remote mysql server not configured properly to accept client connection for your IP/username/password/database.

How to decrease the timeout for my python application connecting to mysql server

We have a python application running with uwsgi, nginx.
We have a fallback mechanism for DBs. ie., if one server refuses to connect, we connect to the other server. But the issue is that the connection takes more than 60s to timeout.
As nginx times out in 60s, it displays the nginx error page. Where can we change the timeout for connecting to mysql servers so that we can make three attempts of connection to mysql in the given 60s nginx timeout period?
We use Web2py and default DAL object with pymysql adapter
you're talking about the option connect_timeout?
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='mysql', connect_timeout=20)
in DAL terms this option will be something about this (not tested)
db = DAL('mysql://username:password#localhost/test', driver_args={connect_timeout=20})

Categories