Linux环境部署 Redis

相关介绍

Redis与其他key - value缓存产品有以下三个特点:

Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。Redis支持数据的备份,即master-slave模式的数据备份。

Redis 优势

性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。
丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。

适用场景

  • 数据的高并发读写
  • 海量数据读写
  • 对数据高可扩展性要求

不适用场景

  • 需要事务支持
  • 基于sql的结构化查询存储

Redis 安装与部署

官方下载网址:传送门

注意,Redis Stack是另外一个产品,是由多个模块组成的。此外,Redis官方的产品,是不支持Windows系统的

 

安装环境

测试gcc环境,确认是否安装C环境(一般都自带)

[root@localhost ~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。

如果没有的化,需要安装C语言编译环境。

gcc安装可以参考:gcc下载及安装教程(未验证真实性,仅供参考)

安装Redis

[root@localhost ~]# date 
2023年 02月 10日 星期五 21:51:13 CST
[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)
[root@localhost local]# tar -zxvf redis-6.2.10.tar.gz
[root@localhost local]# mv redis-6.2.10 redis

使用编译命令make,完成编译操作,以保证兼容当前平台。

若出现Jemalloc/jemalloc.h致命错误,确定是否gcc安装成功。若已成功,可以尝试用make distclean,清楚所有的编译文件,再试一次。

[root@localhost redis]# make
成功编译了以后,调用install完成文件安装
方法一:
[root@localhost redis]# make install
cd src && make install
make[1]: 进入目录“/usr/local/redis/src”
    CC Makefile.dep
make[1]: 离开目录“/usr/local/redis/src”
make[1]: 进入目录“/usr/local/redis/src”

Hint: It's a good idea to run 'make test' ;)

    INSTALL redis-server
    INSTALL redis-benchmark
    INSTALL redis-cli
make[1]: 离开目录“/usr/local/redis/src”
方法二:    
make PREFIX=/usr/local/redis install
这里多了一个关键字 PREFIX= 这个关键字的作用是编译的时候用于指定程序存放的路径。比如我们现在就是指定了redis必须存放在/usr/local/redis目录。假设不添加该关键字Linux会将可执行文件存放在/usr/local/bin目录,

库文件会存放在/usr/local/lib目录。配置文件会存放在/usr/local/etc目录。其他的资源文件会存放在usr/local/share目录。这里指定号目录也方便后续的卸载,后续直接rm -rf /usr/local/redis 即可删除redis。


可以在该文件下进行测试,以保证正确编译、安装
[root@localhost redis]# make test

启动Redis

默认执行程序安装在/usr/local/bin 文件下
[root@localhost bin]# ll
总用量 18924
-rwxr-xr-x 1 root root 4828960 2月  10 22:01 redis-benchmark
lrwxrwxrwx 1 root root      12 2月  10 22:01 redis-check-aof -> redis-server
lrwxrwxrwx 1 root root      12 2月  10 22:01 redis-check-rdb -> redis-server
-rwxr-xr-x 1 root root 5003072 2月  10 22:01 redis-cli
lrwxrwxrwx 1 root root      12 2月  10 22:01 redis-sentinel -> redis-server
-rwxr-xr-x 1 root root 9540968 2月  10 22:01 redis-server
[root@localhost bin]# pwd
/usr/local/bin
redis-benchmark:性能测试工具
redis-check-rdb(dump):修复有问题的RDB文件(快照文件)
redis-check-aof:修复有问题的AOF文件(记录输入命令)
redis-sentinel:Redis集群使用
redis-server:服务器启动命令
redis-cli:客户端操作入口

前台启动(这里根据第一种方式启动)

[root@localhost redis]# redis-server48150:C 10 Feb 2023 22:02:02.142 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
48150:C 10 Feb 2023 22:02:02.142 # Redis version=6.2.10, bits=64, commit=00000000, modified=0, pid=48150, just started
48150:C 10 Feb 2023 22:02:02.142 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
48150:M 10 Feb 2023 22:02:02.142 * Increased maximum number of open files to 10032 (it was originally set to 1024).
48150:M 10 Feb 2023 22:02:02.142 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.10 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 48150
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

48150:M 10 Feb 2023 22:02:02.143 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
48150:M 10 Feb 2023 22:02:02.143 # Server initialized
48150:M 10 Feb 2023 22:02:02.143 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. 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.
48150:M 10 Feb 2023 22:02:02.143 * Ready to accept connections
^C48150:signal-handler (1676037728) Received SIGINT scheduling shutdown...
48150:M 10 Feb 2023 22:02:08.206 # User requested shutdown...
48150:M 10 Feb 2023 22:02:08.206 * Saving the final RDB snapshot before exiting.
48150:M 10 Feb 2023 22:02:08.207 * DB saved on disk
48150:M 10 Feb 2023 22:02:08.207 # Redis is now ready to exit, bye bye...

或者

根据上面的操作已经将redis安装完成了,输入下面命令启动redis


./bin/redis-server& ./redis.conf

redis-server  //默认在/usr/local/bin下

 上面的启动方式是采取后台进程方式,下面是采取显示启动方式(如在配置文件设置了daemonize属性为yes则跟后台进程方式启动其实一样)。
./bin/redis-server ./redis.conf
两种方式区别无非是有无带符号&的区别。 redis-server 后面是配置文件,目的是根据该配置文件的配置启动redis服务。redis.conf配置文件允许自定义多个配置文件,通过启动时指定读取哪个即可。

前台关闭

前台通过ctrl+c可以直接关闭

后台启动

将解压文件中的redis.conf 复制导 /etc 下,通过修改文件中的 daemonize no 为daemonize yes,即可

[root@localhost redis]# cp -a redis.conf /etc/
[root@localhost redis]# sed -i 's/daemonize no/daemonize yes/' /etc/redis.conf
启动命令
[root@localhost redis]# redis-server /etc/redis.conf
[root@localhost redis]# ps -ef | grep redis    //可以查看到已启动
root      48514   2577  0 22:13 pts/0    00:00:00 ../bin/redis-server *:6379
root      48903   2577  0 22:22 pts/0    00:00:00 grep --color=auto redis

连接与关闭

  • 前台通过ctrl+c可以直接关闭
  • 通过 redis-cli shutdown 关闭(已连接则直接shutdown)
  • 通过kill命令关闭

redis.conf配置文件

在目录/usr/local/redis下有一个redis.conf的配置文件。我们上面启动方式就是执行了该配置文件的配置运行的。我么可以通过cat、vim、less等Linux内置的读取命令读取该文件。

也可以通过redis-cli命令进入redis控制台后通过CONFIG GET * 的方式读取所有配置项。 如下:

[root@localhost bin]# redis-cli
127.0.0.1:6379> CONFIG GET *
  1) "rdbchecksum"
  2) "yes"
  3) "daemonize"
  4) "no"
  5) "io-threads-do-reads"
  6) "no"
  7) "lua-replicate-commands"
  8) "yes"
  9) "always-show-logo"
 10) "no"
 11) "protected-mode"
 12) "yes"
 13) "rdbcompression"
 14) "yes"
 15) "rdb-del-sync-files"
 16) "no"
 17) "activerehashing"
 18) "yes"
 19) "stop-writes-on-bgsave-error"
 20) "yes"
 21) "set-proc-title"
 22) "yes"
 23) "dynamic-hz"
 24) "yes"
 25) "lazyfree-lazy-eviction"
 26) "no"
 27) "lazyfree-lazy-expire"
 28) "no"
 29) "lazyfree-lazy-server-del"
 30) "no"
 31) "lazyfree-lazy-user-del"
 32) "no"
 33) "lazyfree-lazy-user-flush"
 34) "no"
 35) "repl-disable-tcp-nodelay"
 36) "no"
 37) "repl-diskless-sync"
 38) "no"
 39) "gopher-enabled"
 40) "no"
 41) "aof-rewrite-incremental-fsync"
 42) "yes"
 43) "no-appendfsync-on-rewrite"
 44) "no"
 45) "cluster-require-full-coverage"
 46) "yes"
 47) "rdb-save-incremental-fsync"
 48) "yes"
 49) "aof-load-truncated"
 50) "yes"
 51) "aof-use-rdb-preamble"
 52) "yes"
 53) "cluster-replica-no-failover"
 54) "no"
 55) "cluster-slave-no-failover"
 56) "no"
 57) "replica-lazy-flush"
 58) "no"
 59) "slave-lazy-flush"
 60) "no"
 61) "replica-serve-stale-data"
 62) "yes"
 63) "slave-serve-stale-data"
 64) "yes"
 65) "replica-read-only"
 66) "yes"
 67) "slave-read-only"
 68) "yes"
 69) "replica-ignore-maxmemory"
 70) "yes"
 71) "slave-ignore-maxmemory"
 72) "yes"
 73) "jemalloc-bg-thread"
 74) "yes"
 75) "activedefrag"
 76) "no"
 77) "syslog-enabled"
 78) "no"
 79) "cluster-enabled"
 80) "no"
 81) "appendonly"
 82) "no"
 83) "cluster-allow-reads-when-down"
 84) "no"
 85) "crash-log-enabled"
 86) "yes"
 87) "crash-memcheck-enabled"
 88) "yes"
 89) "use-exit-on-panic"
 90) "no"
 91) "disable-thp"
 92) "yes"
 93) "cluster-allow-replica-migration"
 94) "yes"
 95) "replica-announced"
 96) "yes"
 97) "aclfile"
 98) ""
 99) "unixsocket"
