Redis - Linux 下 Redis 6.2.6 安装和部署

2022年7月18日
大约 6 分钟

Redis - Linux 下 Redis 6.2.6 安装和部署

Redis(Remote Dictionary Server),即远程字典服务,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。而Redis 6.0引入了SSL、新的 RESP3 协议、ACL、客户端缓存、无盘副本、I/O 线程、更快的RDB加载、新模块API和更多改进。

1、下载redis6.2.6安装包

访问 https://redis.io/download 地址到官网进行下载,本文已最新版redis6.2.6版本演示安装和配置。

图

直接点击上图红框中下载按钮,然后通过远程工具上传至服务器即可,或者通过wget命令下载。

[root@Java-JingXuan home]# wget https://download.redis.io/releases/redis-6.2.6.tar.gz
--2022-01-16 22:09:18--  https://download.redis.io/releases/redis-6.2.6.tar.gz
Resolving download.redis.io (download.redis.io)... 45.60.125.1
Connecting to download.redis.io (download.redis.io)|45.60.125.1|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2476542 (2.4M) [application/octet-stream]
Saving to: ‘redis-6.2.6.tar.gz’

100%[================================================================================================================================================>] 2,476,542   53.1KB/s   in 40s

2022-01-16 22:10:00 (61.0 KB/s) - ‘redis-6.2.6.tar.gz’ saved [2476542/2476542]

2、解压压缩文件

解压redis-6.2.6.tar.gz文件,执行tar -xvf redis-6.2.6.tar.gz命令。

[root@Java-JingXuan home]# tar -xvf redis-6.2.6.tar.gz
...
redis-6.2.6/utils/srandmember/README.md
redis-6.2.6/utils/srandmember/showdist.rb
redis-6.2.6/utils/srandmember/showfreq.rb
redis-6.2.6/utils/systemd-redis_multiple_servers@.service
redis-6.2.6/utils/systemd-redis_server.service
redis-6.2.6/utils/tracking_collisions.c
redis-6.2.6/utils/whatisdoing.sh

3、重命名和移动文件

这里东哥将安装目录移动到/usr/local/目录下,并将redis-6.2.6重名为redis目录,使用mv /home/redis-6.2.6 /usr/local/redis命令,因此有如下操作。

[root@Java-JingXuan home]# mv /home/redis-6.2.6 /usr/local/redis
[root@Java-JingXuan home]# cd /usr/local/redis
[root@Java-JingXuan redis]# ll
total 240
-rw-rw-r--  1 root root 33624 Oct  4 18:59 00-RELEASENOTES
-rw-rw-r--  1 root root    51 Oct  4 18:59 BUGS
-rw-rw-r--  1 root root  5026 Oct  4 18:59 CONDUCT
-rw-rw-r--  1 root root  3384 Oct  4 18:59 CONTRIBUTING
-rw-rw-r--  1 root root  1487 Oct  4 18:59 COPYING
drwxrwxr-x  7 root root  4096 Oct  4 18:59 deps
-rw-rw-r--  1 root root    11 Oct  4 18:59 INSTALL
-rw-rw-r--  1 root root   151 Oct  4 18:59 Makefile
-rw-rw-r--  1 root root  6888 Oct  4 18:59 MANIFESTO
-rw-rw-r--  1 root root 21567 Oct  4 18:59 README.md
-rw-rw-r--  1 root root 93724 Oct  4 18:59 redis.conf
-rwxrwxr-x  1 root root   275 Oct  4 18:59 runtest
-rwxrwxr-x  1 root root   279 Oct  4 18:59 runtest-cluster
-rwxrwxr-x  1 root root  1079 Oct  4 18:59 runtest-moduleapi
-rwxrwxr-x  1 root root   281 Oct  4 18:59 runtest-sentinel
-rw-rw-r--  1 root root 13768 Oct  4 18:59 sentinel.conf
drwxrwxr-x  3 root root  4096 Oct  4 18:59 src
drwxrwxr-x 11 root root  4096 Oct  4 18:59 tests
-rw-rw-r--  1 root root  3055 Oct  4 18:59 TLS.md
drwxrwxr-x  9 root root  4096 Oct  4 18:59 utils

4、redis编译安装

1)redis编译,执行make命令。

[root@Java-JingXuan redis]# make
cd src && make all
make[1]: Entering directory `/usr/local/redis/src'
CC Makefile.dep
...
LINK redis-benchmark
INSTALL redis-check-rdb
INSTALL redis-check-aof

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

make[1]: Leaving directory `/usr/local/redis/src'

2)redis安装执行make install命令。

[root@Java-JingXuan redis]# make install
cd src && make install
make[1]: Entering directory `/usr/local/redis/src'
CC Makefile.dep
make[1]: Leaving directory `/usr/local/redis/src'
make[1]: Entering directory `/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]: Leaving directory `/usr/local/redis/src'

5、redis启动

