Use Docker-Compose to run Mysql8.0 and Redis
This article tell you how to use docker-compose to run Mysql8.0 and Redis.
And I will not tell you how to install Docker-ce. You can use search engine like: Bing or Google.
Docker registry-mirrors
If you are in China, you should use proxy to pull images from DockerHub.
You can use my config, the file is /etc/docker/daemon.json
:
1 | { |
Create directory and files
Create
docker-compose.yml
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# version: '3.8' # Now the version attribute is obsoleted in latest version.
services:
mysql:
image: mysql:8.0
restart: always
container_name: mysql-db
ports:
- 3306:3306
logging:
driver: 'json-file'
options:
max-size: '1g'
environment:
# - TZ=Asia/Shanghai
# - LANG=en_US.UTF-8
# - MYSQL_ROOT_HOST='%'
# 配置root密码
- MYSQL_ROOT_PASSWORD=123456
volumes:
# 挂载数据目录
- ./mysql/data:/var/lib/mysql
# 挂载配置文件目录
- ./mysql/my.cnf:/etc/mysql/my.cnf
- /etc/localtime:/etc/localtime:ro
redis:
image: redis:7.2.5-alpine
restart: always
container_name: Redis
ports:
- "6379:6379"
command: redis-server /etc/redis/redis.conf
volumes:
- ./redis/redis.conf:/etc/redis/redis.conf
- ./redis/logs:/logs
- ./redis/data:/dataCreate
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# 绑定监听IP地址
bind 0.0.0.0
# 自定义密码
# requirepass root # 若需要登录密码可在这里设置密码并取消注释
# 启动端口
port 6379
# redis 默认就开启 rdb 全量备份,以下是默认的备份触发机制
# 900s内至少一次写操作则执行bgsave进行RDB持久化
save 900 1
save 300 10
save 60 10000
# 是否压缩 rdb 备份文件,默认是压缩
# 如果 redis 承载的数据量非常大的话,建议不要压缩
# 因为压缩过程中需要耗费大量 cpu 和内存资源,磁盘相对而言比较廉价
rdbcompression yes
# rdb 备份的文件名
dbfilename dump.rdb
# Redis 备份文件存储目录,注意:该路径是 docker 容器内的路径
dir /data
# 是否开启 aof 增量备份功能。会有一定的性能损耗,如果Redis作为cache类应用,不建议开启。
appendonly yes
# AOF文件的名称,这里使用默认值
appendfilename appendonly.aof
# aof 增量备份的策略,这里是每秒钟一次,将累积的写命令持久化到硬盘中
appendfsync everysecCreate
my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22[mysqld]
user=mysql
default-storage-engine=INNODB
character-set-server=utf8
secure-file-priv=NULL # mysql 8 新增这行配置
default-authentication-plugin=mysql_native_password # mysql 8 新增这行配置
port = 3306 # 端口与docker-compose里映射端口保持一致
#bind-address= localhost #一定要注释掉,mysql所在容器和django所在容器不同IP
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
skip-name-resolve # 这个参数是禁止域名解析的,远程访问推荐开启skip_name_resolve。
[client]
port = 3306
default-character-set=utf8
[mysql]
no-auto-rehash
default-character-set=utf8
Run Containers
1 | sudo docker-compose up -d # 创建启动容器并保持在后台运行 |
The status of running:
1 | a@@kelvyn:~/Desktop/db_docker$ sudo docker-compose ps |
以上,都是单节点快速部署的方法,可用于测试、开发用途,生产勿用!后续看情况可能会出哨兵、集群模式的使用方法。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 萌小恩笔记!
评论
TwikooWaline