Docker Compose - MySQL
docker-compose.yml:
version: '2' services: db: image: mysql:latest volumes: - "./.mysql-data/db:/var/lib/mysql" restart: always ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: rootpass MYSQL_DATABASE: khong MYSQL_USER: khong MYSQL_PASSWORD: khongpass
To start the container run this command:
$ docker-compose up -d Creating network "mysql_default" with the default driver Creating mysql_db_1 ... done $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f349f6de0186 mysql:latest "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql_db_1
Let's enter into the container:
$ docker exec -it mysql_db_1 bash root@f349f6de0186:/#
Then, log on to MySQL db:
root@f349f6de0186:/# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.21 MySQL Community Server - GPL ... mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | khong | | mysql | | performance_schema | | sys | | testdb | +--------------------+ 6 rows in set (0.01 sec) mysql> create database mydb; Query OK, 1 row affected (0.02 sec) mysql> use mydb; Database changed mysql> show tables; Empty set (0.00 sec) mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); Query OK, 0 rows affected (0.04 sec) mysql> show tables; +----------------+ | Tables_in_mydb | +----------------+ | pet | +----------------+ 1 row in set (0.00 sec) mysql> describe pet; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | owner | varchar(20) | YES | | NULL | | | species | varchar(20) | YES | | NULL | | | sex | char(1) | YES | | NULL | | | birth | date | YES | | NULL | | | death | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 6 rows in set (0.01 sec) mysql> select * from pet; +--------+--------+---------+------+------------+-------+ | name | owner | species | sex | birth | death | +--------+--------+---------+------+------------+-------+ | Fluffy | Harold | cat | f | 1993-02-04 | NULL | | Claws | Gwen | cat | m | 1994-03-17 | NULL | | Bluffy | Harold | dog | f | 1989-05-13 | NULL | +--------+--------+---------+------+------------+-------+ 3 rows in set (0.00 sec) mysql> select count(*) from pet; +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set (0.00 sec)
Let's tear it down:
mysql> quit Bye root@f349f6de0186:/# exit exit $ docker-compose down Stopping mysql_db_1 ... done Removing mysql_db_1 ... done Removing network mysql_default
Note that even though the container is gone the database we built is still there in the "./.mysql" since we mounted the volume:
volumes: - "./.mysql-data/db:/var/lib/mysql"
First, create two tables - members and committees:
CREATE TABLE members ( member_id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (member_id) ); CREATE TABLE committees ( committee_id INT AUTO_INCREMENT, name VARCHAR(100), PRIMARY KEY (committee_id) );
mysql> show tables; +-----------------+ | Tables_in_khong | +-----------------+ | committees | | members | +-----------------+ 2 rows in set (0.01 sec)
Insert some rows into the tables members and committees:
INSERT INTO members(name) VALUES('John'),('Jane'),('Mary'),('David'),('Amelia'); INSERT INTO committees(name) VALUES('John'),('Mary'),('Amelia'),('Joe');
mysql> select * from members; +-----------+--------+ | member_id | name | +-----------+--------+ | 1 | John | | 2 | Jane | | 3 | Mary | | 4 | David | | 5 | Amelia | +-----------+--------+ 5 rows in set (0.00 sec) mysql> select * from committees; +--------------+--------+ | committee_id | name | +--------------+--------+ | 1 | John | | 2 | Mary | | 3 | Amelia | | 4 | Joe | +--------------+--------+ 4 rows in set (0.00 sec)
The inner join clause joins two tables based on a condition which is known as a join predicate.
The inner join clause compares each row from the first table with every row from the second table. If values in both rows cause the join condition evaluates to true, the inner join clause creates a new row whose column contains all columns of the two rows from both tables and include this new row in the final result set.
In other words, the inner join clause includes only rows whose values match.
The basic syntax of the inner join clause that joins two tables table_1 and table_2:
SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
The following statement finds members who are also the committee members:
mysql> SELECT * FROM members -> INNER JOIN committees -> ON members.name = committees.name; +-----------+--------+--------------+--------+ | member_id | name | committee_id | name | +-----------+--------+--------------+--------+ | 1 | John | 1 | John | | 3 | Mary | 2 | Mary | | 5 | Amelia | 3 | Amelia | +-----------+--------+--------------+--------+ 3 rows in set (0.01 sec)
Q. How can we list all columns for a given table? - SHOW COLUMNS FROM table;
:
mysql> SHOW COLUMNS FROM members; +-----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+----------------+ | member_id | int | NO | PRI | NULL | auto_increment | | name | varchar(100) | YES | | NULL | | +-----------+--------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec)
The short cut is DESCRIBE table
:
mysql> DESCRIBE members; +-----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+----------------+ | member_id | int | NO | PRI | NULL | auto_increment | | name | varchar(100) | YES | | NULL | | +-----------+--------------+------+-----+---------+----------------+ 2 rows in set (0.01 sec)
The foreign key constraint is used to make a relationship between two tables in the MySQL database. To define any foreign key constraint for any table we have to use the primary key of another table. The primary key is a unique key for a table to identify the particular row in the table and when this primary key is used into another table to make one-to-one or one-to-many or many-to-many relationship then it is called a foreign key.
Before creating a foreign key constraint, we have to create a parent table with the primary key. Suppose our database contains two parent tables named 'books' and 'borrower':
CREATE TABLE books ( id INT NOT NULL AUTO_INCREMENT, title varchar(50) NOT NULL, author varchar(50) NOT NULL, publisher varchar(50) NOT NULL, PRIMARY KEY (id) ) ; CREATE TABLE borrowers ( id VARCHAR(50) NOT NULL, name varchar(50) NOT NULL, address varchar(50) NOT NULL, email varchar(50) NOT NULL, PRIMARY KEY (id) ) ;
mysql> DESCRIBE books; +-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | title | varchar(50) | NO | | NULL | | | author | varchar(50) | NO | | NULL | | | publisher | varchar(50) | NO | | NULL | | +-----------+-------------+------+-----+---------+----------------+ 4 rows in set (0.03 sec) mysql> DESCRIBE borrowers; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | id | varchar(50) | NO | PRI | NULL | | | name | varchar(50) | NO | | NULL | | | address | varchar(50) | NO | | NULL | | | email | varchar(50) | NO | | NULL | | +---------+-------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
Create a table named 'book_borrow_info' with foreign key constraints by executing the following statement. Here, the book_id field is a foreign key for this table and every value of this field must exist in the id field of books table. books is the parent table and book_borrow_info is the child table. Two restrictions are also set with the foreign key here. These are DELETE CASCADE and UPDATE CASCADE. That means if any primary key will remove or update from the parent table then the corresponding records related to child table related to the foreign key will be removed or the foreign key will be updated.
CREATE TABLE book_borrow_info ( borrow_id VARCHAR(50), book_id INT, borrow_date DATE NOT NULL, return_date DATE NOT NULL, status VARCHAR(15) NOT NULL, INDEX par_ind (book_id), PRIMARY KEY (borrow_id, borrow_date), FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE CASCADE ON UPDATE CASCADE );
Let's insert some records in both tables:
INSERT INTO books VALUES (NULL, 'To Kill a Mockingbird', 'Harper Lee', 'Grand Central Publishing'), (NULL, 'One Hundred Years of Solitude', 'Garcia Marquez', 'Lutfi Ozkok'), (NULL, 'A Passage to India', 'Forster, E.M.', 'BBC Hulton Picture Library'), (NULL, 'Invisible Man', 'Ralph Ellison', 'Encyclopædia Britannica, Inc.'); INSERT INTO book_borrow_info VALUES ('123490', 1, '2021-02-15', '2021-02-25', 'Returned'), ('157643', 2, '2021-03-31', '2021-03-10', 'Pending'), ('174562', 4, '2021-04-04', '2021-04-24', 'Borrowed'), ('146788', 3, '2021-04-10', '2021-01-20', 'Borrowed');
mysql> SELECT * from books; +----+-------------------------------+----------------+------------------------------+ | id | title | author | publisher | +----+-------------------------------+----------------+------------------------------+ | 1 | To Kill a Mockingbird | Harper Lee | Grand Central Publishing | | 2 | One Hundred Years of Solitude | Garcia Marquez | Lutfi Ozkok | | 3 | A Passage to India | Forster, E.M. | BBC Hulton Picture Library | | 4 | Invisible Man | Ralph Ellison | Encyclopdia Britannica, Inc. | +----+-------------------------------+----------------+------------------------------+ 4 rows in set (0.00 sec) mysql> SELECT * from book_borrow_info; +-----------+---------+-------------+-------------+----------+ | borrow_id | book_id | borrow_date | return_date | status | +-----------+---------+-------------+-------------+----------+ | 123490 | 1 | 2021-02-15 | 2021-02-25 | Returned | | 146788 | 3 | 2021-04-10 | 2021-01-20 | Borrowed | | 157643 | 2 | 2021-03-31 | 2021-03-10 | Pending | | 174562 | 4 | 2021-04-04 | 2021-04-24 | Borrowed | +-----------+---------+-------------+-------------+----------+ 4 rows in set (0.00 sec)
If we try to insert a value in the foreign key field of the child table that does not exist in the primary key field of the parent table then MySQL will generate an error. The following SQL statement will generate an error because the parent table, books doesn't contain any id value 10:
INSERT INTO book_borrow_info VALUES ('195684', 10, '2021-04-15', '2020-04-30', 'Returned');
mysql> INSERT INTO book_borrow_info VALUES -> ('195684', 10, '2021-04-15', '2020-04-30', 'Returned'); ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`khong`.`book_borrow_info`, CONSTRAINT `book_borrow_info_ibfk_1` FOREIGN KEY (`book_id`) REFERENCES `books` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
After executing the following DELETE statement, when the fourth record will be removed from the books table then the related records from the book_borrow_info table will be removed automatically for the foreign key constraint.
mysql> SELECT * FROM books; +----+-------------------------------+----------------+------------------------------+ | id | title | author | publisher | +----+-------------------------------+----------------+------------------------------+ | 1 | To Kill a Mockingbird | Harper Lee | Grand Central Publishing | | 2 | One Hundred Years of Solitude | Garcia Marquez | Lutfi Ozkok | | 3 | A Passage to India | Forster, E.M. | BBC Hulton Picture Library | | 4 | Invisible Man | Ralph Ellison | Encyclopdia Britannica, Inc. | +----+-------------------------------+----------------+------------------------------+ 4 rows in set (0.00 sec) mysql> SELECT * from book_borrow_info; +-----------+---------+-------------+-------------+----------+ | borrow_id | book_id | borrow_date | return_date | status | +-----------+---------+-------------+-------------+----------+ | 123490 | 1 | 2021-02-15 | 2021-02-25 | Returned | | 146788 | 3 | 2021-04-10 | 2021-01-20 | Borrowed | | 157643 | 2 | 2021-03-31 | 2021-03-10 | Pending | | 174562 | 4 | 2021-04-04 | 2021-04-24 | Borrowed | +-----------+---------+-------------+-------------+----------+ 4 rows in set (0.00 sec) mysql> DELETE FROM books where id = 4; Query OK, 1 row affected (0.03 sec) mysql> SELECT * FROM books; +----+-------------------------------+----------------+----------------------------+ | id | title | author | publisher | +----+-------------------------------+----------------+----------------------------+ | 1 | To Kill a Mockingbird | Harper Lee | Grand Central Publishing | | 2 | One Hundred Years of Solitude | Garcia Marquez | Lutfi Ozkok | | 3 | A Passage to India | Forster, E.M. | BBC Hulton Picture Library | +----+-------------------------------+----------------+----------------------------+ 3 rows in set (0.00 sec) mysql> SELECT * FROM books; +----+-------------------------------+----------------+----------------------------+ | id | title | author | publisher | +----+-------------------------------+----------------+----------------------------+ | 1 | To Kill a Mockingbird | Harper Lee | Grand Central Publishing | | 2 | One Hundred Years of Solitude | Garcia Marquez | Lutfi Ozkok | | 3 | A Passage to India | Forster, E.M. | BBC Hulton Picture Library | +----+-------------------------------+----------------+----------------------------+ 3 rows in set (0.00 sec) mysql> SELECT * FROM book_borrow_info; +-----------+---------+-------------+-------------+----------+ | borrow_id | book_id | borrow_date | return_date | status | +-----------+---------+-------------+-------------+----------+ | 123490 | 1 | 2021-02-15 | 2021-02-25 | Returned | | 146788 | 3 | 2021-04-10 | 2021-01-20 | Borrowed | | 157643 | 2 | 2021-03-31 | 2021-03-10 | Pending | +-----------+---------+-------------+-------------+----------+ 3 rows in set (0.00 sec)
After executing the DELETE statement, when the fourth record will be removed from the books table then the related records from the book_borrow_info table will be removed automatically for the foreign key constraint.
Docker & K8s
- Docker install on Amazon Linux AMI
- Docker install on EC2 Ubuntu 14.04
- Docker container vs Virtual Machine
- Docker install on Ubuntu 14.04
- Docker Hello World Application
- Nginx image - share/copy files, Dockerfile
- Working with Docker images : brief introduction
- Docker image and container via docker commands (search, pull, run, ps, restart, attach, and rm)
- More on docker run command (docker run -it, docker run --rm, etc.)
- Docker Networks - Bridge Driver Network
- Docker Persistent Storage
- File sharing between host and container (docker run -d -p -v)
- Linking containers and volume for datastore
- Dockerfile - Build Docker images automatically I - FROM, MAINTAINER, and build context
- Dockerfile - Build Docker images automatically II - revisiting FROM, MAINTAINER, build context, and caching
- Dockerfile - Build Docker images automatically III - RUN
- Dockerfile - Build Docker images automatically IV - CMD
- Dockerfile - Build Docker images automatically V - WORKDIR, ENV, ADD, and ENTRYPOINT
- Docker - Apache Tomcat
- Docker - NodeJS
- Docker - NodeJS with hostname
- Docker Compose - NodeJS with MongoDB
- Docker - Prometheus and Grafana with Docker-compose
- Docker - StatsD/Graphite/Grafana
- Docker - Deploying a Java EE JBoss/WildFly Application on AWS Elastic Beanstalk Using Docker Containers
- Docker : NodeJS with GCP Kubernetes Engine
- Docker : Jenkins Multibranch Pipeline with Jenkinsfile and Github
- Docker : Jenkins Master and Slave
- Docker - ELK : ElasticSearch, Logstash, and Kibana
- Docker - ELK 7.6 : Elasticsearch on Centos 7
- Docker - ELK 7.6 : Filebeat on Centos 7
- Docker - ELK 7.6 : Logstash on Centos 7
- Docker - ELK 7.6 : Kibana on Centos 7
- Docker - ELK 7.6 : Elastic Stack with Docker Compose
- Docker - Deploy Elastic Cloud on Kubernetes (ECK) via Elasticsearch operator on minikube
- Docker - Deploy Elastic Stack via Helm on minikube
- Docker Compose - A gentle introduction with WordPress
- Docker Compose - MySQL
- MEAN Stack app on Docker containers : micro services
- MEAN Stack app on Docker containers : micro services via docker-compose
- Docker Compose - Hashicorp's Vault and Consul Part A (install vault, unsealing, static secrets, and policies)
- Docker Compose - Hashicorp's Vault and Consul Part B (EaaS, dynamic secrets, leases, and revocation)
- Docker Compose - Hashicorp's Vault and Consul Part C (Consul)
- Docker Compose with two containers - Flask REST API service container and an Apache server container
- Docker compose : Nginx reverse proxy with multiple containers
- Docker & Kubernetes : Envoy - Getting started
- Docker & Kubernetes : Envoy - Front Proxy
- Docker & Kubernetes : Ambassador - Envoy API Gateway on Kubernetes
- Docker Packer
- Docker Cheat Sheet
- Docker Q & A #1
- Kubernetes Q & A - Part I
- Kubernetes Q & A - Part II
- Docker - Run a React app in a docker
- Docker - Run a React app in a docker II (snapshot app with nginx)
- Docker - NodeJS and MySQL app with React in a docker
- Docker - Step by Step NodeJS and MySQL app with React - I
- Installing LAMP via puppet on Docker
- Docker install via Puppet
- Nginx Docker install via Ansible
- Apache Hadoop CDH 5.8 Install with QuickStarts Docker
- Docker - Deploying Flask app to ECS
- Docker Compose - Deploying WordPress to AWS
- Docker - WordPress Deploy to ECS with Docker-Compose (ECS-CLI EC2 type)
- Docker - WordPress Deploy to ECS with Docker-Compose (ECS-CLI Fargate type)
- Docker - ECS Fargate
- Docker - AWS ECS service discovery with Flask and Redis
- Docker & Kubernetes : minikube
- Docker & Kubernetes 2 : minikube Django with Postgres - persistent volume
- Docker & Kubernetes 3 : minikube Django with Redis and Celery
- Docker & Kubernetes 4 : Django with RDS via AWS Kops
- Docker & Kubernetes : Kops on AWS
- Docker & Kubernetes : Ingress controller on AWS with Kops
- Docker & Kubernetes : HashiCorp's Vault and Consul on minikube
- Docker & Kubernetes : HashiCorp's Vault and Consul - Auto-unseal using Transit Secrets Engine
- Docker & Kubernetes : Persistent Volumes & Persistent Volumes Claims - hostPath and annotations
- Docker & Kubernetes : Persistent Volumes - Dynamic volume provisioning
- Docker & Kubernetes : DaemonSet
- Docker & Kubernetes : Secrets
- Docker & Kubernetes : kubectl command
- Docker & Kubernetes : Assign a Kubernetes Pod to a particular node in a Kubernetes cluster
- Docker & Kubernetes : Configure a Pod to Use a ConfigMap
- AWS : EKS (Elastic Container Service for Kubernetes)
- Docker & Kubernetes : Run a React app in a minikube
- Docker & Kubernetes : Minikube install on AWS EC2
- Docker & Kubernetes : Cassandra with a StatefulSet
- Docker & Kubernetes : Terraform and AWS EKS
- Docker & Kubernetes : Pods and Service definitions
- Docker & Kubernetes : Service IP and the Service Type
- Docker & Kubernetes : Kubernetes DNS with Pods and Services
- Docker & Kubernetes : Headless service and discovering pods
- Docker & Kubernetes : Scaling and Updating application
- Docker & Kubernetes : Horizontal pod autoscaler on minikubes
- Docker & Kubernetes : From a monolithic app to micro services on GCP Kubernetes
- Docker & Kubernetes : Rolling updates
- Docker & Kubernetes : Deployments to GKE (Rolling update, Canary and Blue-green deployments)
- Docker & Kubernetes : Slack Chat Bot with NodeJS on GCP Kubernetes
- Docker & Kubernetes : Continuous Delivery with Jenkins Multibranch Pipeline for Dev, Canary, and Production Environments on GCP Kubernetes
- Docker & Kubernetes : NodePort vs LoadBalancer vs Ingress
- Docker & Kubernetes : MongoDB / MongoExpress on Minikube
- Docker & Kubernetes : Load Testing with Locust on GCP Kubernetes
- Docker & Kubernetes : MongoDB with StatefulSets on GCP Kubernetes Engine
- Docker & Kubernetes : Nginx Ingress Controller on Minikube
- Docker & Kubernetes : Setting up Ingress with NGINX Controller on Minikube (Mac)
- Docker & Kubernetes : Nginx Ingress Controller for Dashboard service on Minikube
- Docker & Kubernetes : Nginx Ingress Controller on GCP Kubernetes
- Docker & Kubernetes : Kubernetes Ingress with AWS ALB Ingress Controller in EKS
- Docker & Kubernetes : Setting up a private cluster on GCP Kubernetes
- Docker & Kubernetes : Kubernetes Namespaces (default, kube-public, kube-system) and switching namespaces (kubens)
- Docker & Kubernetes : StatefulSets on minikube
- Docker & Kubernetes : RBAC
- Docker & Kubernetes Service Account, RBAC, and IAM
- Docker & Kubernetes - Kubernetes Service Account, RBAC, IAM with EKS ALB, Part 1
- Docker & Kubernetes : Helm Chart
- Docker & Kubernetes : My first Helm deploy
- Docker & Kubernetes : Readiness and Liveness Probes
- Docker & Kubernetes : Helm chart repository with Github pages
- Docker & Kubernetes : Deploying WordPress and MariaDB with Ingress to Minikube using Helm Chart
- Docker & Kubernetes : Deploying WordPress and MariaDB to AWS using Helm 2 Chart
- Docker & Kubernetes : Deploying WordPress and MariaDB to AWS using Helm 3 Chart
- Docker & Kubernetes : Helm Chart for Node/Express and MySQL with Ingress
- Docker & Kubernetes : Deploy Prometheus and Grafana using Helm and Prometheus Operator - Monitoring Kubernetes node resources out of the box
- Docker & Kubernetes : Deploy Prometheus and Grafana using kube-prometheus-stack Helm Chart
- Docker & Kubernetes : Istio (service mesh) sidecar proxy on GCP Kubernetes
- Docker & Kubernetes : Istio on EKS
- Docker & Kubernetes : Istio on Minikube with AWS EC2 for Bookinfo Application
- Docker & Kubernetes : Deploying .NET Core app to Kubernetes Engine and configuring its traffic managed by Istio (Part I)
- Docker & Kubernetes : Deploying .NET Core app to Kubernetes Engine and configuring its traffic managed by Istio (Part II - Prometheus, Grafana, pin a service, split traffic, and inject faults)
- Docker & Kubernetes : Helm Package Manager with MySQL on GCP Kubernetes Engine
- Docker & Kubernetes : Deploying Memcached on Kubernetes Engine
- Docker & Kubernetes : EKS Control Plane (API server) Metrics with Prometheus
- Docker & Kubernetes : Spinnaker on EKS with Halyard
- Docker & Kubernetes : Continuous Delivery Pipelines with Spinnaker and Kubernetes Engine
- Docker & Kubernetes : Multi-node Local Kubernetes cluster : Kubeadm-dind (docker-in-docker)
- Docker & Kubernetes : Multi-node Local Kubernetes cluster : Kubeadm-kind (k8s-in-docker)
- Docker & Kubernetes : nodeSelector, nodeAffinity, taints/tolerations, pod affinity and anti-affinity - Assigning Pods to Nodes
- Docker & Kubernetes : Jenkins-X on EKS
- Docker & Kubernetes : ArgoCD App of Apps with Heml on Kubernetes
- Docker & Kubernetes : ArgoCD on Kubernetes cluster
- Docker & Kubernetes : GitOps with ArgoCD for Continuous Delivery to Kubernetes clusters (minikube) - guestbook
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization