實(shí)驗(yàn)背景
一般我們都是在x86的機(jī)器上涮因,運(yùn)行MySQL呵俏,ARM服務(wù)器由于目前的生態(tài)不如intel畔师,所以用的不廣泛薄霜。
因?yàn)锳RM架構(gòu)的處理器在功耗上是一個(gè)很大的天然優(yōu)勢(shì)违诗,所以仍有許多人在攻堅(jiān)ARM生態(tài)漱凝。
對(duì)MySQL5.7.x,官方?jīng)]有提供編譯好的基于ARM架構(gòu)的包诸迟,所以如果需要在ARM服務(wù)器上使用MySQL5.7.x茸炒,需要自己使用官方源碼包進(jìn)行編譯。
現(xiàn)在手頭上沒(méi)有ARM虛擬機(jī)亮蒋,所以下面用x86架構(gòu)的CentOS7.5模擬扣典,在ARM機(jī)器上的實(shí)現(xiàn)思路是一樣的!
一慎玖、實(shí)驗(yàn)環(huán)境
虛擬機(jī)操作系統(tǒng): CentOS7.5
docker版本:18.06.0-ce
二贮尖、創(chuàng)建工作目錄,下載鏡像制作所需軟件包
# 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?
# 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"]
#####################################################################################
# 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數(shù)據(jù)目錄為空湿硝、允許以非安全模式(root@'localhost'為空密碼)初始化的前提下薪前,可以將邏輯設(shè)計(jì)的更嚴(yán)謹(jǐn)一點(diǎn):
1. 如果MYSQL_ROOT_PASSWORD為空值,直接以非安全模式(root空密碼)初始化
2. 如果MYSQL_ROOT_PASSWORD不為空值线衫,以非安全模式初始化凿可,然后根據(jù)MYSQL_ROOT_PASSWORD的傳值,設(shè)置root@'localhost'的密碼
# cat docker-entrypoint.sh
########################################################################
#!/bin/sh
set -e
umask 0077
if [ -z "$(ls -A /var/lib/mysql)" ]; then
? ? if [ -z "${MYSQL_ROOT_PASSWORD}" ]; then
? ? ? ? mysqld? --initialize-insecure --user=mysql
? ? else
? ? ? ? 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
fi
# if command starts with an option, prepend mysqld
if [ "${1:0:1}" = '-' ]; then
? ? set -- mysqld "$@"
fi
# If container is started as root user, restart as dedicated mysql user
if [ "$1" = 'mysqld' -a "$(id -u)" = '0' ]; then
? ? exec gosu mysql "$0" "$@"
fi
########################################################################
# chmod +x?? docker-entrypoint.sh
三授账、用Dockerfile文件build鏡像
#? docker build -f Dockerfile -t mysql:5.7.28 .
# docker images
四枯跑、用鏡像起容器測(cè)試
初始化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
##########################################################
# mkdir? /opt/mysqldata
#? sh? ??mysql5.7.28_test.sh
#? chmod 400 /opt/mysqldata/*.pem
# 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;"
開(kāi)啟root遠(yuǎn)程登錄
# 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;"
# docker exec -it mysql cat /etc/passwd
# docker exec -it mysql ls -l /usr/local/mysql
五、將容器服務(wù)注冊(cè)成系統(tǒng)服務(wù)
# 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白热,如果編譯時(shí)沒(méi)指定socket文件路徑敛助,默認(rèn)為?/tmp/mysql.sock,所以 my.cnf 中要么不寫使用默認(rèn)值棘捣,要寫就寫正確辜腺!
# 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
#####################################################
# systemctl? daemon-reload
# systemctl ?start mysqld.service
# systemctl? enable?mysqld.service
# systemctl ?status?mysqld.service
# docker exec -it mysql ps aux
# docker exec -it mysql env
六、參考
MySQL version乍恐、release、distribution 三者區(qū)別
https://www.cnblogs.com/hellotracy/articles/5063983.html
MySQL Source-Configuration Options
https://dev.mysql.com/doc/refman/8.0/en/source-configuration-options.html
Mysql 5.6的 Dockerfile 分析
https://www.cnblogs.com/ivictor/p/4832832.html
MySQL 鏡像相關(guān)環(huán)境變量的使用
https://cloud.tencent.com/developer/article/1439653
Docker容器與鏡像管理
https://www.cnblogs.com/breezey/p/8812039.html
Dockerfile reference
https://docs.docker.com/engine/reference/builder
使用 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 指令詳解
http://www.reibang.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 區(qū)別
https://blog.csdn.net/u010900754/article/details/78526443
Dockerfile 中 ENTRYPOINT 與 CMD 區(qū)別
https://blog.csdn.net/Gekkoou/article/details/80869301
Dockerfile中的RUN测砂、CMD 和 ENTRYPOINT指令的兩種格式(shell與exec)
https://blog.csdn.net/lilygg/article/details/88647718
使用docker run的選項(xiàng)以覆蓋Dockerfile中的設(shè)置詳解
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應(yīng)用示例、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 實(shí)例
https://github.com/docker-library
Docker —— 從入門到實(shí)踐
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分析
http://www.reibang.com/p/e493559e4d22
mysql docker-entrypoint.sh分析
https://segmentfault.com/a/1190000019608671
https://c.lanmit.com/bianchengkaifa/go/2265.html
Docker 環(huán)境變量的運(yùn)用
https://ryanc.cc/archives/docker-env
如何在ENTRYPOINT數(shù)組中使用Docker環(huán)境變量呜投?
https://cloud.tencent.com/developer/ask/102167
docker容器中的環(huán)境變量
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服務(wù)器上源碼編譯安裝MySQL5.7.21
https://blog.csdn.net/smart9527_zc/article/details/81153053
Linux ARM機(jī)器上源碼安裝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 服務(wù)器的編譯
https://www.huaweicloud.com/kunpeng/software/mysql.html