Linux 环境下 Redis 安装及配置

当前操作环境是基于 Alibaba Cloud Linux 3.2104 LTS 64位

Redis 官网下载 Redis 包

按自己所需下载,这里是使用redis-6.0.9进行操作。

Redis 官网上所有版本的包下载:
https://download.redis.io/releases/

更新源并安装相关依赖

yum update yum
yum -y install gcc gcc-c++ autoconf automake make

下载&编译&安装 Redis

#例如文件下载在 /opt 目录
cd /opt
wget http://download.redis.io/releases/redis-6.0.9.tar.gz
tar -zxvf redis-6.0.9.tar.gz
cd redis-6.0.9/
make &&make install
# 默认是安装到 /usr/local/bin

Redis 配置修改

#自定义存放redis.conf文件的,用于redis-server 指定加载该配置启动
#以及后面配置存放dump.rdb及log
mkdir -p /opt/redis
cp /opt/redis-6.0.9/redis.conf /opt/redis/
cd /opt/redis
vim redis.conf

按自身需求来修改 redis.conf 配置,以下是常用的

# 修改为 0.0.0.0 则任意IP可访问,生产环境慎用!
bind 0.0.0.0
# 守护进程,修改为 yes,即可后台运行
daemonize yes 
# 密码,设置后访问Redis必须输入密码
requirepass 1234567
# 监听的端口
port 6379

# 设置redis能够使用的最大内存
maxmemory 512mb
# 日志文件,默认为空,不记录日志,可以指定日志文件名
#logfile "redis.log"
logfile "/opt/redis/redis.log"

# 工作目录,默认是当前目录,也就是运行redis-server时的命令,日志、持久化等文件会保存在这个目录
#dir .
dir /opt/redis


Redis 常用命令

cd /usr/local/bin/
#启动
#这里就指定了加载`/opt/redis/redis.conf` 配置文件来启动服务
./redis-server /opt/redis/redis.conf

#停止
./redis-cli shutdown

#连接测试
redis-cli -p 6379
set name hello
get name
#列出所有key
keys *

#如果设置了密码,则需要授权
auth 你的密码
#auth 1234567

设置 Redis 开机启动

创建redis.service文件

vim /etc/systemd/system/redis.service

填入以下内容:

[Unit]
Description=redis-server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /opt/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli  shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target
#重新载入配置文件,让设置生效。
systemctl daemon-reload
#创建开机启动
systemctl enable redis.service

管理Redis 常用的systemctl 命令

# 启动
systemctl start redis
# 停止
systemctl stop redis
# 重启
systemctl restart redis
# 查看状态
systemctl status redis

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值