文章目录
- [1. 初识Redis](#1. 初识Redis)
-
- [1.5 安装并启动 Redis](#1.5 安装并启动 Redis)
-
- [1.5.1 Ubuntu 安装 redis](#1.5.1 Ubuntu 安装 redis)
-
- [1.5.1.1 使用 apt 安装](#1.5.1.1 使用 apt 安装)
- [1.5.1.2 支持远程连接](#1.5.1.2 支持远程连接)
- [1.5.1.3 启动 Redis 服务](#1.5.1.3 启动 Redis 服务)
- [1.5.1.4 停止 Redis 服务](#1.5.1.4 停止 Redis 服务)
- [1.5.1.5 重启 Redis 服务](#1.5.1.5 重启 Redis 服务)
- [1.5.2 Centos7 安装 redis](#1.5.2 Centos7 安装 redis)
-
- [1.5.2.1 使用 yum 安装](#1.5.2.1 使用 yum 安装)
- [1.5.2.2 创建符号链接](#1.5.2.2 创建符号链接)
- [1.5.2.3 修改配置文件](#1.5.2.3 修改配置文件)
- [1.5.2.4 启动 redis](#1.5.2.4 启动 redis)
- [1.5.2.5 停止 redis](#1.5.2.5 停止 redis)
- [1.5.3 Centos8 安装 redis](#1.5.3 Centos8 安装 redis)
-
- [1.5.3.1 使用 yum 安装](#1.5.3.1 使用 yum 安装)
- [1.5.4 通过 systemd 管理 Redis](#1.5.4 通过 systemd 管理 Redis)
- [1.5.5 支持远程连接](#1.5.5 支持远程连接)
- [1.5.6 通过 systemd 控制 Redis](#1.5.6 通过 systemd 控制 Redis)
- [1.5.7 停止 Redis 服务](#1.5.7 停止 Redis 服务)
- [1.5.8 重启 Redis 服务](#1.5.8 重启 Redis 服务)
- [1.5.9 Redis 重要文件及作用](#1.5.9 Redis 重要文件及作用)
- [1.5.10 配置文件](#1.5.10 配置文件)
- [1.5.11 持久化文件存储目录](#1.5.11 持久化文件存储目录)
- [1.5.12 日志文件目录](#1.5.12 日志文件目录)
- [1.5.13 Redis 命令行客户端](#1.5.13 Redis 命令行客户端)
- [1.6 本章重点回顾](#1.6 本章重点回顾)
1. 初识Redis
1.5 安装并启动 Redis
我们选择
5.0版本,原因是5.0已经支持了大部分的功能特性,而且相比较于7.0版本,更容易进行安装使用。
Redis的官方并不支持微软的Windows操作系统,因为Redis的许多特性都是和操作系统相关的,所以支持Windows会增加维护成本,而且更重要的是大部分公司都在使用Linux操作系统,而Redis在Linux操作系统上的表现已经得到实践的证明。当然
Redis作为一款优秀的开源技术,还是吸引到微软公司的注意,微软公司的开源技术组在Github上维护了一个Redis分支:https://github.com/MSOpenTech/redis。不过还是强烈建议大家在
Linux上使用Redis。在这里,我使用
Ubuntu下安装Redis。
1.5.1 Ubuntu 安装 redis
1.5.1.1 使用 apt 安装
mysql
apt install redis -y

1.5.1.2 支持远程连接
修改
/etc/redis/redis.conf
- 修改
bind 127.0.0.1为bind 0.0.0.0- 修改
protected-mode yes为protected-mode no
mysql
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1 # 注释掉这行
bind 0.0.0.0 # 添加这行
protected-mode no # 把 yes 改成 no
注意:
如果
Linux服务器没打开IPV6,就把bind 0.0.0.0 :::后面的三个冒号去掉。或者自己查询如何开启
Linux服务器的IPV6服务。
1.5.1.3 启动 Redis 服务
mysql
service redis-server start
退出可以
Ctrl+D
1.5.1.4 停止 Redis 服务
mysql
service redis-server stop
1.5.1.5 重启 Redis 服务
mysql
service redis-server restart
1.5.2 Centos7 安装 redis
1.5.2.1 使用 yum 安装
首先安装
scl源, 再安装redis
shell
yum install centos-release-scl-rh
yum install rh-redis5-redis
1.5.2.2 创建符号链接
默认安装的目录为
/opt/rh/rh-redis5/root/usr/bin/, 藏的太深了, 不方便使用。我们通过符号链接, 把需要用到的关键内容设置到方便使用的目录中:
- 针对可执行程序设置符号链接
mysql
cd /usr/bin
ln -s /opt/rh/rh-redis5/root/usr/bin/redis-server ./redis-server
ln -s /opt/rh/rh-redis5/root/usr/bin/redis-sentinel ./redis-sentinel
ln -s /opt/rh/rh-redis5/root/usr/bin/redis-cli ./redis-cli
- 针对配置文件设置符号链接
shell
cd /etc/
ln -s /etc/opt/rh/rh-redis5/ ./redis
1.5.2.3 修改配置文件
- 设置
ip地址
mysql
bind 0.0.0.0
- 关闭保护模式
mysql
protected-mode no
- 启动守护进程
mysql
daemonize yes
- 设置工作目录
先创建工作目录
mysql
mkdir -p /var/lib/redis
再在配置文件中, 设置工作目录
mysql
dir /var/lib/redis
- 设置日志目录
先创建日志目录
mysql
mkdir -p /var/log/redis/
再在配置文件中, 设置日志目录
mysql
logfile /var/log/redis/redis-server.log
1.5.2.4 启动 redis
mysql
redis-server /etc/redis/redis.conf
1.5.2.5 停止 redis
先查看到 redis-server 的 pid
mysql
ps aux | grep redis
然后通过 kill 命令直接杀死 redis 进程
mysql
kill 进程id
1.5.3 Centos8 安装 redis
1.5.3.1 使用 yum 安装
Redis 5.0被包含在CentOS 8源仓库中。想要安装它,直接以root或者其他有sudo权限的用户身份运行下面的命令:
mysql
yum install -y redis
1.5.4 通过 systemd 管理 Redis
一旦安装完成,我们可以将
redis设置为开机自动启动:
shell
root@yudukai:~# systemctl enable redis-server
Synchronizing state of redis-server.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable redis-server
root@yudukai:~#
1.5.5 支持远程连接
默认情况下,
Redis只绑定在127.0.0.1接口上,即只允许从127.0.0.1(localhost)上进行连接Redis服务,但在后面,我们需要在Windows上连接云服务器的Redis进行一系列的操作,所以需要配置允许Redis接受远程访问,修改Redis的配置文件:/etc/redis.conf,
- 定位到
bind 127.0.0.1开头的一行,修改为bind 0.0.0.0以添加全接口支持:- 关闭保护模式,
protected-mode no
mysql
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all the network interfaces available on the server.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1 ::1
#
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
# internet, binding to all the interfaces is dangerous and will expose the
# instance to everybody on the internet. So by default we uncomment the
# following bind directive, that will force Redis to listen only into
# the IPv4 loopback interface address (this means Redis will be able to
# accept connections only from clients running into the same computer it
# is running).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# bind 127.0.0.1 # 注释掉这行
bind 0.0.0.0 # 添加这行
protected-mode no # 把 yes 改成 no
1.5.6 通过 systemd 控制 Redis
启动
Redis服务
mysql
root@yudukai:~# systemctl start redis
验证
Redis是否正确地监听6379端口:
mysql
root@yudukai:~# netstat -nlpt | grep 6379
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 2704903/redis-serve
root@yudukai:~#
1.5.7 停止 Redis 服务
mysql
[root@host ~]# systemctl stop redis
1.5.8 重启 Redis 服务
mysql
[root@host ~]# systemctl restart redis
1.5.9 Redis 重要文件及作用
启动/停止命令或脚本
mysql
/usr/bin/redis-benchmark
/usr/bin/redis-check-aof -> /usr/bin/redis-server
/usr/bin/redis-check-rdb -> /usr/bin/redis-server
/usr/bin/redis-cli
/usr/bin/redis-sentinel -> /usr/bin/redis-server
/usr/bin/redis-server
/usr/libexec/redis-shutdown
redis-server是Redis服务器程序,其余的几个例如:redis-check-aof、redis-check-rdb、redissentinel也都是redis-server的软链接。redis-check-aof是修复AOF文件用的工具,同理redischeck-rdb是修复RDB文件的工具,redis-sentinel是Redis哨兵程序。
redis-cli是在我们学习阶段需要频繁用到的一个命令行客户端程序,随后做介绍。
redis-benchmark用于对Redis做性能基准测试的工具。
redis-shutdown是用于停止Redis的专用脚本。
相比较直接使用这些命令,我们更建议使用上一节讲过的,systemd托管的方式来进行Redis的启动 / 停止。
1.5.10 配置文件
/etc/redis.conf是Redis服务器的配置文件。
/etc/redis-sentinel.conf是Redis Sentinel的配置文件。
mysql
/etc/redis-sentinel.conf
/etc/redis.conf
1.5.11 持久化文件存储目录
Redis持久化生产的RDB和AOF文件都默认生成于该目录下。后边章节我们讲到持久化时会观察这边持久化的一些现象。
mysql
/var/lib/redis/
1.5.12 日志文件目录
/var/log/redis/目录下会保存Redis运行期间生产的日志文件,默认按照天进行分割,并且会将一定日期的日子文件使用gzip格式压缩保存。可以使用任意文本编辑器打开,后边章节我们会通过日志来观察一些现象。
mysql
/var/log/redis/
1.5.13 Redis 命令行客户端
现在我们已经启动了
Redis服务,下面将介绍如何使用redis-cli连接、操作Redis服务。客户端和服务端的交互过程如图所示。
Redis 客户端与服务端的交互过程

redis-cli可以使用两种方式连接Redis服务器。
第一种是交互式方式:通过
redis-cli -h { host } -p { port }的方式连接到Redis服务,后续所有的操作都是通过交互式的方式实现,不需要再执行redis-cli了。例如:
mysql
root@yudukai:~# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set key hello
OK
127.0.0.1:6379> get key
"hello"
127.0.0.1:6379> exit
root@yudukai:~#
第二种是命令方式:
用
redis-cli -h { host } -p { port } { command }就可以直接得到命令的返回结果.例如:
mysql
root@yudukai:~# redis-cli -h 127.0.0.1 -p 6379 ping
PONG
root@yudukai:~# redis-cli -h 127.0.0.1 -p 6379 set key hello
OK
root@yudukai:~# redis-cli -h 127.0.0.1 -p 6379 get key
"hello"
这里有两点要注意:
- 由于我们连接的
Redis服务位于127.0.0.1,端口也使用的是默认的6379端口,所以可以省略-h { host } -p { port }。Redis是学习Redis的重要工具,后续的大量章节都是用它来做讲解。有关
redis-cli提供的更为强大的功能将在后续章节做详细介绍。
1.6 本章重点回顾
Redis的8个特性:速度快、基于键值对的数据结果服务器、功能丰富、简单稳定、客户端语言多、持久化、主从复制、支持高可用和分布式。Redis并不是万金油,有些场景不适合使用Redis进行开发。- 开发运维结合以及阅读源码是用好
Redis的重要方法。- 安装启动
Redis。- 根据需求选择稳定版本的
Redis。