I get [Error 2055] when trying to connect to MySQL on Heroku

Overview

I tried to push and run a python program to connect to MySQL using Heroku's ClearDB, but I couldn't connect with the message ʻerror 2055 Lost connection ~`.

As a result, it was not an error related to MySQL, but the way the program was written, but I would like to summarize it for those who are suffering from the same point.

environment

What not to write in this article

--How to set ClearDB --Detailed operation method of MySQL

Cause

At first, after adding ClearDB, I thought that some settings were necessary, so I investigated various things and tried migration and so on. However, I was able to connect to ClearDB itself, and even if I ran the program that operates by connecting to the database alone, no error occurred, and I could not understand the cause.

At that time, I found this article and read it somehow, but as a cause of SQL operation failure ** When executing a query after disconnecting ** Was written. I took it for granted and tried to go next, but suddenly I looked at my code and found that this was the cause.

Because my code was made like this:

main.py


#Module import

#Import program for database operations
import database

#Main processing
    #Function call in database
    database.get_data()

database.py


import mysql.connector

#Set the connection destination and connect

def get_data():
    #Execute query

connection.commit()
connection.close()

Based on this, if you take a look at this article, the code written outside the function will be executed when database is imported. ..

In other words, the import database of main.py will connect to SQL once, but the get_data () function of database.py is not executed because it is only defined, and the lastconnection The flow is that the connection is closed with .close ().

After that, when I called the function with database.get_data () in main.py, the connection was already disconnected, so of course the query could not be executed and an error occurred.

Even if you execute only database.py, it is understandable that no error occurs because it just disconnects the connection without doing anything after connecting.

Countermeasures

So I modified the code as below and tried to disconnect by calling the function, and I was able to connect without any error.

main.py


#Module import

#Import program for database operations
import database

#Main processing
    #Function call in database
    database.get_data()

    #add to
    database.close_db()

database.py


import mysql.connector

#Set the connection destination and connect

def get_data():
    #Execute query

#Fix
def close_db()
    connection.commit()
    connection.close()

reference

-Case where commit () fails in mysql-connector-python -Run other .py programs in python

Recommended Posts

I get [Error 2055] when trying to connect to MySQL on Heroku
I get an error when trying to install maec 4.0.1.0 with pip
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
I get an error when trying meinheld + WebSocket + mongodb
When I try to import pandas on macOS I get the error No module named'_bz2'
I get an error when I try to raise Python to 3 series using pyenv on Catalina
When I try to connect to MySQL with mysql-connector-python, I cannot connect with the error "SSL connection error: SSL_CTX_set_tmp_dh failed"
When I get an error with Pylint in Atom on Windows
When I get an error with PyInstaller
Connect to mysql
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi
I got an error when trying to install Xgboost and its solution
A story about trying to connect to MySQL using Heroku and giving up
What to do if you get an error when trying to load mnist
When I get a chromedriver error in Selenium
What to do if you get a Permission denied (public key) error when trying to pull on Github
Error when trying to install psycopg2 in Python
Connect to MySQL with Python on Raspberry Pi
Log when I was worried that I could not connect to Wi-Fi on Linux
I got an error when trying to run Hello World in Go language
Connect python to mysql
I get an error when I put opencv in pyautoGUI
I get "too early to create image" on tkinter
I get an error when installing scipy on t2.micro because I don't have enough memory
When I try to install mysqlclient with Django, I get error: command'gcc' failed with exit status 1.
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
When I try to push with heroku, it doesn't work
[Solution] When I try to connect to CloudSQL with GAE, I get an ImportError only when using dev_appserver.
What to do if you get an Undefined error when trying to use pip with pyenv
I got an error when I tried to process luigi in parallel on windows, but the solution
What to do if you get a must override `get_config` error when trying to model.save in Keras
Freetype2 support not available on PIL when trying to blockdiag on CentOS6.5
Checkpoint when you get hooked on LINE parrot return bot on Heroku
A memorandum when I tried to get it automatically with selenium
A note I was addicted to when making a beep on Linux
I don't know how to get query parameters on GAE / P
I tried to get started with Bitcoin Systre on the weekend
Articles to see if rejected when git push heroku master on Heroku
What to do when you get "I can't see the site !!!!"
Steps to install MySQL 8.0 on CentOS 8.1
I get an ImportError on appengine_config.py
How to run matplotlib on heroku
UnicodeEncodeError when trying to run radon
Misunderstanding on how to connect cnn
Connect to MySQL using Flask SQLAlchemy
When I import TensorFlow to Python, I get "Import Error: DLL load failed: The specified module cannot be found."
I want to get angry with my mom when my memory is tight
A story I was addicted to trying to install LightFM on Amazon Linux
A story I was addicted to trying to get a video url with tweepy
When I try to upgrade pip, I get an infinite loop after failing to upgrade
Points that I often get hooked on writing as a MySQL beginner
A memorandum because I stumbled on trying to use MeCab in Python
Get exchange rates on Heroku regularly and upload logs to Amazon S3
I get a firewall warning when I start gqlgen's server.go on my Mac
What to do if you get an error when installing Dlib (Ubuntu)
What to do if you get an error when trying to send a message in tasks.loop () immediately after startup
I get an error ~ is zero, singular U when passing the covariance matrix from the Linear layer to MultivariateNormal
I get an error with import pandas.
Connect to Docker's MySQL container from Flask
Error resolution when installing numba on macOS
What I referred to when studying tkinter