1.1 Redis的详细安装步骤
Redis能够兼容绝大部分的POSIX系统,例如Linux、OS x、OpenBSD、NetBSD和FreeBSD,其中比较典型的是Linux操作系统(例如CentOs、Redhat、Ubuntu、Debian、OS X等)。在Linux安装软件通常有两种方法,第一种是通过各个操作系统的软件管理软件进行安装,例如CentOS有yum管理工具,Ubuntu有apt。但是由于Redis的更新速度相对较快,而这些管理工具不一定能更新到最新的版本,同时前面提到Redis的安装本身不是很复杂,所以一般推荐使用第二种方式:源码的方式进行安装,整个安装只需以下六步即可完成,以6.2.5版本为例:
第一步:下载要安装的Redis源码包
现在opt目录下创建redis文件夹 并进入redis文件夹内
[root@iZuf6a2fzgjflg7bp7q2prZ redis]# wget https://download.redis.io/releases/redis-6.2.5.tar.gz
下载完成如下图

第二步:解压缩Redis源码包
[root@iZuf6a2fzgjflg7bp7q2prZ redis]# tar xzf redis-6.2.5.tar.gz
查看是否解压成功 会多出一个redis-6.2.5文件夹就代表解压缩成功

第三步:建立一个redis目录的软连接,指向redis-3.0.7
[root@iZuf6a2fzgjflg7bp7q2prZ redis]# ln -s redis-6.2.5 redis
第四步:进入redis
[root@iZuf6a2fzgjflg7bp7q2prZ redis]# cd redis
第五步:编译(编译之前确保操作系统已经安装gcc)
gcc的安装命令:yum install gcc-c++
[root@iZuf6a2fzgjflg7bp7q2prZ redis]# make
编译成功如下图:

第六步安装:
[root@iZuf6a2fzgjflg7bp7q2prZ redis]# make install
安装成功如下图

这里有两点要注意:
第一点第3步中建立了一个redis目录的软链接,这样做是为了不把redis目录固定在指定版本上,有利于Redis未来版本升级,算是安装软件的一种好习惯。
第二点第6步中的安装是将Redis的相关运行文件放到/usr/local/bin/下,这样就可以在任意目录下执行Redis的命令。例如安装后,可以在任何目录执行redis-cli-v查看Redis的版本。

