使用Dockerfile构建mysql8.0.20

使用Dockerfile构建mysql8.0.20
在官方镜像 docker.io/library/mysql:8.0.20 的基础上二次构建并做mysql配置优化

1、编写Dockerfile
在构建之前,准备优化过的my.cnf文件,文件放到跟Dockerfile 同一个级别的目录下
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
[mysqld]
server-id=1
bind-address=0.0.0.0
datadir=/data/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
default-time_zone='+8:00'
default-storage-engine=INNODB
default_authentication_plugin=caching_sha2_password   # mysql 8.0用
innodb-print-all-deadlocks = true
max_binlog_size=500M
sort_buffer_size=128M
sync_binlog=1
binlog_rows_query_log_events=1
binlog_row_image='full'
log_output=file
slow_query_log=ON
slow_query_log_file=/var/lib/mysql/mysql-slow.log
log_queries_not_using_indexes=off
long_query_time=3
log_bin=ON
binlog_format=ROW
log-bin=/var/lib/mysql/mysql-bin
# expire_logs_days=15            # mysql 5.x 用
binlog_expire_logs_seconds=604800  # mysql8.0 弃用expire_logs_days
relay_log=mysql-relay-bin
wait_timeout=28800
interactive_timeout=28800
master_info_repository=TABLE
relay_log_info_repository=TABLE
autocommit=ON
report_port=3306
character-set-server=utf8mb4
lower_case_table_names=1
pid-file=/var/run/mysqld/mysqld.pid
log-error=/var/lib/mysql/mysql-error
secure_file_priv=/var/lib/mysql
federated
back_log=3000
max_connections=16384
max_connect_errors=1000
max_user_connections=20000
innodb_buffer_pool_size=8G
innodb_buffer_pool_chunk_size=2G
innodb_buffer_pool_instances=4
innodb_log_file_size=512M
innodb_log_buffer_size=32M
innodb_thread_concurrency=17
innodb_autoextend_increment=64
innodb_concurrency_tickets=5000
innodb_old_blocks_time=1000
innodb_stats_on_metadata=0
innodb_file_per_table=1
innodb_checksum_algorithm=0
join_buffer_size=4M
max_allowed_packet=128M
sort_buffer_size=128M
table_definition_cache=32767
binlog_row_event_max_size=16K
sync_master_info=10000
sync_relay_log=10000
sync_relay_log_info=10000
event_scheduler=ON
innodb_flush_neighbors=1
innodb_page_size=16K
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_flush_log_at_trx_commit=1
flush_time=0
thread_stack=256k
thread_cache_size=128
open_files_limit=65535
tmp_table_size=500M
max_heap_table_size=500M
character_set_server=utf8mb4
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# # Settings user and group are ignored when systemd is used.
# # If you need to run mysqld under a different user or group,
# # customize your systemd unit file for mariadb according to the
# # instructions in http://fedoraproject.org/wiki/Systemd
wait_timeout=28800
interactive_timeout=28800
skip-name-resolve
binlog_cache_size = 1M
# #
[client]
port=3306
socket=/tmp/mysql.sock
default-character-set=utf8mb4

[mysqld_safe]
open-files-limit=32767
innodb_open_files=32767

Dockerfile 文件内容如下:

1
2
3
4
5
6
FROM mysql:8.0.20
MAINTAINER huading
COPY my.cnf /etc/mysql/my.cnf
ENV MYSQL_ROOT_PASSWORD root
EXPOSE 3306
CMD ["mysqld"]

2、构建容器
进入Dockerfile文件所在的目录,执行下面的命令进行容器构建
docker build -t mysql:v8.0.20 .
使用docker image 命令查看构建的镜像
使用Dockerfile构建mysql8.0.20

3、使用镜像创建mysql容器
测试1
docker run --name hd-mysql -p 3306:3306 -d mysql:v8.0.20
使用Dockerfile构建mysql8.0.20
容器hd-mysql 数据库root密码为root

测试2
删除测试1创建的hd-mysql容器, 使用下面命令来创建容器
docker run --name hd-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=hd2020 -d mysql:v8.0.20
使用Dockerfile构建mysql8.0.20
容器hd-mysql 数据库root密码为hd2020 , 我们可以看到虽然我们在构建mysql8.0.20镜像的时候,初始数据库密码为root, 在启动容器的时候,我们使用 -e MYSQL_ROOT_PASSWORD 来更新了root密码。

ps: 我们这个mysql8.0.20镜像做了数据库优化。

相关新闻

联系我们

全国服务热线

400-033-9553

电子邮件:admin@example.com
工作时间:09:00-17:00 周一至周五

在线客服
关注微信
关注微信
分享本页
返回顶部