Create a MySQL environment with Docker from 0-> 1

What to do in this article

Create a MySQL environment with Docker Create Database Make a table Enter data

environment

Docker mysql 5.7

Create a MySQL environment with Docker

Create a parent folder for this article

$ mkdir mysql-sample

And make docker-compose.yml

docker-compose.yml


version: "3"
services:
  mysql:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: mysql
    volumes:
      - "./mysql/db-data/:/var/lib/mysql" #Data persistence
      - "./mysql/my.cnf:/etc/mysql/conf.d/my.cnf" #Necessary to use Japanese as data

Create a mysql folder as a place to store MySQL related data.

And write the MySQL configuration file in my.cnf.

msql/my.cnf


[mysqld]
character-set-server=utf8

This allows you to enter Japanese as mysql data

mysql-sample
├── docker-compose.yml
└── mysql
    └── my.cnf

The mysql client is included in the mysql image. We will use it in this article.

docker-compose up -d

Launch the container with. Then mysql is initialized and There will be a folder called db-data under the project's mysql folder

スクリーンショット 2021-01-10 22.08.55.png

Enter the container with Attach Shell

Start mysql client with

$ mysql -u root -p

The password is mysql set in docker-compose.yml

.sql


Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

...

mysql> 

This completes until you start mysql

Create database

Show all db

.sql


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.04 sec)

mysql> 

Add a new db called sample here

.sql


mysql> create database sample;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample             |
| sys                |
+--------------------+
5 rows in set (0.02 sec)

mysql> 

Type the following command to operate this db

mysql> use sample;
Database changed

This completes the db creation

Create table

At first there is no table.

mysql> show tables;
Empty set (0.01 sec)

mysql> 

Therefore, I type the command to create a table, but since the command to type a table is long, write it first with a text editor etc.

.sql


CREATE TABLE users (
id int PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password CHAR(36) NOT NULL
);

Create a table called users.

The explanation about this query is roughly below

column Mold Explanation
id int(Numerical value) A number to identify the user. Primary key. Automatic serial number
username varchar(255) (String) username
email varchar(255) Email address, UNIQUE to avoid duplication
password CHAR(36)
(Up to 36 English characters)
password. 36 to match the UUID string

Paste the above query as it is and execute it

.sql


mysql> CREATE TABLE users (
    -> id int PRIMARY KEY AUTO_INCREMENT,
    -> username VARCHAR(255) NOT NULL,
    -> email VARCHAR(255) NOT NULL UNIQUE,
    -> password CHAR(36) NOT NULL
    -> );
Query OK, 0 rows affected (0.07 sec)

mysql> show tables;
+------------------+
| Tables_in_sample |
+------------------+
| users            |
+------------------+
1 row in set (0.01 sec)

mysql> show columns from users;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| username | varchar(255) | NO   |     | NULL    |                |
| email    | varchar(255) | NO   | UNI | NULL    |                |
| password | char(36)     | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)

mysql> 

This completes the table creation

Add data

Creating a UUID.

If you use mac, you can make it quickly with uuidgen. For powershell [Guid] :: NewGuid () => 224A68FF-FB77-4F97-94CF-8B7AD846DE05

.sql


insert into users (username, email, password) values ("Teach", "[email protected]", "uooooo");

.sql


mysql> insert into users (username, email, password) values ("Teach", "[email protected]", "uooooo");
Query OK, 1 row affected (0.01 sec)

mysql> 

Data acquisition

.sql


mysql> select * from users;
+----+----------+-------------------+----------+
| id | username | email             | password |
+----+----------+-------------------+----------+
|  1 | Teach    | [email protected] | uooooo   |
+----+----------+-------------------+----------+
1 row in set (0.00 sec)

mysql> 

Supplement

When looking at db from other Docker containers, host will be the service name in docker-compose.yml. Therefore, connect as follows

mysql -u root -h mysql -p

Recommended Posts

Create a MySQL environment with Docker from 0-> 1
Create a Vue3 environment with Docker!
[Note] Create a java environment from scratch with docker
Create Rails 6 + MySQL environment with Docker compose
[Memo] Create a CentOS 8 environment easily with Docker
Create a Spring Boot development environment with docker
Rails + MySQL environment construction with Docker
Build a Node.js environment with Docker
Create SolrCloud verification environment with Docker
Create Laravel environment with Docker (docker-compose)
[Environment construction with Docker] Rails 6 & MySQL 8
Update MySQL from 5.7 to 8.0 with Docker
I tried to create a padrino development environment with Docker
Create a web environment quickly using Docker
Build a PureScript development environment with Docker
Build a WAS execution environment from Docker
Create Spring Boot-gradle-mysql development environment with Docker
[Docker] Create Node.js + express + webpack environment with Docker
Laravel + MySQL + phpMyadmin environment construction with Docker
Build a Wordpress development environment with Docker
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Build a development environment for Django + MySQL + nginx with Docker Compose
[Docker] Connection with MySQL
Create a Privoxy + Tor environment instantly using Docker
Create a simple bulletin board with Java + MySQL
[Windows] [IntelliJ] [Java] [Tomcat] Create a Tomcat9 environment with IntelliJ
Build a Laravel / Docker environment with VSCode devcontainer
Build a WordPress development environment quickly with Docker
Rails6 [API mode] + MySQL5.7 environment construction with Docker
Prepare a scraping environment with Docker and Java
React + Django + Nginx + MySQL environment construction with Docker
Create a docker environment for Oracle 11g XE
I built a rails environment with docker and mysql, but I got stuck
Create a java web application development environment with docker for mac part2
Pytorch execution environment with Docker
[Docker] Create Elasticsearch, Kibana environment!
[First environment construction] I tried to create a Rails 6 + MySQL 8.0 + Docker environment on Windows 10.
MySQL 5.7 (Docker) environment construction memo
Easily build a Vue.js environment with Docker + Vue CLI
Create an E2E test environment with Docker x Cypress
[Note] Build a Python3 environment with Docker in EC2
Create a playground with Xcode 12
[Docker] Rails 5.2 environment construction with docker
Build docker environment with WSL
Build a development environment to create Ruby on Jets + React apps with Docker
Build Rails (API) x MySQL x Nuxt.js environment with Docker
React environment construction with Docker
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
Docker command to create Rails project with a single blow in environment without Ruby
Create a MySQL test environment (+ millions of test data) in 5 minutes
I made a development environment with rails6 + docker + postgreSQL + Materialize.
Create a Docker image with the Oracle JDK installed (yum
How to build [TypeScript + Vue + Express + MySQL] environment with Docker ~ Express ~
Easy environment construction of MySQL and Redis with Docker and Alfred
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Build a Node-RED environment with Docker to move and understand
Create Rails5 and postgresql environment with Docker and make pgadmin available
Node.js environment construction with Docker Compose
Create a lightweight STNS Docker image
Build a Tomcat 8.5 environment with Pleiades 4.8
Let's create Ubuntu environment with vmware