[PYTHON] Connect to Docker's MySQL container from Flask

Overview

Try the Flask tutorial, MySQL is good for DB. There are people like (´ ・ ω ・ \ ) I wanted to check the table with GUI (´ ・ ω ・ \ ) You can easily set up a Docker MySQL container and connect it with Sequel Pro.

Environment confirmed operation

Premise

I'll do a Flask tutorial. The following sites have completed ** STEP0 ** to ** STEP3 **.

$ tree

├── flaskr
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── config.py
│   ├── config.pyc
│   ├── flaskr.db
│   ├── models.py
│   ├── models.pyc
│   ├── static
│   │   └── style.css
│   ├── templates
│   │   ├── layout.html
│   │   └── show_entries.html
│   ├── views.py
│   └── views.pyc
├── manage.py
└── requirements.txt
スクリーンショット 2017-07-09 2.48.02.png

Set up a MySQL container

Set the mysql root password in an environment variable called MYSQL_ROOT_PASSWORD. I will use it later. Connect the port number 3306 so that you can connect from the Mac side.

docker run -d -e MYSQL_ROOT_PASSWORD=your_root_password -p 3306:3306 mysql:5.7

Use Sequel Pro

It's easier to understand by looking at the GUI, so this time it's called Sequel Pro. Use database client software.

Sequel Pro

When you start it, the connection window will open, so enter the following.

If you can connect as below and proceed to an empty screen, it's OK

スクリーンショット 2017-07-09 3.03.43.png

Next, create the database. From the menu above ** Database **-> ** Add Database ** Enter the database name and encoding to create the database. This time I set the database name to "testdb" and the encoding to "UTF-8".

In addition, create a table. In order to run a service like the bulletin board created in the tutorial with MySQL, You need to add the table specified in your Flask application.

Now, press the + button at the bottom left of the screen to create the table. Set the name to ** entries **. If you make a mistake, the table will not be found and an error will occur.

Also, in the Structure tab,

To set. It is OK if it becomes as follows.

スクリーンショット 2017-07-09 3.13.04.png

The database is now ready.

Connect to MySQL

From here, I'll return to Flask again.

You need to install PyMySQL to connect to MySQL. Execute the following command.

$ sudo pip install PyMySQL

Then modify flaskr / config.py to use MySQL. To SQLALCHEMY_DATABASE_URI Now be mysql + pymysql: // [username]: [password] @ [host] / [DBname] Set. Also, SECRET_KEY must still be set to an appropriate character string.

flaskr/config.py


SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:[email protected]/testdb'
SECRET_KEY = 'XXXXXXXXXXXXXXXXXX'

This will run python manage.py and run the application. If you can check the posted data on Sequel Pro It's a success ヾ (゚ д ゚) people (゚ д ゚) ノ

Recommended Posts

Connect to Docker's MySQL container from Flask
Connect to MySQL using Flask SQLAlchemy
Connect to mysql
Connect python to mysql
Connect to sqlite from python
Connect to utf8mb4 database from python
Ssh connect to GCP from Windows
Connect to Packetix VPN from CentOS 7 minimal.
Connect to MySQL with Python within Docker
Connecting from python to MySQL on CentOS 6.4
Flask tutorial (from installation to hello world)
Connect to coincheck's Websocket API from Python
I want to connect to PostgreSQL from various languages
Sum from 1 to 10
Connect to Supercomputer ITO Jupyter from client browser
Problems connecting to MySQL from Docker environment (Debian)
Connect to MySQL with Python on Raspberry Pi
Use Tor to connect from urllib2 [Python] [Mac]
[AWS] Migrate data from DynamoDB to Aurora MySQL
How to change static directory from default in Flask
Automatic conversion from MySQL Workbench mwb file to sql file
Touch MySQL from Python 3
Introducing WebPay from Flask
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Transition from WSL1 to WSL2
Easy to use Flask
Use MySQL from Python
Use MySQL from Python
How to deal with the error that Docker's MySQL container fails to start on Docker Toolbox
From editing to execution
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
I get [Error 2055] when trying to connect to MySQL on Heroku
(For myself) Flask_6 (Open db from python, Mysql basic (phpMyAdmin))
From environment construction to deployment for flask + Heroku with Docker
Check when the Docker container does not connect to the Internet
Connect to centos6 on virtualbox with ssh connection from Mac
Connect to KUINS-III (Kyoto University VPN) from Linux using PPTP
Connect to KUINS-III (Kyoto University VPN) from Linux using IKEv2
Until API made with Flask + MySQL is converted to Docker