【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像

实验背景

一般我们都是在x86的机器上,运行MySQL,ARM服务器由于目前的生态不如intel,所以用的不广泛。

因为ARM架构的处理器在功耗上是一个很大的天然优势,所以仍有许多人在攻坚ARM生态。

对MySQL5.7.x,官方没有提供编译好的基于ARM架构的包,所以如果需要在ARM服务器上使用MySQL5.7.x,需要自己使用官方源码包进行编译。

现在手头上没有ARM虚拟机,所以下面用x86架构的CentOS7.5模拟,在ARM机器上的实现思路是一样的!

 

一、实验环境

 

虚拟机操作系统: CentOS7.5

docker版本:18.06.0-ce

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第1张图片

 

 

二、创建工作目录,下载镜像制作所需软件包

 

# mkdir   /root/mysql5.7.28-docker

# cd   /root/mysql5.7.28-docker

# wget -O   /root/mysql5.7.28-docker/gosu     https://github.com/tianon/gosu/releases/download/1.7/gosu-amd64

# chmod  +x   /root/mysql5.7.28-docker/gosu

#  wget   http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

#  wget   https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.28.tar.gz 

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第2张图片

 

# cat  Dockerfile

############################################################################

FROM centos:centos7.5.1804

COPY mysql-5.7.28.tar.gz /opt

COPY boost_1_59_0.tar.gz /opt

COPY docker-entrypoint.sh  /

COPY gosu /usr/local/bin/

