借鑒博客:https://cloud.tencent.com/developer/article/1670205
1、直接使用docker命令下一個redis的鏡像:
docker pull redis
2、在/home目錄里創建一個redis目錄,里面放一個redis.conf文件和一個data目錄,如下:
redis.conf文件可以到官網里下,然后再上傳到服務器:https://hub.docker.com/_/redis?tab=tags,下載后找到redis.conf這個文件上傳到服務器
3、修改redis.conf配置文件的幾個地方:vim redis.conf
bind 127.0.0.1 #注釋掉這部分,使redis可以外部訪問 daemonize no#用守護線程的方式啟動 requirepass 你的密碼#給redis設置密碼 appendonly yes#redis持久化 默認是no tcp-keepalive 300 #防止出現遠程主機強迫關閉了一個現有的連接的錯誤 默認是300
redis.conf文件內容:可以直接用
# bind 192.168.1.100 10.0.0.1 # bind 127.0.0.1 ::1 #bind 127.0.0.1 protected-mode no port 6379 tcp-backlog 511 requirepass 000415 timeout 0 tcp-keepalive 300 daemonize no supervised no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 30 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 dir ./ replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-disable-tcp-nodelay no replica-priority 100 lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly yes appendfilename "appendonly.aof" 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-max-len 128 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 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
4、使用docker命令啟動redis:
docker run -p 6379:6379 --name redis -v /home/redis/redis.conf:/etc/redis/redis.conf -v /home/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes
或者:加上設置密碼 docker run --restart=always -p 6379:6379 --name myredis -v /home/redis/redis.conf:/etc/redis/redis.conf -v /home/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
2023年4月21日:今天不知道什么鬼,使用掛載命令總是不成功,可能docker下載的版本太新了
那就不掛載了吧,使用不掛載的命令:
docker run -itd --name myredis -p 6379:6379 redis
5、查看是否啟動成功
docker ps #查看正在運行的窗口
docker logs redis #或打印一下redis的啟動日志
成功如下:
.