前言

Linux系统使用docker安装各种服务是一种简单的软件部署方案,这里对常用的mysql数据库做一个记录哈。

安装docker

忽略

下载镜像

1
docker pull mysql:8.4.0

启动脚本

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
#!/bin/bash

# 创建目录
[ ! -f ./my.cnf ] && echo 'my.cnf not found.' && exit

[ ! -d /opt/mysql ] && sudo mkdir /opt/mysql
[ ! -d /opt/mysql/etc ] && sudo mkdir /opt/mysql/etc
[ ! -d /opt/mysql/etc/conf.d ] && sudo mkdir /opt/mysql/etc/conf.d
[ ! -d /opt/mysql/data ] && sudo mkdir /opt/mysql/data
[ ! -d /opt/mysql/log ] && sudo mkdir /opt/mysql/log

cp ./my.cnf /opt/mysql/etc/

# 执行Docker进程启动mysql
# 最后一行,替换为mysql镜像ID(docker images 可以查看镜像明细)

docker run \
--privileged=true \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=root \
--restart=always \
-v /opt/mysql/etc:/etc/mysql \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/log:/log \
-v /usr/share/zoneinfo:/usr/share/zoneinfo \
--name mysql \
-d \
5fc9b4335a45

配置文件

需要和启动脚本放置在同一个目录内, 命名为 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
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.4/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

log_timestamps=system
max_connections=1024
default-time_zone = +8:00
default-storage-engine=INNODB
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
# default_authentication_plugin=mysql_native_password

host-cache-size=0
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

pid-file=/var/run/mysqld/mysqld.pid

[client]
default-character-set=utf8mb4
socket=/var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/