简介

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions, and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster. Learn more →

部署Redis

源码安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 安装基础包
yum install gcc make -y

# 下载资源包
cd /usr/local/src && wget https://download.redis.io/releases/redis-6.2.6.tar.gz

# 解压
tar xzf redis-6.2.6.tar.gz && cd redis-6.2.6

# 编译安装
make && make install
# PS:
# 问题:由于未安装gcc,导致第一次make失败,在安装gcc安装后,再次make报错fatal error: jemalloc/jemalloc.h: No such file or directory
# 解决:该问题是由于第一轮make失败有残留导致,需清理后再编译,命令:make distclean && make && make install

# 生成配置文件
mkdir /etc/redis && cp redis.conf /etc/redis/

# 创建数据持久化目录
mkdir /var/lib/redis

配置Redis

修改 /etc/redis/redis.conf 配置文件如下参数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 服务监听地址
bind * -::*

# 启用密码认证及默认密码
requirepass redis@Inspur.1

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

# RDB持久化规则
save 3600 1
save 300 100
save 60 10000

# 数据持久化目录
dir /var/lib/redis

启动Redis

创建 /usr/lib/systemd/system/redis-server.service 配置文件,内容如下:

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Redis Server
After=network.target

[Service]
Restart=always
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf

[Install]
WantedBy=multi-user.target

重新加载systemctl配置文件,启动redis服务。

1
2
3
systemctl daemon-reload
systemctl start redis-server
systemctl enable redis-server

测试Redis

远程登录redis并验证可用性。

1
2
3
4
5
root@vsphere162: ~ # redis-cli -h vsphere161 -a redis@Inspur.1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
vsphere161:6379> ping
PONG
vsphere161:6379> exit

参考文档