【docker】部署MySQL容器

1. 获取镜像

docker pull mysql:5.7

jsx 复制代码
[#27#root@bserptest15 3309]# docker pull mysql:5.7
5.7: Pulling from library/mysql
20e4dcae4c69: Pull complete 
1c56c3d4ce74: Pull complete 
e9f03a1c24ce: Pull complete 
68c3898c2015: Pull complete 
6b95a940e7b6: Pull complete 
90986bb8de6e: Pull complete 
ae71319cb779: Pull complete 
ffc89e9dfd88: Pull complete 
43d05e938198: Pull complete 
064b2d298fba: Pull complete 
df9a4d85569b: Pull complete 
Digest: sha256:4bc6bc963e6d8443453676cae56536f4b8156d78bae03c0145cbe47c2aad73bb
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7

查看镜像:docker images

jsx 复制代码
[#28#root@bserptest15 3309]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED         SIZE
tomcat                          latest    88b0f1cee84c   10 days ago     519MB
goharbor/prepare                v2.12.2   617f50c1808f   8 weeks ago     208MB
**mysql                           5.7       5107333e08a8   15 months ago   501MB**
centos                          7         b5b4d78bc90c   4 years ago     203MB

2. 运行容器

2.1 准备my.cnf文件

vim /opt/mysql/3309/conf/my.cnf

jsx 复制代码
[mysqld]
# 基本配置
user = mysql
port = 3306
bind-address = 0.0.0.0  # 允许所有 IP 访问(根据需要修改)
datadir = /var/lib/mysql
socket = /var/run/mysqld/mysqld.sock
pid-file = /var/run/mysqld/mysqld.pid

# 性能优化
max_connections = 200         # 最大连接数
wait_timeout = 600            # 空闲连接超时时间(秒)
interactive_timeout = 600     # 交互式连接超时时间(秒)
innodb_buffer_pool_size = 1G  # InnoDB 缓冲池大小(根据内存调整)
innodb_log_file_size = 256M   # InnoDB 日志文件大小
innodb_flush_log_at_trx_commit = 1  # 默认值,保证事务的 ACID 特性
query_cache_type = 0          # 关闭查询缓存(MySQL 8.0 已移除)
query_cache_size = 0          # 查询缓存大小(关闭时设置为 0)

# 日志配置
log-error = /var/log/mysql/error.log       # 错误日志路径
general_log = 0                            # 关闭通用查询日志(生产环境建议关闭)
general_log_file = /var/log/mysql/general.log  # 通用查询日志路径
slow_query_log = 1                         # 开启慢查询日志
slow_query_log_file = /var/log/mysql/slow.log  # 慢查询日志路径
long_query_time = 2                        # 慢查询阈值(秒)

# 安全配置
skip-name-resolve                           # 跳过 DNS 解析,提高性能
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
secure-file-priv = /var/lib/mysql-files     # 限制 LOAD DATA 和 SELECT INTO OUTFILE 的目录
symbolic-links = 0                          # 禁用符号链接以防止安全风险

# 字符集配置
character-set-server = utf8mb4              # 使用 UTF-8 编码支持多语言
collation-server = utf8mb4_general_ci       # 默认排序规则

# 其他配置
tmp_table_size = 64M                        # 内存中临时表的最大大小
max_heap_table_size = 64M                   # 内存中用户创建的堆表的最大大小
table_open_cache = 400                      # 表缓存数量

[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
default-character-set = utf8mb4             # 客户端默认字符集

[mysql]
default-character-set = utf8mb4             # MySQL 客户端工具默认字符集

2.2 运行MySQL

如下示例,我们挂载了3个本地目录,docker容器内的MySQL会访问这些目录

为了避免权限问题,这里提前为mysql用户授权,默认为999

chown -R 999:999 /opt/mysql/3309

jsx 复制代码
docker run --name mysql57 \
>   -e MYSQL_ROOT_PASSWORD=lyu888 \
>   -p 3309:3306 \
>   -v /opt/mysql/3309/data:/var/lib/mysql \
>   -v /opt/mysql/3309/log:/var/log/mysql \
>   -v /opt/mysql/3309/conf/my.cnf:/etc/my.cnf \
>   -d mysql:5.7

--name:指定容器名称为mysql57
-p :端口映射,将容器端口3306映射到宿主机端口3309
-v :目录挂载,宿主机目录:容器目录,宿主机目录会自动创建
-d :容器启动后在后端执行

如果不确定容器内mysql用户的UID和GID,可以通过以下步骤确认

jsx 复制代码
1.启动一个临时容器并进入交互模式
docker run --rm -it mysql:5.7 bash

2.查看MySQL用户的UID和GID
id mysql

3.输出示例:
uid=999(mysql) gid=999(mysql) groups=999(mysql)
可以看到 UID 和 GID 都是 999

3. 验证MySQL启动状态

3.1 docker ps 查看容器运行状态

jsx 复制代码
[#76#root@bserptest15 3309]# docker ps
CONTAINER ID   IMAGE                                 COMMAND                   CREATED             STATUS                PORTS                                                                            NAMES
**5814ce4bc2f5   mysql:5.7**                             "docker-entrypoint.s..."   About an hour ago   Up About an hour      33060/tcp, 0.0.0.0:3309->3306/tcp, :::3309->3306/tcp                             mysql57
aeb76750916e   tomcat:latest                         "catalina.sh run"         4 days ago          Up 4 days             0.0.0.0:8080->8080/tcp, :::8080->8080/tcp                                        elegant_ramanujan

3.2 docker exec 进入容器访问

docker exec -it mysql57 mysql -u root -p

jsx 复制代码
[#75#root@bserptest15 3309]# docker exec -it mysql57 mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

3.3 直接在宿主机上访问

mysql -u root -p -h127.0.0.1 -P3309

jsx 复制代码
[#77#root@bserptest15 3309]# mysql -u root -p -h127.0.0.1 -P3309
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
相关推荐
xin-cyy1 小时前
MySQL的索引和事务
数据库·mysql
不学无术の码农2 小时前
Ubuntu 22.04 (WSL2) 上使用 Docker 安装 Nacos 3.0.0
ubuntu·docker
AnnyYoung4 小时前
从Dockerfile 构建docker镜像——保姆级教程
docker·容器·eureka
一眼青苔4 小时前
如何在MySQL中实现类似Redis的PING命令的功能来检测连接状态?
数据库·redis·mysql
慧一居士4 小时前
Docker Compose 的详细使用总结、常用命令及配置示例
容器·架构
何怀逸4 小时前
安装 Docker
运维·docker·容器
码码哈哈0.04 小时前
2025最新:3分钟使用Docker快速部署单节点Redis
redis·docker·eureka
云攀登者-望正茂4 小时前
通过Kubernetes 外部 DNS控制器来自动管理Azure DNS 和 AKS
容器·kubernetes·azure
裁二尺秋风5 小时前
k8s(11) — 探针和钩子
java·容器·kubernetes
hweiyu005 小时前
MySQL性能分析工具:SHOW PROCESSLIST
数据库·mysql