0

I am trying to connect to ClearDb with Heroku from python app I use mysql.connector

My code look like this:

conn = mysql.connector.connect(
      host="clearDbHost",
      user="123qwe",
      password="123qwe",
      database="heroku_4fsdfsdf30daf8",
      port=3306,
      autocommit = True
    )
curs = conn.cursor()

Is connection crating the tables, but after this is disconnect and can't execute curs.execute() command later in the code I got this error:

Traceback (most recent call last): File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 535, in cmd_query self._cmysql.query(query, _mysql_connector.MySQLInterfaceError: Lost connection to MySQL server during query

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/app/twitter.py", line 855, in getMyFollowersToDatabse() File "/app/twitter.py", line 186, in getMyFollowersToDatabse curs.execute("INSERT INTO users (username, follow_id) VALUES ('" + user.username + "', '" + user.id + "')") File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/cursor_cext.py", line 269, in execute result = self._cnx.cmd_query(stmt, raw=self._raw, File "/app/.heroku/python/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 540, in cmd_query raise errors.get_mysql_exception(exc.errno, msg=exc.msg, mysql.connector.errors.OperationalError: 2013 (HY000): Lost connection to MySQL server during query

I see in the Clear Db like is havin ?reconnect=true in the end of the database name.I am not using curs.close()

  • Oh lost connection might be due to a MySql Script timeout? This happens either when its a big insert, or when there is an IF/THEN type scenario and it gets stuck in the loop and so it times out instead of finishing. If you have access to the MYSQL console check and perhaps increase the server timeout setting, unless unstead for some reason its doing the lifting in Python and that is causing the hang. Depends where you validate and compile your data before insert. Short answer - If you can get MySql to format your data for you, you can get quicker result times to avoid timeout. – easleyfixed Jul 19 '22 at 18:54
  • yes is inserting in the loop around 50 records – Mike Hadzhiev Jul 19 '22 at 19:03
  • How many records does it get before it hangs or does it not complete any? That isn't that many, are there quite a few columns and how many of them are strings that are varchar(40) or larger? If its all Integer inserts it makes no sense, but if you got a few larger strings, then perhaps the heavier load is timing out. Try this: https://stackoverflow.com/questions/14726789/how-can-i-change-the-default-mysql-connection-timeout-when-connecting-through-py – easleyfixed Jul 19 '22 at 19:05
  • To check the timeout : SELECT @@GLOBAL.MAX_EXECUTION_TIME, @@SESSION.MAX_EXECUTION_TIME; To change it : SET SESSION MAX_EXECUTION_TIME=2000; SET GLOBAL MAX_EXECUTION_TIME=2000; – easleyfixed Jul 19 '22 at 19:08
  • Is not inserting any data. is only two columns and are not big – Mike Hadzhiev Jul 19 '22 at 19:12
  • Access denied; you need (at least one of) the SUPER privilege(s) for this operation I got this message I am using free database service and I don't have previleges – Mike Hadzhiev Jul 19 '22 at 19:22
  • Ahh if it is really that small of an insert, its likely what Mike said is most likely the issue here – easleyfixed Jul 19 '22 at 19:54
  • I change the database provider and is working great – Mike Hadzhiev Jul 20 '22 at 19:51
  • Don't use Clear Db addon on Heroku use Jaws – Mike Hadzhiev Jul 20 '22 at 19:51

0 Answers0