I wanted to add a database-based feature to my current linebot, and tried connecting to MySQL using Heroku, but there was a lot of stumbling block. As a result, I gave up, but I hope that it will be helpful for beginners like me, so I will summarize it just in case.
--How to use detailed queries in MySQL
Install MySQL itself with pip install mysql
.
Install the connector.
To be honest, I don't really understand the role, but as the name suggests, it seems necessary to connect to the MySQL server.
There are many types of connectors depending on the purpose and language used, but I use mysqlclient
.
This is also installed with pip install mysqlclient
.
In my case, after finishing all the preparations for MySQL described later, I pushed the code to Heroku to check the operation, but I got an MySQL import error.
There were two causes, but the first one is the description of requirements
in the pushed file.
An error occurred because I didn't list the MySQL connector here (By the way, I wasn't sure at first and listed the version of MySQL itself, but when I pushed it, an error occurred. Masu).
Second, when using MySQL on Heroku, it seems that you will use something called ClearDB, and you will need an account created from it.
I will omit the detailed registration method, but as a caveat, most of the articles about ClearDB say that after registering, copy the environment variable CLEARDB_DATABASE_URL
, change the beginning to mysql2 and set it to DATABASE_URL
. , This operation seems to be necessary only for Ruby.
Therefore, this time, I decided to connect to MySQL using the account of CLEARDB_DATABASE_URL
when ClearDB was added with heroku addons: create cleardb: ignite
.
Since it is described in CLEARDB_DATABASE_URL
in the form of mysql: // [username]: [password] @ [hostname] / [db_name]? Reconnect = true
, mysql --host = [hostname] on the terminal. You can connect by typing --user = [username] --password = [password] [db_name]
.
If I can enter MySQL safely, I will create the necessary tables etc., but a problem occurs here. I typed a query on the monitor to create a table normally, but I got the error ʻERROR 2013`. Immediately after that, when I entered the same query again, the execution itself was done although another error occurred. From this, I thought that I probably had to change the timeout system settings, but did not accept the command to change it. After a lot of research, I came to the conclusion that the root cause was that I had to migrate to change the DB settings. However, although I investigated how to migrate Flask, I couldn't understand it at all. Originally I wasn't particular about MySQL itself, so I gave up and decided to use postgresql, which is Heroku's standard DB.
~~ When I looked it up in the error message, it seemed that it could be solved by increasing the value such as connect_timeout
, but it is not a fundamental solution and it can be processed without problems if the root user enters, so ClearDB I thought there might be a peculiar problem.
So, if you take a closer look at the ClearDB site, ~~
~~ It is recommended to use MySQL Workbench, Sequel Pro for Mac OS X, Navicat, etc. when editing the database. ~~
I found a description saying ~~. These seem to be GUI tools that allow you to operate MySQL without directly entering a query, and I saw someone using MySQL Workbench in an article before, so I decided to try this. Refer to this article for detailed operation method etc., I was able to create the necessary table safely. ~~
As an aside, when I first tried to enter MySQL as a root user after practicing MySQL, I couldn't find the password no matter how much I searched for. I knew that the initial password was listed in the given file when I installed MySQL, but I couldn't find this file no matter how many steps I followed. As a result, I managed to get into MySQL at Refer to this article.
I wrote it for a long time, but in summary
It will be.
This time, I tried a new field called database, and it was a great learning experience because I needed a different perspective from writing the code that I had been doing so far. Beta base seems to be indispensable for almost all services, so I would like to take this opportunity to acquire solid knowledge and broaden my skills.
-How to use MySQL -How to use the convenient official tool MySQL Workbench and how to translate it into Japanese -What to do if you forget your root password in MySQL -How to use MySQL on Heroku
Recommended Posts