Docker

【Docker&MySQL】CentOS7.x上容器部署

2019-06-26  本文已影响46人  Bogon

一、实验背景

运维经常要和数据库打交道,尤其是MySQL,以前的方式是在本机装个MySQL用于本地调试,但有了docker以后就不需要再去繁琐的安装MySQL啦(虽然安装MySQL也不是很麻烦),直接在docker中启一个MySQL容器就可以了。

二、实验环境

操作系统:CentOS7.5 Minimal

IP: 192.168.1.107

三、 安装docker,拉取MySQL镜像

关闭selinux

# setenforce 0

# sed  -i  's/^SELINUX=.*/SELINUX=permissive/g'  /etc/selinux/config

开放相关端口

#  firewall-cmd --zone=public  --add-port=3306/tcp --permanent

#  firewall-cmd --reload

安装docker

# yum -y install  yum-utils device-mapper-persistent-data lvm2

# yum-config-manager  --add-repo    https://download.docker.com/linux/centos/docker-ce.repo

# yum list docker-ce  --showduplicates | sort  -r

#  yum -y install docker-ce-18.06.0.ce

# systemctl  start docker

# systemctl  status docker

# systemctl  enable  docker

# docker version

设置镜像加速

#  curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io

# systemctl restart docker

拉取MySQL5.7.x镜像

# docker pull mysql:5.7.20

# docker images

# docker run -it --rm  mysql:5.7.20  cat /etc/issue

# docker run -it --rm mysql:5.7.20 cat /etc/passwd

四、初始化MySQL

# mkdir /opt/mysqldata

#  mkdir /opt/mysqlconfig

# vim  /opt/mysqlconfig/mysqld.cnf

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

# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; version 2 of the License.

#

# 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 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

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

# chown 999:999  /opt/mysqlconfig/mysqld.cnf

注意:/opt/mysqlconfig/mysqld.cnf如果属主属组为root,那么权限要求为644,如果属组属主uid为999(容器中mysql用户的uid),文件权限可以为644或者640,否则用镜像起MySQL容器,无法读取自定义的挂载配置文件!

# docker run -d \

  --name mysql \

  -p 3306:3306 \

  -e UMASK=0600 \

  -e UMASK_DIR=0700 \

  -e MYSQL_HISTFILE=/dev/null \

  -e MYSQL_ROOT_PASSWORD="MySQL@123" \

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

  -v /opt/mysqlconfig/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \

  mysql:5.7.20

#  docker ps -a

#  ll  /opt/mysqldata

可以看出,生成了MySQL初始化数据,我们后续会用到相关数据,如设置的初始密码。

关于MySQL配置调优,大家可以根据自己的实际应用去编辑mysqld.cnf,本文使用的容器中默认配置。

五、删掉初始化容器,改用systemd控制服务

删掉初始化容器

# docker stop mysql

# docker rm mysql

创建MySQL的systemd Unit文件

# vim /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 192.168.1.107: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/mysql/mysql.conf.d/mysqld.cnf \

  mysql:5.7.20

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 ps -a

# docker logs  mysql

六、参考

How to use this image Start a mysql server instance

https://docs.docker.com/samples/library/mysql

mysql/mysql-docker

https://github.com/mysql/mysql-docker

Centos下使用Docker部署MySQL

https://www.cnblogs.com/seethrough/p/9010522.html

Linux Centos7.x 安装部署Mysql5.7几种方式的操作手册

https://www.cnblogs.com/easonscx/p/10644346.html

用nexus3.x 官方镜像搭建docker私有镜像仓库

https://www.jianshu.com/p/86e573f18df3

基于docker部署MySQL的数据持久化问

https://www.jianshu.com/p/530d00f97cbf

MySQL查看最大连接数和修改最大连接数

https://www.cnblogs.com/aaronguo/p/8412800.html

MySQL+MGR 单主模式和多主模式的集群环境

https://www.cnblogs.com/kevingrace/p/10470226.html

上一篇下一篇

猜你喜欢

热点阅读