Redis安装

文章首发在我的博客:https://blog.liuzijian.com/post/62299bca-35ea-4518-99fb-bbe8672021cc.html

此处使用64位的Rocky Linux release 9.5环境编译安装Redis-7.2.6,Redis要发挥出最佳性能需要安装运行在Linux系统

1.下载

从官方GitHub地址 github.com/redis 下载7.2.6版本源码到服务器

bash 复制代码
cd /opt

wget  https://github.com/redis/redis/archive/refs/tags/7.2.6.tar.gz

2.编译安装

解压下载的tar包后,切换到解压后的目录redis-7.2.6里面,src就是redis的源代码,redis.conf是redis配置文件,sentinel.conf和哨兵模式配置有关,Makefile用于编译安装redis

text 复制代码
[root@localhost opt]# cd redis-7.2.6/
[root@localhost redis-7.2.6]# ll
total 252
-rw-rw-r--.  1 root root  22388 Oct  3 03:13 00-RELEASENOTES
-rw-rw-r--.  1 root root     51 Oct  3 03:13 BUGS
-rw-rw-r--.  1 root root   5027 Oct  3 03:13 CODE_OF_CONDUCT.md
-rw-rw-r--.  1 root root   2634 Oct  3 03:13 CONTRIBUTING.md
-rw-rw-r--.  1 root root   1487 Oct  3 03:13 COPYING
-rw-rw-r--.  1 root root     11 Oct  3 03:13 INSTALL
-rw-rw-r--.  1 root root   6888 Oct  3 03:13 MANIFESTO
-rw-rw-r--.  1 root root    151 Oct  3 03:13 Makefile
-rw-rw-r--.  1 root root  22607 Oct  3 03:13 README.md
-rw-rw-r--.  1 root root   1695 Oct  3 03:13 SECURITY.md
-rw-rw-r--.  1 root root   3628 Oct  3 03:13 TLS.md
drwxrwxr-x.  8 root root    133 Oct  3 03:13 deps
-rw-rw-r--.  1 root root 107512 Oct  3 03:13 redis.conf
-rwxrwxr-x.  1 root root    279 Oct  3 03:13 runtest
-rwxrwxr-x.  1 root root    283 Oct  3 03:13 runtest-cluster
-rwxrwxr-x.  1 root root   1772 Oct  3 03:13 runtest-moduleapi
-rwxrwxr-x.  1 root root    285 Oct  3 03:13 runtest-sentinel
-rw-rw-r--.  1 root root  14700 Oct  3 03:13 sentinel.conf
drwxrwxr-x.  4 root root   8192 Oct  3 03:13 src
drwxrwxr-x. 11 root root   4096 Oct  3 03:13 tests
drwxrwxr-x.  9 root root   4096 Oct  3 03:13 utils

编译安装前,要先安装必要的软件包

bash 复制代码
yum install -y make gcc

要保证gcc版本 >= 4.8.5

text 复制代码
[root@localhost local]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-host-pie --enable-host-bind-now --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.rockylinux.org/ --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --enable-initfini-array --without-isl --enable-multilib --with-linker-hash-style=gnu --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_64=x86-64-v2 --with-arch_32=x86-64 --build=x86_64-redhat-linux --with-build-config=bootstrap-lto --enable-link-serialization=1
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.5.0 20240719 (Red Hat 11.5.0-2) (GCC) 

在解压后的redis-7.2.6目录下执行以下命令,编译安装,将redis安装到/usr/local/redis/bin这个目录下

bash 复制代码
make PREFIX=/usr/local/redis install

编译完成,切换到/usr/local/redis/bin目录,就会看到编译好的redis二进制文件了,里面包含几个命令

  • redis-benchmark 性能测试工具
  • redis-check-aof 修复有问题的AOF文件
  • redis-check-dump 修复有问题的dump.rdb文件
  • redis-cli 客户端,操作入口
  • redis-sentinel redis集群使用
  • redis-server redis服务器启动命今
text 复制代码
[root@localhost redis-7.2.6]# cd /usr/local/redis/bin
[root@localhost bin]# ll
total 28496
-rwxr-xr-x. 1 root root  6446056 Feb  4 13:14 redis-benchmark
lrwxrwxrwx. 1 root root       12 Feb  4 13:14 redis-check-aof -> redis-server
lrwxrwxrwx. 1 root root       12 Feb  4 13:14 redis-check-rdb -> redis-server
-rwxr-xr-x. 1 root root  7030624 Feb  4 13:14 redis-cli
lrwxrwxrwx. 1 root root       12 Feb  4 13:14 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 15699832 Feb  4 13:14 redis-server

返回redis-7.2.6目录,将目录下的redis.conf拷贝到安装目标位置/usr/local/redis/bin,这个是redis的配置文件

bash 复制代码
cp redis.conf  /usr/local/redis/bin

3.运行测试

使用redis.conf配置启动redis

