第二章:Redis 安装与启动
📺 对应集数:p005
p07(第46集)
第二章:Redis 安装与启动
2.1 安装环境
- 操作系统:CentOS 7(Linux)
- Redis 版本:6.2.6
- 说明:Redis 官方没有提供 Windows 版本,网上 Windows 版本是微软自行编译的
2.2 安装步骤
# 1. 安装 GCC 编译器
yum install -y gcc
# 2. 上传 Redis 安装包到 /usr/local/src/ 目录,然后解压
tar -zxvf redis-6.2.6.tar.gz
# 3. 进入解压目录,编译安装
cd redis-6.2.6
make && make install
# 4. 安装完成后,可执行文件在 /usr/local/bin/ 目录下
# redis-server:服务端启动脚本
# redis-cli:命令行客户端
# redis-sentinel:哨兵
2.3 Redis 启动的三种方式
方式一:前台启动(默认)
redis-server
- 直接启动,日志输出在前台
- 缺点:终端关闭 Redis 也停止,不推荐
方式二:指定配置文件启动(后台运行)
# 备份原始配置文件
cp redis.conf redis.conf.bck
# 修改 redis.conf 中的关键配置:
关键配置项:
# 监听地址,改为 0.0.0.0 允许任意 IP 访问(默认 127.0.0.1)
bind 0.0.0.0
# 守护进程模式,改为 yes 后台运行(默认 no)
daemonize yes
# 设置访问密码(建议设置,避免裸奔)
requirepass 123321
# 其他常用配置
port 6379 # 端口,默认 6379
databases 16 # 数据库数量,默认 16(编号 0-15)
dir ./ # 工作目录
maxmemory 512mb # 最大内存限制
logfile "redis.log" # 日志文件名
# 指定配置文件启动
redis-server redis.conf
- 启动后无日志输出,可通过
ps -ef | grep redis验证是否运行
方式三:开机自启(systemd 服务)
# 1. 创建系统服务文件
vim /etc/systemd/system/redis.service
服务文件内容:
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.6/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 2. 重新加载服务
systemctl daemon-reload
# 3. 管理 Redis 服务
systemctl start redis # 启动
systemctl stop redis # 停止
systemctl restart redis # 重启
systemctl status redis # 查看状态
systemctl enable redis # 设置开机自启