Skip to content

Latest commit

 

History

History
217 lines (159 loc) · 5.87 KB

mysql-with-docker-volume.md

File metadata and controls

217 lines (159 loc) · 5.87 KB

How to install MySQL and use a Docker volume on Docker

Reference

Install Docker Engine

  1. Update the apt package index:
$ sudo apt-get update
  1. Install the necessary packages to allow apt to use a repository over HTTPS:
$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common
  1. Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Verify that you now have the key with the fingerprint:

$ sudo apt-key fingerprint 0EBFCD88
    
pub   rsa4096 2017-02-22 [SCEA]
      9DC8 5822 9FC7 DD38 854A  E2D8 8D81 803C 0EBF CD88
uid           [ unknown] Docker Release (CE deb) <docker@docker.com>
sub   rsa4096 2017-02-22 [S]
  1. Set up the stable repository:
$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"
  1. Install the latest version of Docker Engine:
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

Test Docker Installation

  1. Verify that Docker Engine - Community is installed correctly by running the hello-world image:
$ sudo docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.
...
  1. Run docker image ls to list the hello-world image that you downloaded to your machine:
$ sudo docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        15 months ago       1.84kB
  1. You can check the status of all the containers using the below command:
$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                               NAMES
2eeed464cd9f        hello-world         "/hello"                 2 minutes ago       Exited (0) 2 minutes ago                                       objective_perlman
89bb80313ac4        mysql:5.7           "docker-entrypoint.s…"   15 minutes ago      Up 15 minutes              0.0.0.0:3306->3306/tcp, 33060/tcp   vldb-mysql
  1. You can stop one or more running containers:
$ sudo docker stop [container-name]

Install MySQL

Via a Simple Command

Start a MySQL instance is simple:

$ sudo docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=pw -d mysql:5.7
  • some-mysql: The name you want to assign to your container
  • pw: The password to be set for the MySQL root user
  • mysql:5.7: The tag specifying the MySQL version you want. See the manual for relevant tags.

Via docker-compose

You can use docker-compose to run a MySQL container.

  1. Download and install the desired version of docker-compose. You can check all the released versions in the GitHub:
$ sudo curl -L https://github.com/docker/compose/releases/download/1.26.0-rc3/docker-compose-Linux-x86_64 -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose
$ ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
$ sudo docker-compose --version
docker-compose version 1.26.0-rc3, build 46118bc5
  1. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment. For example, my dockers-compose.yml looks like this:
version: '3.1'

services:
  db:
    image: mysql:5.7
    container_name: vldb-mysql
    ports:
      - 3306:3306
    volumes:
      - /home/mijin/test_data:/var/lib/mysql
      - /home/mijin/mysql-conf:/etc/mysql/conf.d
    environment:
      MYSQL_ROOT_PASSWORD: "pw"
  1. Run docker-compose up. Then Compose starts and runs the MySQL app:
$ sudo docker-compose up -d
Creating vldb-mysql ... done

Use a Directory of Host System as a Volume

The mounted directory in the host system can be used as a data directory on MySQL in Docker. Add volumes in the yaml file (dockers-compose.yml):

  db:
    image: mysql:5.7
    ...
    volumes:
      - /home/mijin/test_data:/var/lib/mysql
    ...
  • /home/mijin/test_data: The SSD-mounted directory in host system
  • /var/lib/mysql: The data directory for MySQL in Docker

Add Custom my.cnf to MySQL Container

We can also map a customized my.cnf to MySQL container.

  1. Create a new my.cnf file:
$ vim /home/mijin/mysql-conf/my.cnf
...
  1. Modify dockers-compose.yml to map mysql-conf directory in host system into conf.d directory in Docker:
  db:
    image: mysql:5.7
    ...
    volumes:
      - /home/mijin/mysql-conf:/etc/mysql/conf.d
    ...
  1. Run docker-compose up:
$ sudo docker-compose up -d
Creating vldb-mysql ... done
  1. Run the below command to connect to the created container's bash:
$ sudo docker exec -it vldb-mysql bash
  1. Check the modified server variable in MySQL:
root@89bb80313ac4:/# mysql -uroot -p -e "show variables like '%log_files%'"
+---------------------------+-------+
| Variable_name             | Value |
+---------------------------+-------+
| innodb_log_files_in_group | 3     |
+---------------------------+-------+

The value of innodb_log_files_in_group was changed from 2 (default value) to 3 (the value set in mysql-conf/my.cnf).