docker启动mysql

本文详细描述了如何在Docker容器中使用docker-compose.yml配置MySQL服务,包括设置环境变量、数据卷、网络配置以及初始化SQL脚本,确保UTF-8MB4字符集支持。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录结构

docker-compose.yml

version: '3.4'
services:
  mysql:
    image: mysql:5.7.35
    container_name: mysql
    hostname: mysql
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: Abc@123456
    volumes:
      #设置时区使用宿主机时区,防止时间少8小时
      - ./localtime:/etc/localtime:ro
      - ./data:/var/lib/mysql
      - ./config:/etc/mysql/conf.d
      - ./log:/var/log/mysql/
      - ./init:/docker-entrypoint-initdb.d/
      - ./mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_unicode_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M
      --default-time-zone='+8:00'
    networks:
      test:
        ipv4_address: 172.18.0.10
networks:
  test:
    external: true

mysqld.cnf

# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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
federated

init.sql

CREATE DATABASE IF NOT EXISTS nacos_config DEFAULT CHARACTER SET = 'utf8mb4';
CREATE USER 'renkaige'@'%' IDENTIFIED BY 'Abc@123456';
GRANT ALL ON nacos_config.* TO 'renkaige'@'%' IDENTIFIED BY 'Abc@123456';

mysql.conf

[mysqld]
   character-set-server=utf8mb4
[client]
   default-character-set=utf8mb4
[mysql]
   default-character-set=utf8mb4

localtime

可以从服务器的/etc/localtime获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值