【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
相关推荐
奇特認4 小时前
mysql 集群技术 3.高可用之 MHA
数据库·mysql
Bioinfo Guy6 小时前
高分Panel复现系列|转录因子网络太乱?用模块框把重点圈出来
数据库·生物信息学·生信可视化
ltl8 小时前
Disaggregated DB 合集:Socrates、PolarDB、Taurus 的共同模式
数据库
Blossom i10 小时前
大数据预处理与采集实验一:使用Python操作MySQL数据库
数据库·mysql
雾时之林11 小时前
Linux--软件管理、源码包安装
linux·服务器·数据库
thesky12345611 小时前
智能体面试准备(二十六):Agent 成本工程——模型路由、缓存、预算控制与大小模型分工
缓存·llmops·智能体·模型路由·成本工程·预算控制·小模型分工
老纪的技术唠嗑局12 小时前
超级干货分享:中小企业使用 OceanBase 实践经验汇总
数据库
CLOUD ACE13 小时前
谷歌云代理|零售商Target 如何利用 Spanner Graph 提升零售发现体验并将数据库维护成本降低 50%
数据库·零售
AI大模型-小雄13 小时前
Codex写分页接口为什么越翻越慢?用Cursor Pagination解决重复与漏数据
大数据·数据库·elasticsearch·搜索引擎·chatgpt·后端开发·codex
少晓年13 小时前
从 MySQL 迁移到人大金仓 KingbaseES:完整指南与实践
数据库·mysql