基於官方映象MySQL做自定義業務擴充映象

wadeson發表於2017-11-09

首先從https://hub.docker.com/_/mysql/拉取官方映象,如果速度緩慢,建議新增國內加速

[root@docker ~]# docker images
REPOSITORY                                         TAG                 IMAGE ID            CREATED             SIZE
mysql                                              latest              5709795eeffa        4 days ago          408MB

檢視如何使用mysql映象啟動一個container:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

-e:設定環境變數

  MYSQL_ROOT_PASSWORD這個變數是強制性的,需要指定密碼,如果沒有設定此變數的值,那麼建立container不會成功

  MYSQL_USER:為MySQL服務建立一個使用者(可選變數)

  MYSQL_PASSWORD:為建立的使用者給定密碼(可選變數,與上面變數連用)

  MYSQL_ALLOW_EMPTY_PASSWORD:指定允許mysql使用空密碼(針對的是MySQL服務的root使用者的密碼)

  MYSQL_DATABASE:為MySQL中建立庫,指定庫名稱

更多其他變數檢視:https://hub.docker.com/_/mysql/

 

[root@docker ~]# docker run --name=mysql -e MYSQL_ROOT_PASSWORD=redhat -d mysql

[root@docker ~]# docker exec -it mysql /bin/bash
root@c240bb45b889:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 MySQL Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

指定可以使用空密碼,然後不設定root密碼:

[root@docker ~]# docker run -d --name=mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql
9c9b61491d9c5f035a32c3ccbe8832ac025676051cc6513159d7bd7cec327d6e
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
9c9b61491d9c        mysql               "docker-entrypoint..."   3 seconds ago       Up 2 seconds        3306/tcp            mysql

然後登入到mysql中:

[root@docker ~]# docker exec -it mysql /bin/bash
root@9c9b61491d9c:/# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

對映埠到本機host,然後進行-h host進行登入:

[root@docker ~]# docker run -d --name=mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=redhat mysql
19c8d047c38d445ca114433ef13f56921106b582e563d5763da61c5c8f48e898
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
19c8d047c38d        mysql               "docker-entrypoint..."   4 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp   mysql

使用mysql -uroot -p -h 192.168.101.14進行登入:

[root@docker ~]# docker exec -it mysql /bin/bash
root@19c8d047c38d:/# mysql -uroot -p -h192.168.101.14
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

啟動容器時,掛載對映資料庫資料目錄:

[root@docker ~]# docker run -d --name=mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=redhat -v /data/mysql:/var/lib/mysql mysql
a6f5ff4550fb13ae767cc1e1e37e344c33f90592dc19b2d2ddc80c5e6b842bf1
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a6f5ff4550fb        mysql               "docker-entrypoint..."   4 seconds ago       Up 2 seconds        0.0.0.0:3306->3306/tcp   mysql

檢視對映到本機host的資料目錄:

[root@docker ~]# ll /data/mysql/
total 188484
-rw-r-----. 1 systemd-bus-proxy input       56 Nov  9 14:58 auto.cnf
-rw-------. 1 systemd-bus-proxy input     1679 Nov  9 14:58 ca-key.pem
-rw-r--r--. 1 systemd-bus-proxy input     1107 Nov  9 14:58 ca.pem
-rw-r--r--. 1 systemd-bus-proxy input     1107 Nov  9 14:58 client-cert.pem
-rw-------. 1 systemd-bus-proxy input     1675 Nov  9 14:58 client-key.pem
-rw-r-----. 1 systemd-bus-proxy input     1321 Nov  9 14:58 ib_buffer_pool
-rw-r-----. 1 systemd-bus-proxy input 79691776 Nov  9 14:58 ibdata1
-rw-r-----. 1 systemd-bus-proxy input 50331648 Nov  9 14:58 ib_logfile0
-rw-r-----. 1 systemd-bus-proxy input 50331648 Nov  9 14:58 ib_logfile1
-rw-r-----. 1 systemd-bus-proxy input 12582912 Nov  9 14:58 ibtmp1
drwxr-x---. 2 systemd-bus-proxy input     4096 Nov  9 14:58 mysql
drwxr-x---. 2 systemd-bus-proxy input     8192 Nov  9 14:58 performance_schema
-rw-------. 1 systemd-bus-proxy input     1679 Nov  9 14:58 private_key.pem
-rw-r--r--. 1 systemd-bus-proxy input      451 Nov  9 14:58 public_key.pem
-rw-r--r--. 1 systemd-bus-proxy input     1107 Nov  9 14:58 server-cert.pem
-rw-------. 1 systemd-bus-proxy input     1679 Nov  9 14:58 server-key.pem
drwxr-x---. 2 systemd-bus-proxy input     8192 Nov  9 14:58 sys

當啟動容器時,給MySQL服務建立一個資料庫wordpress,建立一個使用者wordpress,並指定密碼wordpress:

