1

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?

  • Possible duplicate of [python: \[Errno 10054\] An existing connection was forcibly closed by the remote host](https://stackoverflow.com/questions/8814802/python-errno-10054-an-existing-connection-was-forcibly-closed-by-the-remote-h) – Alex K. Dec 20 '17 at 12:56

1 Answers1

0

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.

Slawomir Szor
  • 323
  • 3
  • 9
  • Ah, somehow I misspelled "connection_timeout" as "connect_timeout". I'll try it and reconnect function, see if it helps. –  Dec 20 '17 at 13:23
  • You were right with `connect_timeout` according to [this](https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html) page (section `Compatibitility with Other Connection Interfaces`), but availability depends on the interface. Probably some engine differences :) – Slawomir Szor Dec 20 '17 at 13:32
  • Wrapped code in try/except/else. Working now. Encountered "Lost connection to MySQL server during query", so does that mean reconnect is not working or what? –  Dec 20 '17 at 14:19
  • Main problem fixed using try/except/else block. try: inserting; except: reconnect; else: inserting; Thanks! –  Dec 20 '17 at 14:32