Redis下载与安装及启用
Redis下载
进入官网找到下载地址:https://redis.io/download
历史版本下载地址: http://download.redis.io/releases/
下载安装
# 指定redis存放位置
cd /usr/local/src/
# 下载安装6.2.14版本
wget https://download.redis.io/releases/redis-6.2.14.tar.gz
# 解压
tar -xvf redis-6.2.14.tar.gz安装依赖
CentOS7 安装依赖
# 本地缓存数据清理
yum clean all
# 从软件源服务器下载最新的软件包列表(元数据)并保存到本地缓存中
yum makecache
# 一键安装 C/C++ 编译环境和 Tcl 工具
yum -y install gcc gcc-c++ make tcl
# (可选安装) 安装新版本gcc
# yum -y install centos-release-scl
# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
# source /opt/rh/devtoolset-9/enable
# echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
# 查看GCC版本
gcc --versionUbuntu22.04安装依赖
apt install build-essential gcc g++ make tcl -y编译安装 Redis
# 进入 Redis 源码目录
cd /usr/local/src/redis-6.2.14
# 编译并安装
make && make PREFIX=/usr/local/redis install
# 创建 Redis 工作目录
mkdir -p /usr/local/redis/{etc,logs,data}
# 过滤并生成精简配置文件
grep -E -v "^$|^#" redis.conf > /usr/local/redis/etc/redis.conf启动服务
因为没有设置环境变量所以指令需要去到目录文件执行
# 进入目录
cd /usr/local/redis/bin
# 启动服务
./redis-server使用客户端程序
启动 Redis 命令行客户端
# 进入目录
cd /usr/local/redis/bin
# 启动命令行客户端
./redis-cli连接 Redis 服务器
# 连接远程或非默认配置的 Redis 服务器
./redis-cli -h 指定ip -p 指定端口 -a 指定密码服务器操作
常用配置
在目录 /usr/local/redis/etc 下有一个 redis.conf 的配置文件。我们上面启动方式就是执行了该配置文件的配置运行的。我们可以通过 cat、vim、less 等 linux 内置的读取命令读取该文件。
这里列举下比较重要的配置项:
服务器端
查看是否启动
ps -ef|grep redis启动
前台启动
# 进入目录
cd /usr/local/redis/bin
# 启动服务
./redis-server后台启动
# 修改/usr/local/redis/etc/redis.conf文件
daemonize yes #前台启动,改后台启动
# 进入目录
cd /usr/local/redis
# 指定配置文件 启动
./bin/redis-server ./etc/redis.conf关闭
# 进入目录
cd /usr/local/redis/bin
# 关闭
./redis-cli shutdown设置开机自启
添加systemctl启动项
# 编辑配置文件
vim /etc/systemd/system/redis.service
# 添加以下内容
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
PIDFile=/run/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStartPost=/bin/bash -c 'for i in {1..20}; do [ -f /run/redis_6379.pid ] && exit 0; sleep 0.1; done; exit 1'
ExecStop=/usr/local/redis/bin/redis-cli shutdown
ExecReload=/usr/local/redis/bin/redis-cli CONFIG REWRITE
Restart=on-failure
PrivateTmp=true
[Install]
WantedBy=multi-user.target设置刷新配置和启动服务
# 重新加载 systemd
systemctl daemon-reload
# 停掉当前手动运行的 Redis
pkill redis-server
# 用 systemctl 启动
systemctl start redis
# 设置开机自启
systemctl enable redis
# 检查状态
systemctl status redis设置环境变量永久生效
# 编辑文件
vim /etc/profile
# 在末尾添加
export PATH=$PATH:/usr/local/redis/bin
# 重新加载系统环境变量配置文件,立即生效
source /etc/profile客户端
启动
# 进入目录
cd /usr/local/redis
# 启动
./bin/redis-cli
# 启动后样子
127.0.0.1:6379>关闭
方式一:指令ctrl+c
方式二:执行指令quit
方式三:执行指令exit
设置远程访问
# 修改/usr/local/redis/etc/redis.conf文件
bind 127.0.0.1 #不注释只能本机访问,注释允许任何IP连接
protected-mode no #允许远程访问
# 在文件中添加这一行,设置远程登录密码
requirepass 你的密码
# 重启服务
# 尝试无密码关闭(如果原来没密码)
./redis-cli shutdown
# 如果已经有密码,用-a指定
./redis-cli -a "你的密码" shutdown
# 启动使用完整路径
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf推荐配置文件写法
# 编辑文件
vim /usr/local/redis/etc/redis.conf
# 内容
#bind 127.0.0.1 -::1
protected-mode no
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/usr/local/redis/logs/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /usr/local/redis/data
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes
# 修改完配置文件后需要重启服务
systemctl restart redisRedis客户端
Redis客户端下载
原创
Redis下载与安装及启用
本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。
评论交流
欢迎留下你的想法