全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-12 6 分钟 ✍️ juanwangdev

Redis 简介与安装

Redis是开源的高性能键值数据库,支持多种数据结构,广泛用于缓存、会话、排行榜等场景。

Redis简介

什么是Redis

Bash
Redis = Remote Dictionary Server(远程字典服务)
- 开源(BSD协议)
- 基于内存运行
- 支持持久化
- 支持多种数据结构

核心定位

Bash
内存数据库 + 缓存系统 + 消息队列 + 分布式锁

主要应用场景

Bash
1. 缓存系统:加速数据访问
2. 会话存储:分布式Session
3. 排行榜:实时排名计算
4. 计数器:点赞、访问统计
5. 分布式锁:并发控制
6. 消息队列:异步任务处理
7. 社交网络:共同好友、推荐

Redis安装

Linux安装

方式一:源码编译

Bash
# 下载源码
wget https://download.redis.io/redis-stable.tar.gz
tar xzf redis-stable.tar.gz
cd redis-stable

# 编译
make

# 安装(可选)
make install PREFIX=/usr/local/redis

# 启动
./src/redis-server

方式二:包管理器

Bash
# Ubuntu/Debian
apt update
apt install redis-server

# CentOS/RHEL
yum install redis

# 启动服务
systemctl start redis
systemctl enable redis

macOS安装

Bash
# Homebrew
brew install redis

# 启动服务
brew services start redis

# 手动启动
redis-server

Windows安装

Bash
# 方式一:官方预编译版本
# 下载地址:https://github.com/microsoftarchive/redis/releases
# 解压后运行 redis-server.exe

# 方式二:WSL(推荐)
wsl --install
# 在WSL中按Linux方式安装

Docker安装

Bash
# 拉取镜像
docker pull redis:latest

# 启动容器
docker run -d --name redis \
  -p 6379:6379 \
  redis:latest

# 启动带配置的容器
docker run -d --name redis \
  -p 6379:6379 \
  -v /path/to/redis.conf:/etc/redis/redis.conf \
  redis:latest redis-server /etc/redis/redis.conf

# 启动带持久化的容器
docker run -d --name redis \
  -p 6379:6379 \
  -v /path/to/data:/data \
  redis:latest --appendonly yes

启动与连接

启动Redis

Bash
# 前台启动
redis-server

# 指定配置文件启动
redis-server /etc/redis/redis.conf

# 后台启动(配置文件设置daemonize yes)
redis-server /etc/redis/redis.conf

连接Redis

Bash
# 本地连接
redis-cli

# 指定地址和端口
redis-cli -h 127.0.0.1 -p 6379

# 带密码连接
redis-cli -h 127.0.0.1 -p 6379 -a your_password

# 指定数据库
redis-cli -n 1

基本配置

核心配置项

Bash
# redis.conf

# 绑定地址
bind 127.0.0.1

# 端口
port 6379

# 后台运行
daemonize yes

# 密码
requirepass your_password

# 数据库数量(0-15)
databases 16

# 日志级别
loglevel notice

# 日志文件
logfile /var/log/redis/redis.log

# 数据目录
dir /var/lib/redis

# 最大内存
maxmemory 4gb

# 内存淘汰策略
maxmemory-policy allkeys-lru

验证安装

测试连接

Bash
# 连接Redis
redis-cli

# 测试PING
127.0.0.1:6379> PING
PONG

# 测试基本操作
127.0.0.1:6379> SET test "hello"
OK
127.0.0.1:6379> GET test
"hello"
127.0.0.1:6379> DEL test
(integer) 1

# 查看信息
127.0.0.1:6379> INFO

查看版本

text
# 命令行查看
redis-cli --version
redis-server --version

# 连接后查看
INFO server
# redis_version:7.0.0

目录结构

text
/usr/local/redis/
├── bin/
│   ├── redis-server    # 服务端
│   ├── redis-cli       # 客户端
│   ├── redis-benchmark # 性能测试
│   ├── redis-check-rdb # RDB检查
│   └── redis-check-aof # AOF检查
├── etc/
│   └── redis.conf      # 配置文件
└── data/
    ├── dump.rdb        # RDB文件
    └── appendonly.aof  # AOF文件

常见问题

启动失败

text
# 查看日志
tail -f /var/log/redis/redis.log

# 检查端口占用
netstat -tlnp | grep 6379

# 检查权限
chown -R redis:redis /var/lib/redis

连接失败

text
# 检查服务状态
systemctl status redis

# 检查防火墙
firewall-cmd --list-ports

# 开放端口
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --reload

要点总结

  • Redis是高性能内存数据库,支持多种数据结构
  • Linux推荐源码编译或包管理器安装
  • Windows推荐WSL方式安装
  • Docker安装简单快捷,适合测试环境
  • redis.conf是核心配置文件,设置端口、密码、内存等
  • redis-cli连接Redis,PING测试连通性
  • INFO命令查看服务器详细信息

📝 发现内容有误?点击此处直接编辑

← 上一篇 Redis 核心特点
下一篇 → Redis列表命令
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库