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

一、实验背景

运维经常要和数据库打交道,尤其是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

c74b1d2d8ad4

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

# systemctl  start docker

# systemctl  status docker

# systemctl  enable  docker

# docker version

c74b1d2d8ad4

c74b1d2d8ad4

设置镜像加速

#  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

c74b1d2d8ad4

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

c74b1d2d8ad4

四、初始化MySQL

# mkdir /opt/mysqldata

#  mkdir /opt/mysqlconfig

c74b1d2d8ad4

# 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

c74b1d2d8ad4

#  docker ps -a

#  ll  /opt/mysqldata

c74b1d2d8ad4

可以看出,生成了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

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

c74b1d2d8ad4

# systemctl  daemon-reload

# systemctl start  mysqld.service

# systemctl enable mysqld.service

# systemctl status  mysqld.service

c74b1d2d8ad4

# docker ps -a

# docker logs  mysql

c74b1d2d8ad4

c74b1d2d8ad4

六、参考

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值