docker之redis使用
#拉取redis > docker pull redis:latest latest: Pulling from library/redis 8d691f585fa8: Pull complete 8ccd02d17190: Pull complete 4719eb1815f2: Pull complete 200531706a7d: Pull complete eed7c26916cf: Pull complete e1285fcc6a46: Pull complete Digest: sha256:fe80393a67c7058590ca6b6903f64e35b50fa411b0496f604a85c526fb5bd2d2 Status: Downloaded newer image for redis:latest docker.io/library/redis:latest #查看images > docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest de25a81a5a0b 6 days ago 98.2MB
#运行
docker run -ti redis:latest /bin/bash #启动redis:后台启动redis 并将redis6379端口映射至本地6380端口 容器名设为some-redis1 > docker run --name some-redis1 -p 6380:6379 -d redis:latest --requirepass "123456" a996cb04b0024be3fd4f6d08e3b567ad9389d9c59b1185f601400ab48e17cefd #查看容器 > docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a996cb04b002 redis:latest "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6380->6379/tcp some-redis1 #进入容器&&连接redis客户端 > docker exec -ti a996cb04b002 redis-cli 127.0.0.1:6379> #运行redis命令 127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> set name panpan OK 127.0.0.1:6379> get name "panpan" 127.0.0.1:6379> quit
# redis 集群中查询 #1.进入集群 redis-cli -h $host -p 6379 -c #2.模糊查询key host:6379> KEYS "*GetPartAccount_*" #3.删除key DEL "Account"
#进入容器 > docker exec -ti a996cb04b002 /bin/bash #查看环境变量 root@a996cb04b002:/data# env HOSTNAME=a996cb04b002 REDIS_DOWNLOAD_SHA=6624841267e142c5d5d5be292d705f8fb6070677687c5aad1645421a936d22b3 PWD=/data HOME=/root REDIS_VERSION=5.0.6 GOSU_VERSION=1.11 TERM=xterm REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-5.0.6.tar.gz SHLVL=1 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin _=/usr/bin/env #查看目录 root@a996cb04b002:/data# ls #查看容器内运行的进程 root@a996cb04b002:/data# ps -ef bash: ps: command not found #进入redis客户端 root@a996cb04b002:/data# redis-cli #退出redis客户端 127.0.0.1:6379> quit #远程连接redis root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 #操作redis-cli时需认证 auth yourpassword
应用示例
#模糊查询 root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 --scan --pattern '*GetPartAccount_*' DVMProxyApi-devGetPartAccount_facebook DVMProxyApi-alphaGetPartAccount_facebook DVMProxyApi-testGetPartAccount_facebook #查询value数据 root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 :6379> type DVMProxyApi-devGetPartAccount_facebook hash :6379> Hkeys DVMProxyApi-devGetPartAccount_facebook 1) "absexp" 2) "data" 3) "sldexp" :6379> HGET DVMProxyApi-devGetPartAccount_facebook sldexp "-1" :6379> HGET DVMProxyApi-devGetPartAccount_facebook data "{\"data\":[******
#.net core:获取滑动过期时间 :6379> HGET DVMProxyApi-devGetPartAccount_facebook sldexp "864000000000" #查询redis过期时间:以秒为单位返回 :6379> ttl DVMProxyApi-testGetPartAccount_facebook (integer) 79765 #powershell 时间段转为ttl > New-TimeSpan -Hours 24 #New-TimeSpan -seconds 76765 Days : 1 Hours : 0 Minutes : 0 Seconds : 0 Milliseconds : 0 Ticks : 864000000000 TotalDays : 1 TotalHours : 24 TotalMinutes : 1440 TotalSeconds : 86400 TotalMilliseconds : 86400000
#删除fields :6379> hdel DVMProxyApi-devGetPartAccount_facebook data absexp sldexp (integer) 3 #查看过期时间 :6379> ttl DVMProxyApi-testGetPartAccount_facebook (integer) 78700 #获取在哈希表中指定 key 的所有字段和值 :6379> hgetall DVMProxyApi-devGetPartAccount_facebook (empty list or set) #删除key :6379> del DVMProxyApi-devGetPartAccount_facebook (integer) 0 #检查给定 key 是否存在。 :6379> exists DVMProxyApi-devGetPartAccount_facebook (integer) 0 :6379> quit root@a996cb04b002:/data# redis-cli -h 192.168.*.* -p 6379 --scan --pattern '*GetPartAccount_*' DVMProxyApi-alphaGetPartAccount_facebook DVMProxyApi-testGetPartAccount_facebook root@a996cb04b002:/data#
redis-cli, the Redis command line interface