【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> 
相关推荐
川石课堂软件测试29 分钟前
涨薪技术|Kubernetes(k8s)之认识Pod
功能测试·云原生·容器·贪心算法·kubernetes·单元测试
爱吃喵的鲤鱼1 小时前
MySQL——数据类型
java·数据库·mysql
云上艺旅2 小时前
K8S学习之前站五:清理docker的overlay2 目录
学习·docker·云原生·kubernetes
明月看潮生3 小时前
青少年编程与数学 02-011 MySQL数据库应用 02课题、MySQL数据库安装
数据库·mysql·青少年编程·编程与数学
用户86178277365183 小时前
整表复制
java·后端·mysql
安於宿命4 小时前
【MySQL】表的约束
android·mysql·性能优化
互联网上的猪4 小时前
MySQL 进阶学习笔记(包括MySQL的存储引擎、索引、SQL优化、视图、存储过程、触发器、锁InnoDB引擎和MySQL管理)的相关内容详细版
笔记·学习·mysql
tonngw4 小时前
Docker 使用指南
运维·docker·容器
生活百般滋味,人生需要笑对。 --佚名6 小时前
docker安装rabbitmq
docker·容器·rabbitmq