RUN yum clean all && \

    yum -y install gcc gcc-c++ ncurses ncurses-devel make cmake bison bison-devel openssl openssl-devel pwgen zlib zlib-devel && \

    cd /opt && \

    tar -zxf boost_1_59_0.tar.gz  && \

    mv boost_1_59_0 /usr/local/ && \

    groupadd -g  2020 mysql && \

    useradd -r -u 2020 -g  mysql -s /sbin/nologin mysql && \

    mkdir -p /var/lib/mysql && \

    tar -zxf mysql-5.7.28.tar.gz && \

    cd mysql-5.7.28 && \

    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

          -DMYSQL_DATADIR=/var/lib/mysql \

          -DSYSCONFDIR=/etc \

          -DMYSQL_USER=mysql \

          -DWITH_MYISAM_STORAGE_ENGINE=1 \

          -DWITH_INNOBASE_STORAGE_ENGINE=1 \

          -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

          -DWITH_MEMORY_STORAGE_ENGINE=1 \

          -DWITH_READLINE=1 \

          -DMYSQL_TCP_PORT=3306 \

          -DENABLED_LOCAL_INFILE=1 \

          -DENABLE_DOWNLOADS=1 \

          -DWITH_PARTITION_STORAGE_ENGINE=1 \

          -DEXTRA_CHARSETS=all \

          -DDEFAULT_CHARSET=utf8 \

          -DDEFAULT_COLLATION=utf8_general_ci \

          -DWITH_DEBUG=0 \

          -DMYSQL_MAINTAINER_MODE=0 \

          -DWITH_BOOST=/usr/local/boost_1_59_0 && \

    make -j `grep processor /proc/cpuinfo | wc -l` && \

    make install && \

    chown -R mysql:mysql /usr/local/mysql /var/lib/mysql && \

    yum -y remove gcc gcc-c++ ncurses-devel make cmake bison-devel openssl-devel zlib-devel && \

    yum clean all && \

    rm -rvf  /opt/* /usr/local/boost_1_59_0  /var/cache/yum/*

VOLUME /var/lib/mysql

ENV  PATH=/usr/local/mysql/bin:$PATH

ENTRYPOINT ["/docker-entrypoint.sh"]

EXPOSE 3306 33060

CMD ["mysqld"]

#####################################################################################

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第3张图片

 

# cat docker-entrypoint.sh

###################################################

#!/bin/sh

set -e

umask 0077

if [ -z "$(ls -A /var/lib/mysql)" ]; then

    mysqld  --initialize-insecure --user=mysql

    cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

    chmod +x /etc/init.d/mysqld

    /etc/init.d/mysqld start

    mysql -u root --skip-password -e "alter user root@'localhost' identified by \"${MYSQL_ROOT_PASSWORD}\";"

    mysql -u root  -p"${MYSQL_ROOT_PASSWORD}" -e "flush privileges;"

    /etc/init.d/mysqld stop

    rm -f /etc/init.d/mysqld

fi

exec gosu mysql "$@"

####################################################

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第4张图片

 

# chmod +x   docker-entrypoint.sh

 

三、用Dockerfile文件build镜像

 

#  docker build -f Dockerfile -t mysql:5.7.28 .

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第5张图片

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第6张图片

 

# docker images

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第7张图片

 

 

四、用镜像起容器测试

 

初始化MySQL

 

# cat  mysql5.7.28_test.sh  

#########################################################

#!/bin/bash

mysql_ver="5.7.28"

mysql_datadir="/opt/mysqldata"

mysql_root_pwd="MySQL@123"

registry_addr=""

docker run -itd  \

  --name mysql \

  -p 3306:3306 \

  -e MYSQL_HISTFILE=/dev/null \

  -v ${mysql_datadir}:/var/lib/mysql \

  -e MYSQL_ROOT_PASSWORD="${mysql_root_pwd}" \

  mysql:${mysql_ver}

sleep 30

if ss -tan | grep -w "3306" > /dev/null 2>&1; then

  echo "Mysql init successfuly!"

else

  echo "Mysql init failed!"

fi

##########################################################

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第8张图片

# mkdir  /opt/mysqldata

#  sh    mysql5.7.28_test.sh

#  chmod 400 /opt/mysqldata/*.pem

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第9张图片

# docker exec -it mysql mysql -u root -p"MySQL@123"

# docker exec -it mysql mysql -u root -p"MySQL@123" -e "select user,host from mysql.user;"

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第10张图片

 

开启root远程登录

# docker exec -it mysql mysql -u root -p"MySQL@123" -e "grant all privileges on *.* to root@'%' identified by 'MySQL@123' with grant option;"

 # docker exec -it mysql mysql -u root -p"MySQL@123" -e "flush privileges;"

# docker exec -it mysql mysql -u root -p"MySQL@123" -e "select user,host from mysql.user;"

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第11张图片

 

# docker exec -it mysql cat /etc/passwd

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第12张图片

 

 

 

五、将容器服务注册成系统服务

 

# docker stop mysql

# docker rm  mysql

 

# mkdir   /opt/mysqlconfig

# cat   /opt/mysqlconfig/mysqld.cnf

###################################################

[mysqld]

pid-file                         = /var/lib/mysql/mysqld.pid

socket                          = /tmp/mysql.sock

datadir                          = /var/lib/mysql

symbolic-links                         = 0

max_connections                   = 1000

skip_name_resolve

character-set-client-handshake  = FALSE

lower_case_table_names             = 1

sql-mode                        = "ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,STRICT_ALL_TABLES"

character-set-server            = utf8

collation-server                    = utf8_general_ci

init_connect                          = "SET NAMES 'utf8'"

 

[mysql]

default-character-set          = utf8

 

[client]

default-character-set          = utf8

 

###################################################

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第13张图片

注意: 编译的MySQL,如果编译时灭有指定socket文件路径,默认为 /tmp/mysql.sock,所以 my.cnf 中要么不写使用默认值,要写就写正确!

 

 

# chown  -R  2020:2020   /opt/mysqlconfig

 

# cat /etc/systemd/system/mysqld.service

#####################################################

[Unit]

Description=MySQL Server

After=network-online.target docker.service

Requires=docker.service

[Service]

ExecStartPre=-/usr/bin/docker rm -f mysql

ExecStart=/usr/bin/docker run \

  --name mysql \

  -p 3306:3306 \

  -e UMASK=0600 \

  -e UMASK_DIR=0700 \

  -e MYSQL_HISTFILE=/dev/null \

  -v /opt/mysqldata:/var/lib/mysql \

  -v /opt/mysqlconfig/mysqld.cnf:/etc/my.cnf \

  mysql:5.7.28

ExecStop=/usr/bin/docker stop mysql

LimitNOFILE=65535

Restart=on-failure

StartLimitBurst=3

StartLimitInterval=60s

[Install]

WantedBy=multi-user.target

#####################################################

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第14张图片

 

# systemctl  daemon-reload

# systemctl  start mysqld.service

# systemctl  enable mysqld.service

# systemctl  status mysqld.service

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第15张图片

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第16张图片

 

# docker exec -it mysql ps aux

# docker exec -it mysql env

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第17张图片

 

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第18张图片

 

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第19张图片

 

六、参考

 

Mysql 5.6的 Dockerfile 分析

https://www.cnblogs.com/ivictor/p/4832832.html

 

Docker容器与镜像管理

https://www.cnblogs.com/breezey/p/8812039.html

 

Dockerfile reference

https://docs.docker.com/engine/reference/builder

【MySQL&docker】基于CentOS7.5 编译制作MySQL5.7.28镜像_第20张图片

使用 Dockerfile 定制镜像

https://yeasy.gitbooks.io/docker_practice/image/build.html

 

docker 官方的 redis 镜像如何指定配置文件

https://segmentfault.com/q/1010000008272753

https://stackoverflow.com/questions/50790197/why-redis-in-docker-need-set-daemonize-to-no

 

What does set -e and exec “$@” do for docker entrypoint scripts?

https://stackoverflow.com/questions/39082768/what-does-set-e-and-exec-do-for-docker-entrypoint-scripts

 

编写 docker-entrypoint.sh 入口文件

https://docs.lvrui.io/2017/06/09/%E7%BC%96%E5%86%99docker-entrypoint-sh%E5%85%A5%E5%8F%A3%E6%96%87%E4%BB%B6

 

Dockerfile 指令详解

https://www.jianshu.com/p/eb9bd494105c

 

What is the purpose of [ x“$1” != x“” ]?

https://stackoverflow.com/questions/29892916/what-is-the-purpose-of-x1-x

 

CMD ENTRYPOINT 区别

https://blog.csdn.net/u010900754/article/details/78526443

 

Dockerfile 中 ENTRYPOINT 与 CMD 区别

https://blog.csdn.net/Gekkoou/article/details/80869301

 

Dockerfile中的RUN、CMD 和 ENTRYPOINT指令的两种格式(shell与exec)

https://blog.csdn.net/lilygg/article/details/88647718

 

使用docker run的选项以覆盖Dockerfile中的设置详解

https://blog.csdn.net/taiyangdao/article/details/73123939

 

docker-entrypoint.sh 文件的用处

https://blog.csdn.net/u013272009/article/details/84073136

 

10 Docker Image Security Best Practices

https://snyk.io/blog/10-docker-image-security-best-practices/

 

docker与gosu

https://blog.csdn.net/boling_cavalry/article/details/93380447

 

MySQL docker entrypoint入口文件详解

https://www.cnblogs.com/breezey/p/8812197.html

 

Docker: 精通ENTRYPOINT指令

https://blog.csdn.net/CHENYUFENG1991/article/details/78766584

 

Dockerfile定制镜像、Dockerfile应用示例、CMD 与 ENTRYPOINT

https://blog.csdn.net/weixin_41927237/article/details/81878282

 

Docker: 精通ENTRYPOINT指令

https://blog.csdn.net/CHENYUFENG1991/article/details/78766584

 

怎么理解 Dockerfile中   set  -- mysqld"$@"   命令?

https://segmentfault.com/q/1010000003874406

 

docker与gosu

https://blog.csdn.net/boling_cavalry/article/details/93380447

 

sudo or gosu

https://segmentfault.com/a/1190000004527476

 

docker-library 大量Dockerfile 实例

https://github.com/docker-library

 

Docker —— 从入门到实践

https://yeasy.gitbooks.io/docker_practice/image/dockerfile/user.html

 

10 Docker Image Security Best Practices

https://snyk.io/blog/10-docker-image-security-best-practices

 

docker-mysql5.7.27-arm

https://github.com/whutwf/docker-mysql5.7.27-arm

 

mysql 5.6 Dockerfile分析

https://www.jianshu.com/p/e493559e4d22

 

mysql docker-entrypoint.sh分析

https://segmentfault.com/a/1190000019608671

https://c.lanmit.com/bianchengkaifa/go/2265.html

 

Docker 环境变量的运用

https://ryanc.cc/archives/docker-env

 

如何在ENTRYPOINT数组中使用Docker环境变量?

https://cloud.tencent.com/developer/ask/102167

 

docker容器中的环境变量

https://xuxinkun.github.io/2019/03/12/docker-env

 

entrypoint.sh shell脚本解析

https://www.cnblogs.com/iiiiher/p/8033194.html

 

如何编写最佳的Dockerfile

https://segmentfault.com/a/1190000009514788

https://segmentfault.com/a/1190000015606305

 

docker-entrypoint.sh 文件的用处

https://blog.csdn.net/u013272009/article/details/84073136

 

 

ARM服务器上源码编译安装MySQL5.7.21

https://blog.csdn.net/smart9527_zc/article/details/81153053

 

Linux ARM机器上源码安装MySQL5.7.23

https://blog.csdn.net/qd1308504206/article/details/99686650

 

ARM CentOS7 上安装MysQL 8.0.15教程

https://blog.csdn.net/fengshaungme/article/details/88746201

 

MySQL5.7.x的交叉编译

https://github.com/velarn/CrossCompliteArmMysql

 

MySQL 5.6 基于 ARM 服务器的编译

https://www.huaweicloud.com/kunpeng/software/mysql.html

你可能感兴趣的:(docker,MySQL,ARM)