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.
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
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
It's easier to understand by looking at the GUI, so this time it's called Sequel Pro. Use database client software.
When you start it, the connection window will open, so enter the following.
0.0.0.0
root
MYSQL_ROOT_PASSWORD
3306
(blank is OK)If you can connect as below and proceed to an empty screen, it's OK
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.
The database is now ready.
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