How to recycle mysql connection while using mysql.connector in python - python

I am working on some flask based api.And it is hosted in python anywhere free version[1]: http://Pythonanywhere.com.
I am using mysql.connector for establishing connection between mysql database and the code.The problem is the mysql database connection will be closed after 5 minutes. Then it will show a MYSQL connection not available error.how to get rid of this error?
I am establishing the connection like this
import mysql.connector
db_config={"host":"","user":"","password":"","database":"","raise_on_warnings":True}# "connection_timeout":86400}
mydb=mysql.connector.connect(**db_config)

In Pythonanywhere free version there will be a limited daily quota for Mysql connection. It seems like you are not closing your connection after use.So after some number of requests your quota will be exceeded. That is why it is showing mysql connection is not available. You should close the connection after use. Then you can reuse mysql connection within allowed quota. If you want more number of connection at a time you can consider upgrading pythonanywherepackage
For more information refer the below link,
https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlconnection-close.html

Related

How to verify if a mysql database is reachable quickly in python?

I have a mysql server running on my local network that isn't reachable off the network, and it needs to stay like this.
When I am on a different network the following code hangs for about 5-10 seconds, my guess is that its retrying to connect for a number of attempts:
import mysql.connector
conn = mysql.connector.connect(
host="Address",
user="user",
password="password",
database="database"
)
Is there a way to "ping" the mysql server before this code to verify that the MySQL server is reachable or limit the number of retries?
At the moment I am having to use a try-except clause to catch if the server is not reaachable.
Instead of trying to implement specific behavior before connecting, adjust the connect timeout so that you don't have to wait - according to your need, the server is down if you can't connect within a short timeframe anyway.
You can use connection_timeout to adjust the socket timeout used when connecting to the server.
If you set it to a low value (seems like it's in seconds - so 1 should work fine) you'll get the behavior you're looking for (and it will also help you catch any issues with the user/password/database values).

Is there a limit on the number of connections opened from a client's side to a SQL Server database?

I'm working on a Python application with an SQL Server database using pyodbc, and I need to open multiple connections from the application's side to the database.
I learnt that the max number of connections allowed on an instance of the SQL Server database is 32,767. My understanding is this is the max that the DB instance "can handle", i.e. all simultaneous users combined.
Is there a limit on how many connections one client can open towards the same database instance, is it also 32,767? If yes, where / how is this limit configured?
Taking an educated guess here that there is no connection count limit on the client side towards the same DB instance, there is a limit of 32,767 on the server side, but the client would be more likely to run out of other resources way before it gets close to this figure.
I was using one connection, one cursor, and threading to insert multiple records, but kept getting a "connection is busy" error, this is resolved by adding "MARS_Connection=yes" in the pyodbc database connection string, thanks to this MS documentation.
Related:
How costly is opening and closing of a DB connection?
Can I use multiple cursors on one connection with pyodbc and MS SQL Server?

pymysql close connection and clean port

I'm making a really transactional script that is migrating data from one nosql db to mysql but after a like 2 or 3 minutes I get this message
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on
'foo.com' ([Errno 99] Cannot assign requested address)")
I'm already opening and closing connections as soon as they finish (since the script is multiprocessing to get a better usage of the server resources) I already have something that waits between tries,
but rather than having a bandaid solution I would like to make my script better, is there a way in which I can make pymysql disconnect and clean the used port?
I'm using an aws ubuntu server for the migration, I'm already aware that ubuntu keeps the ports open for 60 seconds and I've already extended the range of ports to the max, the script is running on a pool of 15 concurrent process, the mysql server is aws hosted (aurora).
So far while testing the script that I'm using processes about 10000 records per second.
Update:
Missed a 0 on the amount of records per second

How to close mysql connection automatically after specified time in python?

I want to close MySQL database connection after 50 sec automatically if queries are taking more than 50 sec? Is there any option in python while making connection or any other solution to do that ?
Reference site for python database connection
Look Connection in this site they might explained about timeout for query, you can pass an integer which is in seconds

python mysql database connection error

I am trying to access the remote database from one Linux server to another which is connected via LAN.
but it is not working.. after some time it will generate an error
`_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '192.168.0.101' (99)")'
this error is random it will raise any time.
each time create a new db object in all methods.
and close the connection as well then also why this error raise.
can any one please help me to sort out this problem
This issue is due to so many pending request on the remote database.
So in this situation MySql closes the connection to the running script.
to overcome this situation put
time.sleep(sec) # here int is a seconds in number that to sleep the script.
it will solve this issue.. without transferring database to local server or any other administrative task on mysql
My solution was to collect more queries for one commit statement if those were insert queries.

Categories