docker部署mysql8

文件目录结构如下:

image-20230315111913878

my.cnf 文件内容如下:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#my.cnf参考,请根据实际情况修改配置参数
[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysql]
prompt="MySQL [\\d]> "
no-auto-rehash

[mysqld]
port = 3306
socket = /tmp/mysql.sock
default_authentication_plugin = mysql_native_password
default-time_zone = '+8:00'
datadir = /data
pid-file = /data/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
secure_file_priv=/var/lib/mysql

init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci

skip-name-resolve
skip-log-bin
#skip-networking
back_log = 300

# For Mysql8.0 Only
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

max_connections = 2607
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M

thread_cache_size = 8

ft_min_word_len = 4

#log_bin = mysql-bin
#binlog_format = mixed
#binlog_expire_logs_seconds = 604800

#log_error = /data/mysql-error.log
#slow_query_log = 1
#long_query_time = 1
#slow_query_log_file = /data/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1

interactive_timeout = 28800
wait_timeout = 28800

[mysqldump]
quick
max_allowed_packet = 500M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

docker-compose.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
version: "2.0"

services:

  db:
    image: mysql:8.0
    command:
      --max_connections=1000
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ./mysql/data:/var/lib/mysql/ # ./mysql路径可以替换成自己的路径
      - ../../sql/:/docker-entrypoint-initdb.d/ # 初始化的脚本
      - ./mysql/my.cnf:/etc/mysql/my.cnf # 配置文件
    ports:
      - 3306:3306
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: sf-admin
      MYSQL_USER: sf-admin
      MYSQL_PASSWORD: 123456

  redis:
    image: redis:alpine
    command: --requirepass "123456"
    restart: always
    ports:
      - 6379:6379
    environment:
      TZ: Asia/Shanghai

启动命令如下:

1
docker-compose up -d  --compatibility

接下来学习一下限制资源的写法:

image-20230315112324241

重启服务一下,看看

由于做了资源限制, 并且没有使用 swarm, 所以要加上–compatibility 参数, 不然会报错 WARNING: Some services (web) use the 'deploy' key, which will be ignored. Compose does not support 'deploy' configuration - use docker stack deploy to deploy to a swarm.

注意:

⚠️ 注意:启动 mysql 报如下错误,那是因为 MYSQL 新特性 secure_file_priv 对读写文件的影响。

ERROR: mysqld failed while attempting to check config command was: “mysqld –verbose –help”

mysqld: Error on realpath() on ‘/var/lib/mysql-files’ (Error 2 - No such file or directory) 2019-09-14T09:52:51.015937Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for –secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files 2019-09-14T09:52:51.018328Z 0 [ERROR] [MY-010119] [Server] Aborting 解决问题: windows 下:修改 my.ini 在[mysqld]内加入 secure_file_priv=/var/lib/mysql linux 下:修改 my.cnf 在[mysqld]内加入 secure_file_priv=/var/lib/mysql 三、连接 MySQL 1、进入 mysql 容器 docker exec -it my_mysql /bin/bash

2、登陆 mysql mysql -u root -p

3、mysql 8 设置允许远程用户访问 update user set host = ‘%’ where user = ‘root’ and host=‘localhost’; # 更新 root - localhost 为 root - %

GRANT ALL ON . TO ‘root’@’%’; # 设置允许远程用户访问

flush privileges;# 刷新权限

ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘密码’;#更新用户加密方式,mysql8 默认的加密方式为 caching_sha2_password 与 mysql5 的加密方式 mysql_native_password 不同

Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 06, 2025 05:52 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up