Skip to content

Docker MySQL5.7

1)普通安装

$ docker pull mysql:5.7
$ docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

2)持久化存储

1
2
3
4
5
6
7
8
9
$ docker run -d -p 3306:3306 --privileged=true \
-v /opt/docker_mysql/log:/var/log/mysql \
-v /opt/docker_mysql/data:/var/lib/mysql \
-v /opt/docker_mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
--restart=unless-stopped \
--name mysql \
mysql:5.7 \

3)Docker-compose 安装

version: '3.8'
services:
  mysql:
    container_name: mysql57
    image: mysql:5.7
    restart: always
    ports:
      - 3306:3306
    privileged: true
    volumes:
      - $PWD/log:/var/log/mysql
      - $PWD/conf:/etc/mysql/conf.d
      - $PWD/data:/var/lib/mysql
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: Admin@123
      MYSQL_USER: admin
      MYSQL_PASS: Admin123
    command: [
        '--character-set-server=utf8mb4',
        '--collation-server=utf8mb4_general_ci',
        # 允许客户端并发连接的最大数量,默认值是151,一般将该参数设置为500-2000
        '--max_connections=3000'
    ]

附加基本配置文件

#!/bin/bash
# 创建程序目录
mkdir -p $PWD/{conf,data,log}

# 创建配置文件
tee $PWD/conf/my.cnf<<-'EOF'
[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
# 实现mysql不区分大小(开发需求,建议开启)
lower_case_table_names=1
# By default we only accept connections from localhost
#bind-address   = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
default-time_zone = '+8:00'

# [可选]日志设置
# error日志路径
# log-error=/var/log/mysql/error.log
# 开启慢查询 1:开启
# slow_query_log=1
# 定义3秒以上为慢查询
# long_query_time=3
# 慢查询日志路径
# slow-query-log-file=/var/log/mysql/slow.log

# 更改字符集 如果想Mysql在后续的操作中文不出现乱码,则需要修改配置文件内容
symbolic-links=0
# 服务端字符集
character-set-server=utf8mb4
# 字符集排序规则
collation-server=utf8mb4_unicode_ci
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
EOF