1.2 配置、启动、操作、关闭Redis
Redis安装之后,src和/usr/local/bin目录下多了几个以redis开头可执行文件,我们称之为Redis Shell,这些可执行文件可以做很多事情,例如可以启动和停止Redis、可以检测和修复Redis的持久化文件,还可以检测Redis的性能。
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# cd /usr/local/bin/
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# ll
总用量 18972
-rw-r--r-- 1 root root 92 8月 25 11:54 dump.rdb
-rwxr-xr-x 1 root root 4828456 8月 25 11:27 redis-benchmark
lrwxrwxrwx 1 root root 12 8月 25 11:27 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root 12 8月 25 11:27 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 5002080 8月 25 11:27 redis-cli
lrwxrwxrwx 1 root root 12 8月 25 11:27 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 9490600 8月 25 11:27 redis-server
| 可执行文件 | 说明 |
|---|---|
| redis-server | 启动Redis |
| redis-cli | Redis命令行客户端 |
| redis-benchmark | Redis基准测试工具 |
| redis-check-aof | Redis-AOF持久化文件检测和修复工具 |
| redis-check-rdb | Redis-RDB持久化文件检测和修复工具 |
| redis-sentinel | 启动redis-sentinel |
1.2.1 启动Redis
启动Redis有三种方式:默认配置、运行配置、配置文件启动
- 默认配置启动
这种方法会使用Redis的默认配置来启动,下面就是redis-server执行后输出的相关日志:
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-server
12473:C 25 Aug 2021 11:51:51.339 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12473:C 25 Aug 2021 11:51:51.339 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=12473, just started
12473:C 25 Aug 2021 11:51:51.339 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
12473:M 25 Aug 2021 11:51:51.339 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 12473
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
12473:M 25 Aug 2021 11:51:51.340 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
12473:M 25 Aug 2021 11:51:51.340 # Server initialized
12473:M 25 Aug 2021 11:51:51.340 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
12473:M 25 Aug 2021 11:51:51.340 * Loading RDB produced by version 6.2.5
12473:M 25 Aug 2021 11:51:51.340 * RDB age 172 seconds
12473:M 25 Aug 2021 11:51:51.340 * RDB memory usage when created 0.77 Mb
12473:M 25 Aug 2021 11:51:51.340 * DB loaded from disk: 0.000 seconds
12473:M 25 Aug 2021 11:51:51.340 * Ready to accept connections
可以看到直接使用redis-server启动Redis后,会打印出一些日志,通过日志可以看到一些信息,上例中可以看到:
-当前的Redis版本的是6.2.5。
-Redis的默认端口是6379。
-Redis建议要使用配置文件来启动。
因为直接启动无法自定义配置,所以这种方式是不会在生产环境中使用的。
- 运行启动
redis-server加上要修改配置名和值(可以是多对),没有设置的配置将使用默认配置:
#redis-server --configKey1 configvaluel --configKey2 configvalue2
例如,如果要用6380作为端口启动Redis,那么可以执行:
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-server --port 6380
22256:C 25 Aug 2021 14:35:09.229 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
22256:C 25 Aug 2021 14:35:09.229 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=22256, just started
22256:C 25 Aug 2021 14:35:09.229 # Configuration loaded
22256:M 25 Aug 2021 14:35:09.229 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6380
| `-._ `._ / _.-' | PID: 22256
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
22256:M 25 Aug 2021 14:35:09.230 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
22256:M 25 Aug 2021 14:35:09.230 # Server initialized
22256:M 25 Aug 2021 14:35:09.230 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
22256:M 25 Aug 2021 14:35:09.230 * Loading RDB produced by version 6.2.5
22256:M 25 Aug 2021 14:35:09.230 * RDB age 122 seconds
22256:M 25 Aug 2021 14:35:09.230 * RDB memory usage when created 0.83 Mb
22256:M 25 Aug 2021 14:35:09.230 * DB loaded from disk: 0.000 seconds
22256:M 25 Aug 2021 14:35:09.230 * Ready to accept connections
- 配置文件启动
将配置写到指定文件里,例如我们将配置写到了/usr/local/bin/redis.conf中,那么只需要执行如下命令即可启动Redis:
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-server /usr/local/bin/redis.conf
25310:C 25 Aug 2021 14:38:02.388 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
25310:C 25 Aug 2021 14:38:02.388 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=25310, just started
25310:C 25 Aug 2021 14:38:02.388 # Configuration loaded
25310:M 25 Aug 2021 14:38:02.388 * monotonic clock: POSIX clock_gettime
25310:M 25 Aug 2021 14:38:02.388 # Warning: Could not create server TCP listening socket ::1:6379: bind: Cannot assign requested address
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.5 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 25310
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
25310:M 25 Aug 2021 14:38:02.389 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
25310:M 25 Aug 2021 14:38:02.389 # Server initialized
25310:M 25 Aug 2021 14:38:02.389 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
25310:M 25 Aug 2021 14:38:02.389 * Loading RDB produced by version 6.2.5
25310:M 25 Aug 2021 14:38:02.389 * RDB age 12 seconds
25310:M 25 Aug 2021 14:38:02.389 * RDB memory usage when created 0.77 Mb
25310:M 25 Aug 2021 14:38:02.389 * DB loaded from disk: 0.000 seconds
25310:M 25 Aug 2021 14:38:02.389 * Ready to accept connections
Redis有60多个配置,这里只给出一些重要的配置(如下表)
| 配置名 | 配置说明 |
|---|---|
| port | 端口 |
| logfile | 配置文件 |
| dir | Redis工作目录(存放持久化文件和日志文件) |
| daemonize | 是否以守护进程的方式启动(后台启动) |
Redis目录下都会有一个redis.conf配置文件,里面就是Redis的默认配置,通常来讲我们会在一台机器上启动多个Redis,并且将配置集中管理在指定目录下,而且配置不是完全手写的,而是将redis.conf作为模板进行修改。
显然通过配置文件启动的方式提供了更大的灵活性,所以大部分生产环境都以这种方式进行启动
那么经过上面3种启动方式 我们发现启动后要回到命令界面就会退出Redis这是因为我们不是后台启动,那我需要修改redis.conf配置文件中的daemonize设置为yes
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# vim redis.conf