[root@docker ~]# docker run -d --name=mysql -p3306:3306 -e MYSQL_ROOT_PASSWORD=redhat -v /data/mysql:/var/lib/mysql -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpress mysql
4ec8d2d23af884a90aed4e8f5ffd81a7beaa7a3e65065dfdaba49056880d985a
[root@docker ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
4ec8d2d23af8        mysql               "docker-entrypoint..."   4 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp   mysql

驗證建立的結果:

root@b5f97b989c51:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql> select user from mysql.user;
+-----------+
| user      |
+-----------+
| root      |
| wordpress |
| mysql.sys |
| root      |
+-----------+
4 rows in set (0.00 sec)

mysql> 

當這次建立的時候,必須先將上次建立的資料目錄進行刪除掉,因為如果不刪除的話,還是上次建立container時的那次的資料

 

根據上面的一些操作可以明顯的瞭解到,利用官方映象啟動容器然後執行sql必須麻煩,所以為了需求需要將映象進行自定義擴容,能夠在官方映象上執行sql等其他需求

[root@docker mysql]# tree
.
├── business.sql
├── Dockerfile
├── mysqld.cnf
└── setup.sh

檢視Dockerfile檔案:

[root@docker mysql]# cat Dockerfile 
FROM mysql

MAINTAINER json_hc@163.com

ENV MYSQL_ALLOW_EMPTY_PASSWORD yes

COPY mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
COPY business.sql /root/business.sql
COPY setup.sh /root/setup.sh

RUN chmod +x /root/setup.sh
EXPOSE 3306

ENTRYPOINT ["/root/setup.sh"]

Dockerfile中基於官方MySQL映象,目前是MySQL5.7的版本,所以MySQL軟體不需要進行安裝,根據上一篇自定義構建MySQL的原理,可以知道在MySQL服務安裝好的情況下

編寫shell指令碼重新將MySQL服務進行初始化,然後在啟動和關閉資料庫服務起降執行所需要的sql,最後前臺執行MySQL程式就行

在最開始有一個ENV MYSQL_ALLOW_EMPTY_PASSWORD yes環境變數的設定,這是由於官方的MySQL映象必須進行二選一進行root使用者的密碼要求

於是Dockerfile就是將需要執行的sql和shell指令碼以及配置檔案進行了copy

檢視setup.sh這個shell指令碼:

[root@docker mysql]# cat setup.sh 
#!/bin/sh
chown -R mysql:mysql /var/lib/mysql

#mysql_install_db --user=mysql --datadir=/var/lib/mysql > /dev/null
mysqld --initialize-insecure --user=mysql > /dev/null

mysqld --user=mysql &

sleep 5

mysql < /root/business.sql

sleep 5

ps -wef | grep mysql | grep -v grep | awk '{print $2}' | xargs kill -9

mysqld --user=mysql

由於MySQL5.6和MySQL5.7初始化的過程不一樣,預設情況下MySQL5.7初始化會生成一個root使用者的隨機密碼,可以使用--initialize-insecure進行初始化使root使用者

的密碼為空,達到初始化的目的

檢視business.sql

[root@docker mysql]# cat business.sql 
grant all privileges on *.* to 'root'@'localhost' identified by 'root';

create database wordpress DEFAULT CHARACTER SET utf8;

USE mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

UPDATE user SET password=PASSWORD("root") WHERE user='root';
FLUSH PRIVILEGES;

檢視配置檔案

[root@docker mysql]# cat mysqld.cnf 
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error      = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address   = 0.0.0.0
COPY mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf這是官方配置檔案的所在地
最後進行構建:
[root@docker mysql]# docker build -t mysql:v1 .
[root@docker mysql]# docker run -d --name=mysql -p3306:3306 -v /data/mysql:/var/lib/mysql mysql:v1
9a7509d3f48eba1a67ec690db652c5b73cbcc88c96ff871701c0ef7becc3ecc4
[root@docker mysql]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                    NAMES
9a7509d3f48e        mysql:v1            "/root/setup.sh"    3 seconds ago       Up 2 seconds        0.0.0.0:3306->3306/tcp   mysql

驗證business.sql的內容:

[root@docker mysql]# docker exec -it mysql /bin/bash
root@9a7509d3f48e:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 MySQL Community Server (GPL)

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

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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

mysql> 

對映並掛載到本機host的資料目錄:(掛載之前刪掉/data/mysql,避免遺留的是上次掛載的資料)

[root@docker ~]# ll /data/mysql/
total 122916
-rw-r-----. 1 systemd-bus-proxy input       56 Nov  9 15:22 auto.cnf
-rw-r-----. 1 systemd-bus-proxy input      419 Nov  9 15:22 ib_buffer_pool
-rw-r-----. 1 systemd-bus-proxy input 12582912 Nov  9 15:22 ibdata1
-rw-r-----. 1 systemd-bus-proxy input 50331648 Nov  9 15:22 ib_logfile0
-rw-r-----. 1 systemd-bus-proxy input 50331648 Nov  9 15:22 ib_logfile1
-rw-r-----. 1 systemd-bus-proxy input 12582912 Nov  9 15:22 ibtmp1
drwxr-x---. 2 systemd-bus-proxy input     4096 Nov  9 15:22 mysql
drwxr-x---. 2 systemd-bus-proxy input     8192 Nov  9 15:22 performance_schema
drwxr-x---. 2 systemd-bus-proxy input     8192 Nov  9 15:22 sys
drwxr-x---. 2 systemd-bus-proxy input       20 Nov  9 15:22 wordpress

配置檔案和sql語句都可以根據業務進行替換,然後在MySQL映象的基礎上進行構建

該構建專案在GitHub:https://github.com/jsonhc/docker_project/tree/master/docker_dockerfile/mysql

 

相關文章