100) ""
101) "pidfile"
102) ""
103) "replica-announce-ip"
104) ""
105) "slave-announce-ip"
106) ""
107) "masteruser"
108) ""
109) "cluster-announce-ip"
110) ""
111) "syslog-ident"
112) "redis"
113) "dbfilename"
114) "dump.rdb"
115) "appendfilename"
116) "appendonly.aof"
117) "server_cpulist"
118) ""
119) "bio_cpulist"
120) ""
121) "aof_rewrite_cpulist"
122) ""
123) "bgsave_cpulist"
124) ""
125) "ignore-warnings"
126) ""
127) "proc-title-template"
128) "{title} {listen-addr} {server-mode}"
129) "masterauth"
130) ""
131) "requirepass"
132) ""
133) "supervised"
134) "no"
135) "syslog-facility"
136) "local0"
137) "repl-diskless-load"
138) "disabled"
139) "loglevel"
140) "notice"
141) "maxmemory-policy"
142) "noeviction"
143) "appendfsync"
144) "everysec"
145) "oom-score-adj"
146) "no"
147) "acl-pubsub-default"
148) "allchannels"
149) "sanitize-dump-payload"
150) "no"
151) "databases"
152) "16"
153) "port"
154) "6379"
155) "io-threads"
156) "1"
157) "auto-aof-rewrite-percentage"
158) "100"
159) "cluster-replica-validity-factor"
160) "10"
161) "cluster-slave-validity-factor"
162) "10"
163) "list-max-ziplist-size"
164) "-2"
165) "tcp-keepalive"
166) "300"
167) "cluster-migration-barrier"
168) "1"
169) "active-defrag-cycle-min"
170) "1"
171) "active-defrag-cycle-max"
172) "25"
173) "active-defrag-threshold-lower"
174) "10"
175) "active-defrag-threshold-upper"
176) "100"
177) "lfu-log-factor"
178) "10"
179) "lfu-decay-time"
180) "1"
181) "replica-priority"
182) "100"
183) "slave-priority"
184) "100"
185) "repl-diskless-sync-delay"
186) "5"
187) "maxmemory-samples"
188) "5"
189) "maxmemory-eviction-tenacity"
190) "10"
191) "timeout"
192) "0"
193) "replica-announce-port"
194) "0"
195) "slave-announce-port"
196) "0"
197) "tcp-backlog"
198) "511"
199) "cluster-announce-bus-port"
200) "0"
201) "cluster-announce-port"
202) "0"
203) "cluster-announce-tls-port"
204) "0"
205) "repl-timeout"
206) "60"
207) "repl-ping-replica-period"
208) "10"
209) "repl-ping-slave-period"
210) "10"
211) "list-compress-depth"
212) "0"
213) "rdb-key-save-delay"
214) "0"
215) "key-load-delay"
216) "0"
217) "active-expire-effort"
218) "1"
219) "hz"
220) "10"
221) "min-replicas-to-write"
222) "0"
223) "min-slaves-to-write"
224) "0"
225) "min-replicas-max-lag"
226) "10"
227) "min-slaves-max-lag"
228) "10"
229) "maxclients"
230) "10000"
231) "active-defrag-max-scan-fields"
232) "1000"
233) "slowlog-max-len"
234) "128"
235) "acllog-max-len"
236) "128"
237) "lua-time-limit"
238) "5000"
239) "cluster-node-timeout"
240) "15000"
241) "slowlog-log-slower-than"
242) "10000"
243) "latency-monitor-threshold"
244) "0"
245) "proto-max-bulk-len"
246) "536870912"
247) "stream-node-max-entries"
248) "100"
249) "repl-backlog-size"
250) "1048576"
251) "maxmemory"
252) "0"
253) "hash-max-ziplist-entries"
254) "512"
255) "set-max-intset-entries"
256) "512"
257) "zset-max-ziplist-entries"
258) "128"
259) "active-defrag-ignore-bytes"
260) "104857600"
261) "hash-max-ziplist-value"
262) "64"
263) "stream-node-max-bytes"
264) "4096"
265) "zset-max-ziplist-value"
266) "64"
267) "hll-sparse-max-bytes"
268) "3000"
269) "tracking-table-max-keys"
270) "1000000"
271) "client-query-buffer-limit"
272) "1073741824"
273) "repl-backlog-ttl"
274) "3600"
275) "auto-aof-rewrite-min-size"
276) "67108864"
277) "logfile"
278) ""
279) "watchdog-period"
280) "0"
281) "dir"
282) "/usr/local/redis"
283) "save"
284) "3600 1 300 100 60 10000"
285) "client-output-buffer-limit"
286) "normal 0 0 0 slave 268435456 67108864 60 pubsub 33554432 8388608 60"
287) "unixsocketperm"
288) "0"
289) "slaveof"
290) ""
291) "notify-keyspace-events"
292) ""
293) "bind"
294) ""
295) "oom-score-adj-values"
296) "0 200 800"
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379> exit
此处折叠,点击展开

