01 Redis概念
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
Redis可以以键值对的形式作为一种NoSql数据库,因为Redis将所有数据都存放在内存中,所以其可达到的数据读写性能非常惊人。
除了性能惊人,Redis还具备可持久化的特点,它可以将内存中的数据利用快照和日志的形式保存到磁盘上。另外,Redis还提供了键过期,发布订阅,事务,流水线等其他功能。
02 Redis重要特性
-
读写速度块
Redis将所有数据都存放在内存中
Redis使用C语言编写,接近底层硬件性能好
Redis使用单线程架构,减少CPU切换的性能消耗 -
支持多种数据结构
基于键值对的数据结构服务器
支持数据结构:字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets)等 -
功能丰富,可扩展性强
Redis提供键过期功能,可以实现缓存
Redis提供发布订阅功能,可以作为消息系统
Redis提供管道Pipeline功能,客户端可以将一批命令一次性传到Redis中,减少网络开销 -
简单稳定
代码简单,开源版本中代码行仅在5万行左右
处理模型简单,采用单线程模型,所以使得Redis服务端处理模型变得简单
不依赖操作系统中的任何类库,独立性好 -
可持久化
RDB方式
AOF方式 -
主从复制
-
高可用和分布式
Redis哨兵
Redis集群
03 Redis应用场景
支撑功能 | 应用场景 |
---|---|
键过期功能 | 缓存session会话,缓存mysql数据 |
列表,有序集合 | 热度排名排行榜,发布时间排行榜 |
集合 | 共同好友,共同兴趣,广告投放 |
计数器 | 文章浏览量,视频播放量,评论留言量 |
发布订阅 | 消息队列系统,缓存ELK日志收集 |
04 Redis安装准备
好的目录规划,有利于软件系统的维护与升级
# redis下载目录
/download/
# redis 安装目录
/opt/redis_cluster/redis_[port]/{
conf} # 节点配置
/opt/redis_cluster/redis_[port]/{
logs} # 节点日志
/opt/redis_cluster/redis_[port]/{
pid} # 节点进程
# redis 数据目录
/data/redis_cluster/redis_[port]/redis_[port].rdb
# redis 运维脚本
/root/scipts/redis_shell.sh
05 Redis安装部署
5.1 创建目录
mkdir download
mkdir -p /data/redis_cluster/redis_6379 # 数据目录
mkdir -p /opt/redis_cluster/redis_6379/{
conf,pid,logs}
# tree /opt/ 得到如下目录
/opt/
└── redis_cluster
└── redis_6379
├── conf
├── logs
└── pid
5.2 安装Redis
cd /download
wget https://download.redis.io/releases/redis-6.0.6.tar.gz
tar -zxvf redis-6.0.6.tar.gz -C /opt/redis_cluster/
# 创建软连接:使用别名避免版本变更问题,便于脚本路径识别
ln -s /opt/redis_cluster/redis-6.0.6 /opt/redis_cluster/redis
# make 安装
cd redis
make # 生成二进制Redis命令
make install # 将二进制命令放入/usr/local/bin目录下提供给用户使用
可以查看/usr/local/bin
目录下的redis命令,这也是安装redis的主要目的
root@master:/opt/redis_cluster/redis$ ll /usr/local/bin/
total 38484
-rwxr-xr-x 1 root root 5452064 Aug 4 08:08 redis-benchmark*
-rwxr-xr-x 1 root root 9504128 Aug 4 08:08 redis-check-aof*
-rwxr-xr-x 1 root root 9504128 Aug 4 08:08 redis-check-rdb*
-rwxr-xr-x 1 root root 5381912 Aug 4 08:08 redis-cli*
lrwxrwxrwx 1 root root 12 Aug 4 08:08 redis-sentinel -> redis-server*
-rwxr-xr-x 1 root root 9504128 Aug 4 08:08 redis-server*
5.3 Redis默认配置
安装Redis的时候并没有自动生成配置文件等其他内容,采用如下方式生成默认配置文件
# 进入到安装目录的utils目录下,运行install_server.sh脚本
cd /opt/redis_cluster/redis/utils
./install_server.sh
如果报错,可能是由于版本原有,Redis-6.0之后的版本会出现如下问题
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
解决方法,在install_server.sh
脚本中注释掉对应语句
vim install_server.sh
# 注释以下语句
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
#echo "This systems seems to use systemd."
#echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
#exit 1
#fi
生成完成之后,输出如下内容,可以看到默认配置文件的路径为/etc/redis/6379.conf
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Success!
Starting Redis server...
Installation successful!
5.4 配置Redis
5.3节中的默认配置文件内容过于全面,很多部分在实际使用中不一定需要,所以在本届介绍较为简单的配置内容。
进入配置文件目录并创建配置文件
cd /opt/redis_cluster/redis_6379/conf/
vim redis_6379.conf
在配置文件中配置如下内容
### 以守护进程模式启动
daemonize yes
### 绑定主机地址,一般绑定为内网地址
bind 172.16.255.131
### 监听端口
port 6379
### pid文件和log文件目录的路径
pidfile /opt/redis_cluster/redis_6379/pid/redis_6379.pid
logfile /opt/redis_cluster/redis_6379/logs/redis_6379.log
### 设置数据库的数量,默认数据库数量为0
databases 16
### 指定本地持久化文件的文件名,默认是dump.rdb
dbfilename redis_6379.rdb
### 本地数据库的目录
dir /data/redis_cluster/redis_6379
06 启动Redis
启动与登录Redis
# 启动Redis服务命令
redis-server /opt/redis_cluster/redis_6379/conf/redis_6379.conf
# 登录Redis服务命令
redis-cli
redis-cli -h 172.16.255.101 # your ip address
检查Redis是否启动成功
ps -ef|grep redis # 查看进程
tail -f /opt/redis_cluster/redis_6379/logs/redis_6379.log # 查看Redis服务日志
关闭Redis服务:登录Redis然后输入SHUTDOWN
root@master:$ redis-cli
127.0.0.1:6379> SHUTDOWN
# 或者直接执行如下命令
redis-cli shutdown