☰
dockers-compose离线安装MySQL8.0.46
2026/10/6 18:54:55 网站建设 项目流程

dockers-compose离线安装MySQL8.0.46

首先通过在一台可以上网的服务器上下载MySQL 8.0.46的镜像文件再导入到生产环境的容器中。

# docker pull mysql:8.0.46 8.0.46: Pulling from library/mysql 96d30d9fbee8: Pull complete edf85873f64e: Pull complete 6ef6c7b50a93: Pull complete e3e5d1ac74c1: Pull complete 0d74d296605b: Pull complete 297d04cfe470: Pull complete 4c8a3e0d4e4b: Pull complete a63160a5eda1: Pull complete 7534d1db9f8d: Pull complete 49ec2dab01d9: Pull complete ab24264a27e9: Pull complete 796812c73292: Download complete af166387641d: Download complete Digest: sha256:7dcddc01f13bab2f15cde676d44d01f61fc9f99fe7785e86196dfc07d358ae2b Status: Downloaded newer image for mysql:8.0.46 docker.io/library/mysql:8.0.46 [root@dockerser sf_]# [root@dockerser sf_]# docker images i Info → U In Use IMAGE ID DISK USAGE CONTENT SIZE EXTRA databasus/databasus:latest f41965acf7e7 1.66GB 458MB U mysql:8.0.43 3e646bcda0d9 1.07GB 250MB U mysql:8.0.46 7dcddc01f13b 1.09GB 249MB postgres:12.16 974bda3757f2 575MB 151MB U postgres:14.18 90da0743e4ed 624MB 163MB # docker save -o /opt/mysql8.0.46.tar mysql:8.0.46

目标端导入:

[root@db-sjhf-27-24 docker_images]# docker load -i mysql8.0.46.tar d43f07c40901: Loading layer [==================================================>] 47.31MB/47.31MB afe04ba8686c: Loading layer [==================================================>] 884B/884B 4a5079cdf3cb: Loading layer [==================================================>] 783.6kB/783.6kB ac8332d12fa0: Loading layer [==================================================>] 6.173MB/6.173MB b7ee228f4507: Loading layer [==================================================>] 2.607kB/2.607kB 1700d9acc2ce: Loading layer [==================================================>] 335B/335B e6e1a47306ad: Loading layer [==================================================>] 49.93MB/49.93MB 16d50f7c95c0: Loading layer [==================================================>] 316B/316B 1bc63e2ed476: Loading layer [==================================================>] 129.4MB/129.4MB 04024da05501: Loading layer [==================================================>] 5.327kB/5.327kB cdb99ca7755b: Loading layer [==================================================>] 120B/120B Loaded image: mysql:8.0.46 [root@db-sjhf-27-24 docker_images]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql 8.0.46 6cd09145362d 5 months ago 799MB

创建目录

mkdir -p /data/mysql_dkcp/{data,conf,log}

创建yml文件

vi docker-mysql.yml

services: mysql: image: mysql:8.0.46 container_name: mysql restart: always environment: MYSQL_ROOT_PASSWORD: "Pwd123" MYSQL_ROOT_HOST: "%" TZ: Asia/Shanghai mem_limit: 5g memswap_limit: 5g mem_reservation: 5g # ============================================================ # 文件句柄 / 进程限制 # ============================================================ ulimits: nofile: soft: 65535 hard: 65535 nproc: soft: 65535 hard: 65535 # ============================================================ # 数据持久化 # ============================================================ volumes: - /data/mysql_dkcp/data:/var/lib/mysql # MySQL 配置 - /data/mysql_dkcp/conf/my-mysql.cnf:/etc/mysql/conf.d/my-mysql.cnf:ro # 日志 - /data/mysql_dkcp/log:/var/log/mysql # 初始化脚本 - /data/mysql_dkcp/init-scripts:/docker-entrypoint-initdb.d # 时区 - /etc/localtime:/etc/localtime:ro # ============================================================ # 使用宿主机网络 # 对 MGR 33061 以及 MySQL 3306 比较方便 # ============================================================ network_mode: host # ============================================================ # MySQL 启动参数 # ============================================================ command: - --default-authentication-plugin=mysql_native_password - --lower_case_table_names=1 # ============================================================ # 健康检查 # ============================================================ healthcheck: test: [ "CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -u root -p\"$$MYSQL_ROOT_PASSWORD\" --silent" ] interval: 10s timeout: 5s retries: 5 start_period: 60s

创建my-mysql.cnf文件

# cat my-mysql.cnf [mysqld] ############################ # Basic ############################ port=3306 character-set-server=utf8mb4 collation-server=utf8mb4_0900_ai_ci default-time-zone='+08:00' lower_case_table_names=1 ############################ # InnoDB ############################ innodb_buffer_pool_size=2G innodb_buffer_pool_instances=4 innodb_log_buffer_size=64M innodb_flush_log_at_trx_commit=1 innodb_flush_method=O_DIRECT sync_binlog=1 ############################ # Connection ############################ max_connections=500 thread_cache_size=100 ############################ # Per-connection memory ############################ sort_buffer_size=2M join_buffer_size=2M read_buffer_size=128K read_rnd_buffer_size=256K ############################ # Temporary tables ############################ tmp_table_size=128M max_heap_table_size=128M ############################ # Table cache ############################ table_open_cache=4000 table_definition_cache=2000 ############################ # File descriptor ############################ open_files_limit=65535 ############################ # Binary Log ############################ server-id=27 log_bin=mysql-bin binlog_format=ROW binlog_expire_logs_seconds=604800 max_binlog_size=512M ############################ # Performance Schema ############################ performance_schema=ON ############################ # Slow Log ############################ slow_query_log=ON slow_query_log_file=/var/log/mysql/slow.log long_query_time=2 ############################ # Error Log ############################ log_error=/var/log/mysql/error.log

启动mysql

cd /data/mysql_dkcp docker-compose -f docker-mysql.yml up -d

查看容器状态
docker-compose -f docker-mysql.yml ps

查看启动日志
第一次启动建议重点看日志:
docker-compose -f docker-mysql.yml logs --tail=100 mysql

实时观察:
docker-compose -f docker-mysql.yml logs -f mysql

检查 Healthcheck,正常应该返回:healthy
docker inspect --format=‘{{.State.Health.Status}}’ mysql

确认 3306 端口
因为你当前配置使用:
network_mode: host
所以直接检查:
ss -lntp | grep 3306

现在做一次 MySQL 登录验证
docker exec -it mysql mysql -uroot -p"Pwd123"

再看实际资源
docker stats mysql --no-stream

如果只是修改配置文件 my-mysql.cnf,则:
docker-compose -f docker-mysql.yml restart

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询