这里列举下比较重要的配置项

配置项名称 配置项值范围 说明
daemonize yes、no yes表示启用守护进程,默认是no即不以守护进程方式运行。其中Windows系统下不支持启用守护进程方式运行
port   指定 Redis 监听端口,默认端口为 6379
bind   绑定的主机地址,如果需要设置远程访问则直接将这个属性备注下或者改为bind * 即可,这个属性和下面的protected-mode控制了是否可以远程访问 。
protected-mode yes 、no 保护模式,该模式控制外部网是否可以连接redis服务,默认是yes,所以默认我们外网是无法访问的,如需外网连接rendis服务则需要将此属性改为no。
timeout 300 当客户端闲置多长时间后关闭连接,如果指定为 0,表示关闭该功能
loglevel debug、verbose、notice、warning 日志级别,默认为 notice
databases 16 设置数据库的数量,默认的数据库是0。整个通过客户端工具可以看得到
rdbcompression yes、no 指定存储至本地数据库时是否压缩数据,默认为 yes,Redis 采用 LZF 压缩,如果为了节省 CPU 时间,可以关闭该选项,但会导致数据库文件变的巨大。
dbfilename dump.rdb 指定本地数据库文件名,默认值为 dump.rdb
dir   指定本地数据库存放目录
requirepass   设置 Redis 连接密码,如果配置了连接密码,客户端在连接 Redis 时需要通过 AUTH <password> 命令提供密码,默认关闭
maxclients 0 设置同一时间最大客户端连接数,默认无限制,Redis 可以同时打开的客户端连接数为 Redis 进程可以打开的最大文件描述符数,如果设置 maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis 会关闭新的连接并向客户端返回 max number of clients reached 错误信息。
maxmemory XXX <bytes> 指定 Redis 最大内存限制,Redis 在启动时会把数据加载到内存中,达到最大内存后,Redis 会先尝试清除已到期或即将到期的 Key,当此方法处理 后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。Redis 新的 vm 机制,会把 Key 存放内存,Value 会存放在 swap 区。配置项值范围列里XXX为数值。

