[PYTHON] Try to make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 4] ~ MySQL construction and DB migration with Docker ~

<< Part 3 | Part 5 >>

Create a MySQL container with Docker

Create a docker folder directly under the project, and create a db_data folder (may have a different name) and docker-compose.yml directly under it.

concentratio #Project root directory
├── config 
│   └── ...
│   
├── docker 
│   ├── db_data #For data persistence
│   └── docker-compose.yml
└── ...

docker/docker-compose.yml


version: '2'
services:
  db:
    image: mariadb:latest
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
    environment:
      - MYSQL_ROOT_USER=root
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=concentratio
      - MYSQL_USER=user
      - MYSQL_PASSWORD=user
    volumes:
      - db_data:/var/lib/mysql # db_If you have changed the name of the data folder, rewrite it with the changed folder name.
      - ./db_data:/docker-entrypoint-initdb.d # db_If you have changed the name of the data folder, rewrite it with the changed folder name.
      - ./db_data:/etc/mysql/conf.d # db_If you have changed the name of the data folder, rewrite it with the changed folder name.
    ports:
      - '3333:3306'

volumes:
  db_data:
    driver: local

Since the db_data folder is mounted in /docker-entrypoint-initdb.d, if you put an arbitrary SQL file in the db_data folder, it will be started when the container is started (first time?). It will execute the SQL file. </ font>

docker container start

Move to the docker directory with the cd command and then start the MySQL container with docker-compose up -d.

docker$ docker-compose up -d
Creating docker_db_1 ... done

By the way Stop is docker-compose stop (or docker stop container ID or container name) Delete is docker-copose down -v (or docker rm container ID or container name)

Check if the MySQL container is running

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
03dd318a2ad7        mariadb:latest      "docker-entrypoint.s…"   29 seconds ago      Up 29 seconds       0.0.0.0:3333->3306/tcp   docker_db_1

Try connecting with MySQL workbench

image.png

image.png It looks like I was able to connect

DB migration

pip install mysqlclient and django-environ

pip3 install django-environ mysqlclient

Create .env file

concentratio #Project root directory
├── config 
│   └── ...
│   
├── docker 
│   └── ...
│
├──.env
│
└── ...

.env.default


DEBUG=True
DATABASE_URL=mysql://user:[email protected]:3333/concentratio

Edit settings.py

config/settings.py


.
..
...
import environ #add to(django-Import environ)
.
..
...
#add to
ENV_FILE = os.path.join(BASE_DIR, '.env') # .env file path
ENV = environ.Env()
ENV.read_env(ENV_FILE) # django-in environ.Read env file
...
..
.
#add to
DATABASES = {
    'default': ENV.db()
}
DATABASES['default']['ATOMIC_REQUESTS'] = True # ATOMIC_When REQUESTS is set to True, the entire view becomes a transaction (if an exception occurs during view processing, the previous DB operation is rolled back).
...
..
.

DB migration

python3 manage.py makemigrations
python3 manage.py migrate

The migrate command is okay as long as various OKs are given.

$ python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

Check again with MySQL workbench

A django admin table has been created. DB migration is complete. image.png

Create superuser

You can create a superuser with python3 manage.py createsuperuser.

$ python3 manage.py createsuperuser
Username : admin #Set appropriately (this time, admin)
Email address: [email protected] #Set appropriately (admin this [email protected])
Password: #Set appropriately (this time, admin)
Password (again): #Set appropriately (this time, admin)
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
This password is too common.
Bypass password validation and create user anyway? [y/N]: y #I get a warning that the password is easy, but it doesn't matter if it's "y"
Superuser created successfully.

image.png

The user has been created. that's all.

<< Part 3

Recommended Posts

Try to make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 4] ~ MySQL construction and DB migration with Docker ~
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 3] ~ Implementation of nervous breakdown ~
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 6] ~ User Authentication 2 ~
Let's make a nervous breakdown application with Vue.js and Django-Rest-Framework [Part 5] ~ User authentication ~
Let's make a nervous breakdown app with Vue.js and Django-Rest-Framework [Part 2] ~ Vue setup ~
Let's make a nervous breakdown app with Vue.js and Django-Rest-Framework [Part 1] ~ Django setup ~
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation
Try to make a "cryptanalysis" cipher with Python
WEB scraping with python and try to make a word cloud from reviews
Try to make a dihedral group with Python
Try to make a command standby tool with python
Try to operate DB with Python and visualize with d3
How to make a shooting game with toio (Part 1)
Try to bring up a subwindow with PyQt5 and Python
[Introduction to Tensorflow] Understand Tensorflow properly and try to make a model
Let's make a WEB application for phone book with flask Part 1
Rubyist tried to make a simple API with Python + bottle + MySQL
Let's make a WEB application for phone book with flask Part 2
How to make a surveillance camera (Security Camera) with Opencv and Python
Try to make a web service-like guy with 3D markup language
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
Let's make a WEB application for phone book with flask Part 3
I tried to make a todo application using bottle with python
Let's make a WEB application for phone book with flask Part 4
Deploy a Django application with Docker
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Try to make a capture software with as high accuracy as possible with python (2)
Try to make foldl and foldr with Python: lambda. Also time measurement
How to make a container name a subdomain and make it accessible in Docker
I want to make a web application using React and Python flask