Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL, should I stay connected or connect when needed?

Tags:

python

mysql

I have been logging temperatures at home to a MySQL database (read 10 sensors in total every 5 minutes), and have been using Python, but I am wondering something...

Currently when I first run my program, I run the normal connect to MySQL, which is only run once.

db = MySQLdb.connect(mysql_server, mysql_username, mysql_passwd, mysql_db)
cursor = db.cursor()

Then I collect the data and publish it to the database successfully. The script then sleeps for 5 minutes, then starts again and collects and publishes the data again and so on. However, I only connect once, and I don't ever disconnect; it just keeps going in a loop. I only disconnect if I terminate the program.

Is this the best practice? That is, keeping the connection open all the time to the MySQL server, or should I disconnect after I have done a insert/commit?

The reason I ask: every now and then, I have to restart the script because maybe my MySQL server has gone offline or some other issue. Should I:

  • Keep doing what I am doing and just handle any MySQL database disconnections with a reconnect,
  • Put it in the crontab to collect data every five minutes and have no loop and no sleep, or
  • Something else?
like image 443
Matt. Avatar asked Oct 06 '13 12:10

Matt.


People also ask

Should you keep database connection open?

For fast response time and high throughput, it's actually best to keep database connections open and reuse them for subsequent requests. Most database frameworks offer some kind of connection pool mechanism where a request handler can get a database connection for its work and return it to the pool afterwards.

Should I close MySQL connection after every query?

Do I need to close Mysqli connection? Explicitly closing open connections and freeing result sets is optional. However, it's a good idea to close the connection as soon as the script finishes performing all of its database operations, if it still has a lot of processing to do after getting the results.

What is the correct way to connect to a MySQL database?

To connect to the database server, confirm that the MySQL Database Server is running on your machine, right-click the Databases > MySQL Server node in the Services window and choose Connect. You might be prompted to supply a password to connect to the server.

Do you need Internet connection for MySQL?

recommendation is you should be in the network(LAN) both your local host and remote server so its possible ,, There isnt necessary to have an internet connections.


1 Answers

MySQL servers are configured to handle a fixed limited number of connections. It's not a good practice to tie up a connection that you are not using constantly. So typically you should close the connection as soon as you are done with it, and reconnect only when you need it again. MySQLdb's connections are context mangagers, so you could use the with-statement syntax to make closing the connection automatic.

connection = MySQLdb.connect(
    host=config.HOST, user=config.USER,
    passwd=config.PASS, db=config.MYDB, )
with connection as cursor:
    print(cursor)
    # the connection is closed for you automatically 
    # when Python leaves the `with-suite`.

For robustness, you might want to use try..except to handle the case when (even on the first run) connect fails to make a connection.

Having said that, I would just put it in a crontab entry and dispense with sleeping.

like image 196
unutbu Avatar answered Oct 01 '22 03:10

unutbu