查看Redis是否正在运行

1.采取查看进程方式

[root@localhost bin]# ps -aux | grep redis

 

 

2、采取端口监听查看方式

[root@localhost bin]# netstat -lanp | grep 6379

redis-cli

 

redis-cli是连接本地redis服务的一个命令,通过该命令后可以连接redis的脚本控制台。如下图

输入exit可以退出redis脚本控制台

关闭运行中的Redis服务

输入redis-cli 进入控制台后输入命令shutdown即可关闭运行中的Redis服务了。如下图:

 

 

 至此安装完毕

远程连接Redis服务

我用的是RedisDesktopManager 这款连接工具

安装方法参考:2022年官网下安装RedisDesktopManager最全版与官网查阅方法

工具参考:Redis -几款可视化工具

主要记录远程连接不上的问题解决:

1.首先检查Linux的防火墙是否开启

开放6379端口号
[root@localhost]#firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost]#firewall-cmd --reload
success
或者
关闭防火墙
[root@localhost]# systemctl stop firewalld.service

2.检查redis的启动配置是否正确

  • 查看redis是否启动
    [root@localhost bin]#  ps -ef | grep redis
    root      49314      1  0 22:36 ?        00:00:01 redis-server 127.0.0.1:6379
    root      49814   2577  0 22:54 pts/0    00:00:00 grep --color=auto redis

