同福

使用Dockerfile基于5.7镜像搭建MySQL微服务环境【20210517】

介绍

介绍

大家根福哥学会了使用Dockerfile创建Docker镜像的技巧了,那么我们现在搭建服务器环境就不需要再去下载软件的源代码了,也不需要编译安装了,更加不用操心软件和操作系统的各种兼容问题了。

今天福哥带着大家来安装mysql的环境,MySQL是数据库引擎,和php、python不同之处在于MySQL是不能简单地通过k8s进行负载均衡的,而且MySQL的数据库的数据会持续更新需要持久化保存起来,这些我们在本课都可以跟福哥学到。

镜像

tag

虽然MySQL已经出了8版本,但是福哥还是选择了兼容性比较好的5.7版本。

home/topic/2021/0518/11/0e636cb7e01e62cec3c201599c5e4216.png

手动安装

福哥先在临时容器里面手动安装一遍环境,然后再整理到Dockerfile里面,这样大家会看得比较清楚一些~~

配置文件

MySQL的配置文件在/etc/mysql/mysql.conf.d/mysqld.cnf里面,我们可以在制作Dockerfile时候根据自己的情况进行自定义的配置。

/etc/mysql/mysql.conf.d/mysqld.cnf

root@mysql5:/# cat /etc/mysql/mysql.conf.d/mysqld.cnf 
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA


#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html


[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	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

福哥这边只进行了如下的一些配置调整。

server_id = 131415926

# log bin
log-bin=mysql-bin
binlog-do-db=tfapi_utf8
expire_logs_days=10
max_binlog_size=1G
binlog-format=row

# performance
max_allowed_packet=104857600

# innodb
innodb_flush_log_at_trx_commit = 2 # 0 - no writen, 1 - writen everytimes, 2 - writen per second

# sql
sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

Dockerfile

最后福哥把前面的设置命令整理到一起写成Dockerfile,这样大家就可以通过Dockerfile安装环境了。

my-extra.cnf

首先福哥建立了一个扩展的配置文件my-extra.cnf,把自定义的参数写到里面去,导入到镜像里面。

# server id
server_id = 131415926

# log bin
log-bin=mysql-bin
binlog-do-db=tfapi_utf8
expire_logs_days=10
max_binlog_size=1G
binlog-format=row

# performance
max_allowed_packet=104857600

# innodb
innodb_flush_log_at_trx_commit = 2 # 0 - no writen, 1 - writen everytimes, 2 - writen per second

# sql
sql_mode = 'NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

Dockerfile

创建Dockerfile,将my-extra.cnf导入进来,用完后再删除掉。

FROM mysql:5.7

MAINTAINER Andy Bogate
MAINTAINER tongfu@tongfu.net
MAINTAINER http://docker.tongfu.net

EXPOSE 3306

# config
ADD my-extra.cnf /tmp/my-extra.cnf
RUN cat /tmp/my-extra.cnf >> /etc/mysql/mysql.conf.d/mysqld.cnf

# clear
RUN rm -f /tmp/my-extra.cnf

创建完镜像,启动一个容器,进去里面登入mysql终端,可以看到我们在my-extra.cnf里设置的参数生效了!

home/topic/2021/0518/14/665fb590ab9492644d38037c675087c6.png

总结

今天福哥带着大家使用Dockerfile搭建了MySQL微服务环境了,可以发现使用Dockerfile方式搭建环境我们真的只需要关心我们需要关心的部分,繁琐的编译参数、依赖库、环境参数等等一系列的问题基础镜像都给我们解决好了。

下一课,福哥会带着搭建学习使用Dockerfile搭建Redis环境,敬请期待~~