【Redis】3.Redis安装与命令行客户端

文章目录

  • [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 操作系统,而 RedisLinux 操作系统上的表现已经得到实践的证明。

当然 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.1bind 0.0.0.0
  • 修改 protected-mode yesprotected-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/ , 藏的太深了, 不方便使用。我们通过符号链接, 把需要用到的关键内容设置到方便使用的目录中:

  1. 针对可执行程序设置符号链接
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
  1. 针对配置文件设置符号链接
shell 复制代码
cd /etc/
ln -s /etc/opt/rh/rh-redis5/ ./redis

1.5.2.3 修改配置文件
  1. 设置 ip 地址
mysql 复制代码
bind 0.0.0.0
  1. 关闭保护模式
mysql 复制代码
protected-mode no
  1. 启动守护进程
mysql 复制代码
daemonize yes
  1. 设置工作目录

先创建工作目录

mysql 复制代码
mkdir -p /var/lib/redis

再在配置文件中, 设置工作目录

mysql 复制代码
dir /var/lib/redis
  1. 设置日志目录

先创建日志目录

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-serverpid

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-serverRedis 服务器程序,其余的几个例如:redis-check-aofredis-check-rdbredissentinel也都是 redis-server 的软链接。redis-check-aof 是修复 AOF 文件用的工具,同理 redischeck-rdb是修复 RDB 文件的工具,redis-sentinelRedis 哨兵程序。

redis-cli 是在我们学习阶段需要频繁用到的一个命令行客户端程序,随后做介绍。

redis-benchmark 用于对 Redis 做性能基准测试的工具。

redis-shutdown 是用于停止 Redis 的专用脚本。
相比较直接使用这些命令,我们更建议使用上一节讲过的,systemd 托管的方式来进行 Redis 的启动 / 停止。


1.5.10 配置文件

/etc/redis.confRedis 服务器的配置文件。

/etc/redis-sentinel.confRedis Sentinel 的配置文件。

mysql 复制代码
/etc/redis-sentinel.conf
/etc/redis.conf

1.5.11 持久化文件存储目录

Redis 持久化生产的 RDBAOF 文件都默认生成于该目录下。后边章节我们讲到持久化时会观察这边持久化的一些现象。

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"

这里有两点要注意:

  1. 由于我们连接的 Redis 服务位于 127.0.0.1,端口也使用的是默认的 6379 端口,所以可以省略 -h { host } -p { port }
  2. Redis 是学习 Redis 的重要工具,后续的大量章节都是用它来做讲解。

有关 redis-cli 提供的更为强大的功能将在后续章节做详细介绍。


1.6 本章重点回顾

  1. Redis8 个特性:速度快、基于键值对的数据结果服务器、功能丰富、简单稳定、客户端语言多、持久化、主从复制、支持高可用和分布式。
  2. Redis 并不是万金油,有些场景不适合使用 Redis 进行开发。
  3. 开发运维结合以及阅读源码是用好 Redis 的重要方法。
  4. 安装启动 Redis
  5. 根据需求选择稳定版本的 Redis
相关推荐
凌虚2 小时前
基于 PostgreSQL WAL 构建 CDC 系统:原理与工程实现
数据库·后端·postgresql
BullSmall2 小时前
PostgreSQL 14 pg_dumpall 完整备份指南
数据库·postgresql·oracle
一只枫林3 小时前
MySQL内、外连接知识点汇总
java·前端·数据库
暮暮祈安3 小时前
Celery 新手入门指南
java·数据库·python·flask·httpx
xiaowang1234shs4 小时前
怪兽轻断食技术深度测评:从断食计时引擎到AI识别算法的工程实践解析
数据库·人工智能·算法·macos·机器学习·p2p·visual studio
Quincy_Freak4 小时前
银河麒麟 aarch64 环境下轻量 SQLite 管理方案实践:SQLiteGo 落地体验
数据库·sqlite·sqlitego
灵机一物5 小时前
企业选型参考:2026 CXL 内存扩展方案六强测评与落地指南
服务器·网络·数据库·人工智能
秋风渡.5 小时前
MySQL_数据类型知识
数据库·mysql
Season4505 小时前
Redis命令 (generic即通用命令)
数据库·redis·bootstrap