redis安装使用
原创大约 3 分钟
redis安装使用
redis 自述
参考资料:
https://redis.io/docs/latest/get-started/
Redis 是一种内存数据存储,广泛被数百万开发者用于缓存、向量数据库、文档数据库、流处理引擎和消息代理。Redis 内置了复制功能,并提供不同级别的磁盘持久化。它支持复杂的数据类型(例如,字符串、哈希、列表、集合、有序集合和 JSON),并在这些数据类型上定义了原子操作。
Redis Stack 介绍
Redis Stack 是在 Redis 的基础上扩展的版本,包含了一些额外的模块和功能,使其更加强大和多功能。Redis Stack 包含了以下组件:
- RedisJSON:用于存储、查询和操作 JSON 数据。
- RediSearch:用于全文搜索和二级索引。
- RedisTimeSeries:用于时间序列数据的高效存储和分析。
- RedisBloom:用于布隆过滤器、Count-Min Sketch 等概率数据结构。
- RedisGraph:用于图形数据存储和图形查询。
总结
Redis 适合简单、高性能的数据存储和缓存需求,而 Redis Stack 适合需要更复杂数据处理能力的应用。如果你的应用需要处理 JSON 数据、执行全文搜索、处理时间序列数据或使用图形数据结构,那么 Redis Stack 是一个更好的选择。否则,Redis 的简单性和高性能可能已经足够满足你的需求。
搭建redis
docker 配置文件下载
wget http://download.redis.io/redis-stable/redis.conf
或
wget https://raw.githubusercontent.com/redis/redis/unstable/redis.conf
redis配置项部分修改
# bind * -::* # like the default, all available interfaces
bind * -::*
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
protected-mode no
# user <username> ... acl rules ...
#
# For example:
#
# user worker +@list +@connection ~jobs:* on >ffa9203c493aa99
user redis +@all ~* on >password
user default +@all ~* on >password
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
requirepass # IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
requirepass password
docker 启动参考
docker run -d -p 6379:6379 --name redis-sample redis:7.2.5
# or
ROOT='/data/redis'
docker run -d -p 6379:6379 --name redis-sample \
-v ${ROOT}/redis.conf:/etc/redis.conf \
-v ${ROOT}/data:/data \
redis:7.2.5 redis-server /etc/redis.conf
运行操作
docker exec -it redis-sample /bin/bash
root@68cd7cf68911:/etc# redis-cli
127.0.0.1:6379> set k 1234
OK
127.0.0.1:6379> get k
"1234"
日志报错解决
1:M 11 Jul 2024 05:30:55.323 # Failed to write PID file: Permission denied
更新配置文件
# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
#
# Note that on modern Linux systems "/run/redis.pid" is more conforming
# and should be used instead.
pidfile /tmp/redis_6379.pid
docker compose 文件
version: '3'
services:
redis:
image: redis:7.2.5
restart: always
command: redis-server /etc/redis.conf
volumes:
- /redis/redis.conf:/etc/redis.conf
- /redis/data:/data
ports:
- "6379:6379"