如何部署Docker MySQL?

Docker

1 基础知识

MySQL是一个关系型数据库管理系统
MySQL由瑞士MySQL AB公司开发,属于Oracle旗下产品。
MySQL是最流行的关系型数据库管理系统之一。
MySQL在WEB应用方面是最好的RDBMS(Relational Database Management System)应用软件之一

2 最佳实践

2.1 部署Docker集群

如果你尚未部署Docker集群环境,请参阅如下章节部署,

如何部署Docker生产环境?

2.2 部署前的准备

2.2.1 准备MySQL镜像

In docker01

cd /data/docker/images/
docker load -i mysql_5.6.34.tar
docker tag mysql:5.6.34 docker01.cmdschool.org:5000/mysql:5.6.34
docker push docker01.cmdschool.org:5000/mysql:5.6.34
docker image rm mysql:5.6.34

另外,安装包的离线下载请在能上网的docker环境的机器上使用如下命令,

docker pull mysql:5.6.34
docker save mysql:5.6.34 -o mysql_5.6.34.tar

另外,其他版本请从以下链接下载,
https://hub.docker.com/_/mysql

2.2.2 准备adminer镜像

In docker01

cd /data/docker/images/
docker load -i adminer_4.7.7.tar
docker tag adminer:4.7.7 docker01.cmdschool.org:5000/adminer:4.7.7
docker push docker01.cmdschool.org:5000/adminer:4.7.7
docker image rm adminer:4.7.7

另外,安装包的离线下载请在能上网的docker环境的机器上使用如下命令,

docker pull adminer:4.7.7
docker save adminer:4.7.7 -o adminer_4.7.7.tar

https://hub.docker.com/_/adminer

2.3 部署MySQL

In docker01

2.3.1 创建服务配置文件

vim /data/docker/yml/mysql-stack.yml

加入如下配置,

# Use root/example as user/password credentials
version: '3.1'

services:

  db:
    image: docker01.cmdschool.org:5000/mysql:5.6.34
    volumes:
      - /data/docker/service/mysql/data:/var/lib/mysql
      - /data/docker/service/mysql/conf:/etc/mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: mysqlpwd
      TZ: Asia/Shanghai
    deploy:
      replicas: 1
      placement:
        constraints: [node.labels.mysql == true]
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "5"
  adminer:
    image: docker01.cmdschool.org:5000/adminer:4.7.7
    restart: always
    environment:
      TZ: Asia/Shanghai
    ports:
      - 8080:8080
    deploy:
      replicas: 1
      placement:
        constraints: [node.labels.adminer == true]
    logging:
      driver: "json-file"
      options:
        max-size: "100m"
        max-file: "5"

根据配置需求创建数据存放目录,
In docker02

mkdir -p /data/docker/service/mysql/data
mkdir -p /data/docker/service/mysql/conf/conf.d/

根据配置需求创建MySQL主配置文件,

vim /data/docker/service/mysql/conf/my.cnf

加入如下配置,

[mysqld]
skip-host-cache
skip-name-resolve

# Copyright (c) 2015, 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 Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port            = 3306
socket          = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
nice            = 0

[mysqld]
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address   = 127.0.0.1

#log-error      = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

根据MySQL的主配置文件创建子目录配置,

vim /data/docker/service/mysql/conf/conf.d/docker.cnf

加入如下配置,

[mysqld]
skip-host-cache
skip-name-resolve

根据配置的需求设置运行标记,
In docker01

docker node update --label-add mysql=true docker02.cmdschool.org
docker node update --label-add adminer=true docker01.cmdschool.org

2.3.2 部署服务

docker stack deploy -c /data/docker/yml/mysql-stack.yml mysql

2.3.3 测试MySQL服务

In docker02

docker exec -it `docker container ls | grep 'mysql' | cut -d" " -f1 ` bash

然后,我们使用如下命令登录数据库,

mysql -uroot -pmysqlpwd

可使用如下命令查看当前的库,

show databases;

2.3.4 测试adminer服务

http://10.168.0.210:8080/
可见如下界面,

可通过之前定义的密码登录,详细如下,
adminuser:root
password:mysqlpwd

参阅文档
=====================

官方参阅
————
https://hub.docker.com/_/mysql
https://hub.docker.com/_/adminer

文件兼容性参考
————–
https://docs.docker.com/compose/compose-file/

没有评论

发表评论

Docker
如何打包Docker Nginx?

1 前言 一个问题,一篇文章,一出故事。 由于docker hub原版本的nginx镜像过于精简,于 …

Docker
如何打包Docker MAC-Telnet?

1 基础知识 一款可以使用MikroTik RouterOS MAC-Telnet协议连接Mikro …

Docker
如何部署Docker Unifi-WiFi?

1 前言 一个问题,一篇文章,一出故事。 笔者Unifi-WiFi想要迁移至docker环境,于是产 …