前提

Docker正常运行,Docker国内镜像已经配置,好的,开始下一步。

运行Mysql8镜像

拉取镜像

1
docker pull mysql

镜像启动和文件挂载

复制下面命令执行,3306是对外访问暴露的端口,当然你也可以设置为3306,主要还是看个人,记得在服务器安全组开放端口。-v /usr/local/mysql是把容器内部挂载在宿主机上,自己也可以更改一下挂载的目录

1
2
3
4
5
6
docker run -p 3306:3306 --name mysql --restart=always --privileged=true \
-v /usr/local/mysql/log:/var/log/mysql \
-v /usr/local/mysql/data:/var/lib/mysql \
-v /usr/local/mysql/conf:/etc/mysql/conf.d \
-v /etc/localtime:/etc/localtime:ro \
-e MYSQL_ROOT_PASSWORD=root -d mysql

创建配置文件

1
2
3
4
cd /usr/local/mysql/conf
# 写入配置文件
vim mysql.cnf

填入一下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
# 设置东八区时区
default-time_zone = '+8:00'
# 设置密码验证规则,default_authentication_plugin参数已被废弃
# 改为authentication_policy
#default_authentication_plugin=mysql_native_password
authentication_policy=mysql_native_password


#secure_file_priv=/var/lib/mysql
secure_file_priv=
init_connect='SET collation_connection = utf8mb4_0900_ai_ci'
init_connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
collation-server=utf8mb4_0900_ai_ci
skip-character-set-client-handshake
skip-name-resolve

重启容器

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
# 重启mysql容器

docker restart mysql
# 设置docker启动时启动mysql
docker update mysql --restart=always

# 重启mysql容器
docker restart mysql

# 进入mysql容器内部
docker exec -it mysql /bin/bash

# 连接Mysql
mysql -uroot -p

# 设置用户远程登陆
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';

# 刷新数据库
flush privileges;

# 退出mysql
exit;

# 退出mysql容器
exit

运行Redis镜像

创建配置文件

1
2
3
4
5
mkdir /data/redis


vim redis.conf

填入以下内容

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 注释以便外网访问
#bind 127.0.0.1

protected-mode yes

port 6379

tcp-backlog 511

timeout 0

# 防止出现远程主机强迫关闭了一个现有的连接的错误 默认是300
tcp-keepalive 300

# 使用守护线程启动
daemonize no

supervised no

pidfile /var/run/redis_6379.pid

loglevel notice

logfile ""

databases 16

always-show-logo yes

save 900 1
save 300 10
save 60 10000

stop-writes-on-bgsave-error yes

rdbcompression yes

rdbchecksum yes

dbfilename dump.rdb

rdb-del-sync-files no

dir ./

replica-serve-stale-data yes

replica-read-only yes

repl-diskless-sync no

repl-diskless-sync-delay 5


repl-diskless-load disabled

repl-disable-tcp-nodelay no

# By default the priority is 100.
replica-priority 100

acllog-max-len 128

# redis访问密码
requirepass redis213

lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no



lazyfree-lazy-user-del no

# Redis持久化
appendonly yes

appendfilename "appendonly.aof"

appendfsync everysec

no-appendfsync-on-rewrite no


auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

aof-load-truncated yes

aof-use-rdb-preamble yes

lua-time-limit 5000

slowlog-log-slower-than 10000


slowlog-max-len 128


latency-monitor-threshold 0

notify-keyspace-events ""


hash-max-ziplist-entries 512
hash-max-ziplist-value 64

list-max-ziplist-size -2


list-compress-depth 0


set-max-intset-entries 512

zset-max-ziplist-entries 128
zset-max-ziplist-value 64

hll-sparse-max-bytes 3000

stream-node-max-bytes 4096
stream-node-max-entries 100

activerehashing yes

client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

hz 10

dynamic-hz yes


aof-rewrite-incremental-fsync yes

rdb-save-incremental-fsync yes

jemalloc-bg-thread yes

主要注意几个配置项

bind 127.0.0.1 #注释掉这部分,使redis可以外部访问
daemonize no#用守护线程的方式启动
requirepass 你的密码#给redis设置密码
appendonly yes#redis持久化  默认是no
tcp-keepalive 300 #防止出现远程主机强迫关闭了一个现有的连接的错误 默认是300

运行容器

运行以下命令,

1
2
3
4
5
docker run -p 6379:6379 --name redis --restart=always  --privileged=true \
-v /home/wlf/mydata/redis/redis.conf:/etc/redis/redis.conf \
-v /home/wlf/mydata/redis/data:/data \
-d redis redis-server /etc/redis/redis.conf --appendonly yes

运行完成过后docker ps查看容器是否运行即可。

image

查看日志

docker logs redis

image

ai绘画