3

Redis + Docker 搭建方案

 2 years ago
source link: https://jack-zheng.github.io/hexo/2022/01/24/Redis-Docker/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Redis + Docker 搭建方案

发表于

2022-01-24 更新于 2022-03-02 分类于 Redis

阅读次数: 3 本文字数: 1.8k 阅读时长 ≈ 2 分钟

通过 Redis 搭建 docker 实验环境

docker pull redis

# 创建容器
docker run -itd --name redis-test -p 6379:6379 redis

# 进入容器
docker exec -it redis-test /bin/bash

# 开启客户端
redis-cli
# 启动远程客户端
# redis-cli -h host -p port -a password

# 测试
ping
# PONG

# 默认有 16 个数据库,默认用第 0 个
select 1

# 显示当前库下所有的键
keys *

# 清空当前 DB
flushdb

# 删除所有数据库,危
flushall

Issues

清空的时候抛错,我估计是我的 docker redis 没有配置本地存储信息,并不能进行本地化相关操作,创建的时候挂在到本地可能就行了

flushdb 
(error) MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persist on disk. Commands that may modify the data set are disabled, because this instance is configured to report errors during writes if RDB snapshotting fails (stop-writes-on-bgsave-error option). Please check the Redis logs for details about the RDB error.

猜测失败,是配置问题,关闭了快找功能,导致保存失败,可以在客户端使用 config set stop-writes-on-bgsave-error no 解决问题

# 单个值最大能存储 512M
set name "Jack"

# 拿值
get name

# 查看类型
type name

# 存储 hash
HMSET runoob field1 "Hello" field2 "World"

# hash 取值
HGET runoob field1
#"Hello"
HGET runoob field2
#"World"

# list 列表,左序存入
lpush runoob redis
lpush runoob mongodb
lpush runoob rabbitmq

lrange runoob 0 10
# 1) "rabbitmq"
# 2) "mongodb"
# 3) "redis"

# 查看列表大小
llen runoob

# Set 无序集合
sadd members jack
sadd members tom
sadd members tom
smembers members
# 1) "tom"
# 2) "jack"

# 查看大小
scard members

# zset 有序集合
zadd dbs 0 mongodb
zadd dbs 0 redis
zadd dbs 0 rabit

ZRANGEBYSCORE dbs 0 10000
# 1) "mongodb"
# 2) "rabit"
# 3) "redis"

# 查看大小
zcard dbs

Using Redis in Python

# 安装依赖
pip install redis

# ipython 操作
import redis
r = redis.StrictRedis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
print(r['foo'])
# b'bar'
print(type(r.get('foo')))
# <class 'bytes'>

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK