Wsl2中Ubuntu中Docker运行Mysql8镜像及Redis镜像

前提

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

运行Mysql8镜像

拉取镜像

docker pull mysql

镜像启动和文件挂载

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

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

创建配置文件

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

填入一下内容

[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

重启容器

# 重启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镜像

创建配置文件

mkdir /data/redis


vim redis.conf

填入以下内容

# 注释以便外网访问
#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

运行容器

运行以下命令,

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绘画


Wsl2中Ubuntu中Docker运行Mysql8镜像及Redis镜像
https://wangijun.com/2023/06/17/other-13/
作者
无良芳
发布于
2023年6月17日
许可协议