Skip to main content
 首页 » 操作系统

Docker 基于官方镜像MySQL做自定义业务扩充镜像

2022年07月18日26xxx_UU

首先从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


本文参考链接:https://www.cnblogs.com/maohuidong/p/9914799.html