Loading

通过docker-compose快速构建mysql

1. 目录结构:


|─docker-compose.yml
└─mysql
   |─Dockerfile
   └─my.cnf 

2. docker-compose.yml

这里通过mysql文件下的Dockerfile进行构建,方便配置

version: '3'
services:
  mysql:
    build: ./mysql/
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - "./mysql/data:/var/lib/mysql"
  • MYSQL_ROOT_PASSWORD:指定root账号的密码
  • volumes:指定数据挂在盘,将docker中的数据映射到本地磁盘,方便docker删除后数据恢复

3. Dockerfile

使用dockerfile的主要目的是为了便于快速配置,将当前mysql目录中的my.cnf配置文件拷贝到进行中去

from mysql:8
COPY ./my.cnf /etc/mysql/

4. my.cnf

使用的默认配置,如果需要其他配置可以自行添加进去

# Copyright (c) 2017, 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
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/

在根目录启动mysql

F:\docker\mysql>docker-compose up -d
Creating mysql_mysql_1 ... done
posted @ 2020-12-14 10:52  IT搬瓦工  阅读(351)  评论(0编辑  收藏  举报