title: mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'l ocalhost:3306' (111 Connection refused)
body: <p>I am using <strong>mysql connect</strong> in the <strong>python script</strong>.
  Python script needs to work for long time
  It works fine for a few hours but it has issues to connect mysql after some hours
  Here is my codes.</p>
  <p><strong>Code to connect mysql</strong></p>
  <pre><code>host = &quot;localhost&quot;
  user = &quot;root&quot;
  def get_connection():
    connection = mysql.connector.connect(
      host=host,
      user=user,
      password=password,
      database=database
    )
    return connection
  </code></pre>
  <p><strong>Script repeats below codes to insert the results</strong></p>
  <pre><code>connection = get_connection()
  cursor = connection.cursor()
  cursor.executemany(insert_query, records_to_insert)
  connection.commit()
  posts = cursor.rowcount + 1
  cursor.execute(&quot;&quot;&quot;UPDATE topics set posts=%s WHERE tid=%s&quot;&quot;&quot;, (posts, topic_id, ))
  connection.commit()
  cursor.close()
  connection.close()
  </code></pre>
  <p>Could you give me a solution to resolve my issues?
  Thanks.</p>