bash 复制代码
./redis-server ./redis.conf 
text 复制代码
[root@localhost bin]# ./redis-server ./redis.conf
5837:C 04 Feb 2025 13:15:52.211 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it 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.
5837:C 04 Feb 2025 13:15:52.212 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
5837:C 04 Feb 2025 13:15:52.213 * Redis version=7.2.6, bits=64, commit=00000000, modified=0, pid=5837, just started
5837:C 04 Feb 2025 13:15:52.213 * Configuration loaded
5837:M 04 Feb 2025 13:15:52.214 * Increased maximum number of open files to 10032 (it was originally set to 1024).
5837:M 04 Feb 2025 13:15:52.215 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 7.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 5837
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5837:M 04 Feb 2025 13:15:52.224 * Server initialized
5837:M 04 Feb 2025 13:15:52.224 * Ready to accept connections tcp

出现这个界面就说明启动成功了。

4.简单设置

vim redis.conf配置文件,进行以下修改,完成后重启redis-server

1.默认daemonize no 改为 daemonize yes,让redis从后台启动。

ini 复制代码
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize yes

2.默认protected-mode yes 改为 protected-mode no,关闭保护模式,让其他服务(例如spring-boot后端服务)可以连接到这个redis。

ini 复制代码
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and the default user has no password, the server
# only accepts local connections from the IPv4 address (127.0.0.1), IPv6 address
# (::1) or Unix domain sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured.
protected-mode no

3.默认bind 127.0.0.1改为直接注释掉(默认bind 127.0.0.1只能本机访问)或改成0.0.0.0,否则影响远程IP连接

ini 复制代码
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interface. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1     # listens on two specific IPv4 addresses
# bind 127.0.0.1 ::1              # listens on loopback IPv4 and IPv6
# bind * -::*                     # like the default, all available interfaces
#
# ~~~ 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 on the
# IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
# will only be able to accept client connections from the same host that it is
# running on).
#
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# COMMENT OUT THE FOLLOWING LINE.
#
# You will also need to set a password unless you explicitly disable protected
# mode.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0

4.添加redis密码,requirepass一行的注释打开,并将默认密码改为requirepass 自己设置的密码,设置密码更加安全防止被黑客利用攻击。

ini 复制代码
# IMPORTANT NOTE: starting with Redis 6 "requirepass" is just a compatibility
# layer on top of the new ACL system. The option effect will be just setting
# the password for the default user. Clients will still authenticate using
# AUTH <password> as usually, or more explicitly with AUTH default <password>
# if they follow the new protocol: both will work.
#
# The requirepass is not compatible with aclfile option and the ACL LOAD
# command, these will cause requirepass to be ignored.
#
requirepass lzj

5.客户端redis-cli

1.客户端连接 使用redis-cli命令进入交互模式。 -a设置密码 -p设置端口,不写默认6379 -h设置主机(没有用到) 进入交互模式后,客户端发送ping指令,服务端返回pong即为连接成功。 使用quit命令断开连接,退出交互模式。

text 复制代码
[root@localhost bin]# ./redis-cli -a lzj  -p 6379 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> quit
[root@localhost bin]#

如果忘记输入密码,交互模式下无法执行命令,使用auth命令补上密码,即可连接成功。

text 复制代码
[root@localhost bin]# ./redis-cli   -p 6379 
127.0.0.1:6379> ping
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth lzj
OK
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> 

服务端不设置密码的话,-a, auth都是不需要的。

2.使用客户端命令关闭redis服务器,该命令关闭的是redis-server。

text 复制代码
./redis-cli -a lzj  -p 6379 shutdown

6.用Docker安装Redis

text 复制代码
docker run -d \
--privileged=true \
-v /data/redis/conf:/usr/local/etc/redis \
-v /data/redis/data:/data \
-p 16379:6379 \
--name redis7 \
redis:7.0.11  \
redis-server /usr/local/etc/redis/redis.conf
相关推荐
电脑玩家粉色男孩3 分钟前
八、Spring Boot 日志详解
java·spring boot·后端
ChinaRainbowSea1 小时前
八. Spring Boot2 整合连接 Redis(超详细剖析)
java·数据库·spring boot·redis·后端·nosql
叫我DPT1 小时前
Go 中 defer 的机制
开发语言·后端·golang
我们的五年2 小时前
【Linux网络编程】:守护进程,前台进程,后台进程
linux·服务器·后端·ubuntu
谢大旭2 小时前
ASP.NET Core自定义 MIME 类型配置
后端·c#
SomeB1oody4 小时前
【Rust自学】19.5. 高级类型
开发语言·后端·设计模式·rust
计算机-秋大田4 小时前
基于微信小程序的实习记录系统设计与实现(LW+源码+讲解)
vue.js·spring boot·后端·微信小程序·小程序·课程设计
爱上语文5 小时前
Spring核心:AOP(1):基本使用
后端
热爱学习的路人甲5 小时前
深入理解etcd(九)--- wal
后端