docker-compose部署Mysql8.0

docker-compose 部署mysql 8.x

编写docker-compose.yaml文件

version: '3.1'
services:
  db:
    image: mysql:8.0.20
    container_name: mysql
    restart: always
    environment:
      # 设置root密码
      MYSQL_ROOT_PASSWORD: Abcd1234
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
    ports:
      - 3306:3306
    volumes:
      - ./data/mysql:/var/lib/mysql # 挂载mysql数据卷
      - ./config/my.cnf:/etc/mysql/my.cnf # 挂载mysql my.cnf配置文件

创建文件 my.cnf

如果一开始没有这个文件可以从容器内部复制出来

# 启动一个容器
docker run --name mysql -itd mysql:2.0.20
# 复制容器内部my.cnf文件到宿主机的config文件夹下
docker cp mysql:/etc/mysql/my.cnf ./config/my.cnf

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
character-set-server=utf8
collation-server=utf8_general_ci
default-authentication-plugin = mysql_native_password
lower_case_table_names = 1
#解决时差问题
default-time-zone = '+08:00'
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Custom config should go here
[client]
default-character-set=utf8
!includedir /etc/mysql/conf.d/

# 开启 binlog
log-bin=mysql-bin
# 选择 ROW 模式
binlog-format=ROW
# 配置 MySQL replaction 需要定义,不要和 canal 的 slaveId 重复
server_id=1

部署mysql

docker-compose up -d
0%