如果是redis 3.2以上的版本默认是不允许外网访问的,需要修改redis.conf配置文件

  • 修改protected-mode
修改protected-mode yes
改为:protected-mode no.
#protected-mode参数是为了禁止外网访问redis,如果启用了,则只能够通过localhost ip (127.0.0.1)访问Redis


[root@localhost bin]# cat /etc/redis.conf | grep protected-mode
protected-mode yes
[root@localhost bin]# sed -i 's/protected-mode yes/protected-mode no/' /etc/redis.conf 
[root@localhost bin]# cat /etc/redis.conf | grep protected-mode
protected-mode no
  • 关闭 bind 127.0.0.1
    注释掉bind 127.0.0.1, 或者修改bind 0.0.0.0,表示允许所有ip地址访问
    [root@localhost bin]# cat /etc/redis.conf | grep 'bind 127.0.0.1 -::1' 
    bind 127.0.0.1 -::1
    [root@localhost bin]# sed -i 's/bind 127.0.0.1 -::1/bind 0.0.0.0 -::1/' /etc/redis.conf 
    [root@localhost bin]# cat /etc/redis.conf | grep 'bind 0.0.0.0 -::1' 
    bind 0.0.0.0 -::1

3.密码:建议检查一下Redis是否配置了密码,并且在yml配置中是否做了对应的处理(没有密码则不配置密码选项);

4.保护模式:建议检查redis.conf中protected-mode选项的参数是否为no,如果为yes则需要修改为no;

5.超时设置:建议检查一下yml中spring.redis.timeout的参数是否进行了设置,如果没有则需要设置为不为0的数字,例如spring.redis.timeout=5000。

若进行过Redis的配置修改,则需要重启Redis的服务才能正确生效。

重启服务

[root@localhost bin]# ps -ef |grep redis
root      49314      1  0 22:36 ?        00:00:02 redis-server 127.0.0.1:6379
root      50192   2577  0 23:03 pts/0    00:00:00 grep --color=auto redis
[root@localhost bin]# kill -9 49314
[root@localhost bin]# redis-server /etc/redis.conf 

 

 

 

成功连接

 

posted @ 2023-02-10 20:36  聆听说书人  阅读(302)  评论(0编辑  收藏  举报