提示:vim 进入编辑文件后 按i 进入编辑模式 修改后 按Esc键 然后输入 :wq 就可以退出编辑
修改文件后再次启动 就是以后台运行方式启动了
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-server /usr/local/bin/redis.conf
[root@iZuf6a2fzgjflg7bp7q2prZ bin]#
1.2.2 Redis命令行客户端 连接Redis服务
现在我们已经启动了Redis服务,下面将介绍如何使用redis-cli连接、操作Redis服务。redis-cli可以使用两种方式连接Redis服务器。
- 第一种是交互式方式:通过redis-cli-h{host}-p{port}的方式连接到Redis服务,之后所有的操作都是通过交互的方式实现,不需要再执行redis-cli了,例如:
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-cli -h 127.0.1 -p 6379
127.0.1:6379> set hello world
OK
127.0.1:6379> get hello
"world"
127.0.1:6379>
- 第二种是命令方式:用redis-cli-h ip{host}-p{port} {command}就可以直接得到命令的返回结果,例如:
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-cli -h 127.0.0.1 -p 6379 get hello
"world"
[root@iZuf6a2fzgjflg7bp7q2prZ bin]#
这里有1点要注意:如果没有-h参数,那么默认连接127.0.0.1;如果没有-p,那么默认6379端口,也就是说如果-h和-p都没写就是连接127.0.0.1:6379这个Redis实例
1.2.3 停止Redis服务
Redis提供了shutdown命令来停止Redis服务,例如要停掉127.0.0.1上6379端口上的Redis服务,可以执行如下操作。
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-cli shutdown
当使用redis-cli再次连接该Redis服务时,看到Redis已经“失联”。
[root@iZuf6a2fzgjflg7bp7q2prZ bin]# redis-cli -h 127.0.0.1 -p 6379 get hello
Could not connect to Redis at 127.0.0.1:6379: Connection refused
[root@iZuf6a2fzgjflg7bp7q2prZ bin]#
这里有三点需要注意一下:
1 Redis关闭的过程:断开与客户端的连接、持久化文件生成,是一种相对优雅的关闭方式。
2 除了可以通过shutdown命令关闭Redis服务以外,还可以通过kill进程号的方式关闭掉Redis,但是不要粗暴地使用kill-9强制杀死Redis服务,不但不会做持久化操作,还会造成缓冲区等资源不能被优雅关闭,极端情况会造成AOF和复制丢失数据的情况。
3 shutdown还有一个参数,代表是否在关闭Redis前,生成持久化文件:
redis-cli shutdown nosave | save
以上是Redis在Liunx上的安装启动及停止的详细步骤
参考了-Redis开发与运维 作者:付磊 张益军 此书全面讲解Redis基本功能及其应用,并结合线上开发与运维中的实际案例,深入分析并总结了实际工作中遇到的“陷阱”,以及背后的原因,包含大规模集群开发与管理的场景、应用案例与开发技巧.强烈推荐大家可以阅读一下.
本文详细介绍了在Linux系统上安装Redis的6.2.5版本,包括源码编译、配置启动、操作与停止Redis服务,以及使用redis-cli连接和管理。还涵盖了Redis的重要配置和常用命令。
2万+

被折叠的 条评论
为什么被折叠?