为了方便管理,将Redis6.2.6目录中的conf配置文件和常用命令移动到统一目录中。

切换至redis根目录,创建bin和etc文件,将redis.conf文件移动到/usr/local/redis/etc/目录,将mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server 文件移动到/usr/local/redis/bin/目录。

[root@Java-JingXuan redis]# mkdir etc
[root@Java-JingXuan redis]# mkdir bin
[root@Java-JingXuan redis]# mv redis.conf /usr/local/redis/etc/
[root@Java-JingXuan redis]# cd src/
[root@Java-JingXuan src]# mv mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server /usr/local/redis/bin/
[root@Java-JingXuan src]# ll ../bin/
total 37524
-rwxrwxr-x 1 root root     735 Oct  4 18:59 mkreleasehdr.sh
-rwxr-xr-x 1 root root 4829472 Jan 16 22:30 redis-benchmark
-rwxr-xr-x 1 root root 9524032 Jan 16 22:30 redis-check-aof
-rwxr-xr-x 1 root root 9524032 Jan 16 22:30 redis-check-rdb
-rwxr-xr-x 1 root root 5003752 Jan 16 22:30 redis-cli
-rwxr-xr-x 1 root root 9524032 Jan 16 22:30 redis-server

执行redis-server命令,启动redis服务。另外,更多其他软件安装配置步骤,如mysql、kafka、fastdfs、elasticsearch各种版本等中间件安装配置图文步骤,公众号Java精选,回复中间件三个汉字,获取所有软件安装步骤。切勿乱回复,否则什么也没有!!!

[root@Java-JingXuan redis]# ./bin/redis-server
26344:C 16 Jan 2022 22:38:18.616 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
26344:C 16 Jan 2022 22:38:18.617 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=26344, just started
26344:C 16 Jan 2022 22:38:18.617 # Warning: no config file specified, using the default config. In order to specify a config file use ./bin/redis-server /path/to/redis.conf
26344:M 16 Jan 2022 22:38:18.617 * monotonic clock: POSIX clock_gettime
_._  
_.-``__ ''-._     
_.-``    `.  `_.  ''-._   Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```.  ```\/    _.,_ ''-._  
(    '      ,       .-`  | `,    )     Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
|    `-._   `._    /     _.-'    |     PID: 26344
`-._    `-._  `-./  _.-'    _.-'   
|`-._`-._    `-.__.-'    _.-'_.-'|  
|    `-._`-.__.-'_.-'    |   https://redis.io       
`-._    `-._`-.__.-'_.-'    _.-'   
|`-._`-._    `-.__.-'    _.-'_.-'|  
|    `-._`-.__.-'_.-'    |  
`-._    `-._`-.__.-'_.-'    _.-'   
`-._    `-.__.-'    _.-'       
`-.__.-'   
`-.__.-'

26344:M 16 Jan 2022 22:38:18.618 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
26344:M 16 Jan 2022 22:38:18.618 # Server initialized
26344:M 16 Jan 2022 22:38:18.618 # 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.
26344:M 16 Jan 2022 22:38:18.618 * Ready to accept connections

6、设置后台启动redis

编辑redis.conf文件,将daemonize属性改为yes,执行vim /usr/local/redis/etc/redis.conf命令。

[root@Java-JingXuan redis]# vim /usr/local/redis/etc/redis.conf

图

执行./bin/redis-server /usr/local/redis/etc/redis.conf命令,启动redis服务。

[root@Java-JingXuan redis]# ./bin/redis-server /usr/local/redis/etc/redis.conf

7、设置redis密码

编辑redis.conf文件,找到requirepass这个参数,若是没有增加requirepass参数并设置密码参考如下图所示,执行vim /usr/local/redis/etc/redis.conf命令。

图

保存后,重启redis服务即可生效。

8、设置redis服务远程访问

编辑redis.conf文件,找到bind 127.0.0.1 -::1配置,将用#号注释掉即可,参考如下图所示。

图

然后执行ps -ef|grep redis命令,找到已启动的redis服务,使用kill命令杀掉redis服务进程。再执行./bin/redis-server /usr/local/redis/etc/redis.conf命令,启动redis服务。

[root@Java-JingXuan redis]# ps -ef|grep redis
root     27715     1  0 22:45 ?00:00:00 ./bin/redis-server 127.0.0.1:6379
root     29297 25962  0 22:53 pts/4    00:00:00 grep --color=auto redis
[root@Java-JingXuan redis]# kill -9 27715
[root@Java-JingXuan redis]# ./bin/redis-server /usr/local/redis/etc/redis.conf
[root@Java-JingXuan redis]# ps -ef|grep redis
root     29323     1  0 22:53 ?00:00:00 ./bin/redis-server *:6379
root     29357 25962  0 22:54 pts/4    00:00:00 grep --color=auto redis

使用连接redis服务的客户端连接测试,如下图所示表示连接成功。

图