文章目录
- 1. 初识Redis
-
- 1.5 安装并启动 Redis
-
- 1.5.1 Ubuntu 安装 redis
-
- 1.5.1.1 使用 apt 安装
- 1.5.1.2 支持远程连接
- 1.5.1.3 启动 Redis 服务
- 1.5.1.4 停止 Redis 服务
- 1.5.1.5 重启 Redis 服务
- 1.5.2 Centos7 安装 redis
-
- 1.5.2.1 使用 yum 安装
- 1.5.2.2 创建符号链接
- 1.5.2.3 修改配置文件
- 1.5.2.4 启动 redis
- 1.5.2.5 停止 redis
- 1.5.3 Centos8 安装 redis
-
- 1.5.3.1 使用 yum 安装
- 1.5.4 通过 systemd 管理 Redis
- 1.5.5 支持远程连接
- 1.5.6 通过 systemd 控制 Redis
- 1.5.7 停止 Redis 服务
- 1.5.8 重启 Redis 服务
- 1.5.9 Redis 重要文件及作用
- 1.5.10 配置文件
- 1.5.11 持久化文件存储目录
- 1.5.12 日志文件目录
- 1.5.13 Redis 命令行客户端
- 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 安装
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
# 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 服务
service redis-server start
退出可以Ctrl+D
1.5.1.4 停止 Redis 服务
service redis-server stop
1.5.1.5 重启 Redis 服务
service redis-server restart
1.5.2 Centos7 安装 redis
1.5.2.1 使用 yum 安装
首先安装 scl 源, 再安装 redis
yum install centos-release-scl-rh
yum install rh-redis5-redis
1.5.2.2 创建符号链接
默认安装的目录为 /opt/rh/rh-redis5/root/usr/bin/ , 藏的太深了, 不方便使用。我们通过符号链接, 把需要用到的关键内容设置到方便使用的目录中:
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
cd /etc/
ln -s /etc/opt/rh/rh-redis5/ ./redis
1.5.2.3 修改配置文件
bind 0.0.0.0
protected-mode no
daemonize yes
先创建工作目录
mkdir -p /var/lib/redis
再在配置文件中, 设置工作目录
dir /var/lib/redis
先创建日志目录
mkdir -p /var/log/redis/
再在配置文件中, 设置日志目录
logfile /var/log/redis/redis-server.log
1.5.2.4 启动 redis
redis-server /etc/redis/redis.conf
1.5.2.5 停止 redis
先查看到 redis-server 的 pid
ps aux | grep redis
然后通过 kill 命令直接杀死 redis 进程
kill 进程id
1.5.3 Centos8 安装 redis
1.5.3.1 使用 yum 安装
Redis 5.0 被包含在 CentOS 8 源仓库中。想要安装它,直接以 root 或者其他有 sudo 权限的用户身份运行下面的命令:
yum install -y redis
1.5.4 通过 systemd 管理 Redis
一旦安装完成,我们可以将 redis 设置为开机自动启动:
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
# 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 服务
root@yudukai:~# systemctl start redis
验证 Redis 是否正确地监听 6379 端口:
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 服务
[root@host ~]# systemctl stop redis
1.5.8 重启 Redis 服务
[root@host ~]# systemctl restart redis
1.5.9 Redis 重要文件及作用
启动/停止命令或脚本
/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 的配置文件。
/etc/redis-sentinel.conf
/etc/redis.conf
1.5.11 持久化文件存储目录
Redis 持久化生产的 RDB 和 AOF 文件都默认生成于该目录下。后边章节我们讲到持久化时会观察这边持久化的一些现象。
/var/lib/redis/
1.5.12 日志文件目录
/var/log/redis/ 目录下会保存 Redis 运行期间生产的日志文件,默认按照天进行分割,并且会将一定日期的日子文件使用 gzip 格式压缩保存。
可以使用任意文本编辑器打开,后边章节我们会通过日志来观察一些现象。
/var/log/redis/
1.5.13 Redis 命令行客户端
现在我们已经启动了 Redis 服务,下面将介绍如何使用 redis-cli 连接、操作 Redis 服务。客户端和服务端的交互过程如图所示。
Redis 客户端与服务端的交互过程

redis-cli 可以使用两种方式连接 Redis 服务器。
第一种是交互式方式:
通过 redis-cli -h { host } -p { port } 的方式连接到 Redis 服务,后续所有的操作都是通过交互式的方式实现,不需要再执行 redis-cli 了。
例如:
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 } 就可以直接得到命令的返回结果.
例如:
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-cli 提供的更为强大的功能将在后续章节做详细介绍。





