Redis的下载与安装

1.Redis下载

下载地址:https://redis.io/download

2.Redis安装

  • 将redis-6.2.6.tar.gz 上传到 /opt目录(也可以直接用yum下载到linux)
  • 进入 /opt目录 ,执行解压命令:tar-zxvf redis-6.2.6.tar.gz
  • 解压完成后进入redis-6.2.6目录:cd redis-6.2.6
  • 在redis-6.2.6目录下执行make命令(编译指令)
  • 执行 make install 进行安装
  • 到此安装完成 , 安装目录在 /usr/local/bin

查看安装目录:

[root@chenbaohui bin]# ls
dump.rdb redis-benchmark redis-check-rdb redis-sentinel
genhash redis-check-aof redis-cli redis-server

redis-benchmark:性能测试工具,可以在自己机器运行,看看自己机器性能如何

redis-check-aof:修复有问题的AOF文件,

rdb和aof后面讲

redis-check-dump:修复有问题的dump.rdb 文件

redis-sentinel:Redis 集群使用

redis-server:Redis 服务器启动命令

redis-cli:客户端,操作入口

Redis启动/后台启动

直接启动

执行命令: redis-server

启动成功后显示以下内容:

[root@chenbaohui home]# redis-server
7997:C 01 Apr 2025 04:29:21.197 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
7997:C 01 Apr 2025 04:29:21.197 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=7997, just started
7997:C 01 Apr 2025 04:29:21.197 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
7997:M 01 Apr 2025 04:29:21.198 * Increased maximum number of open files to 10032 (it was originally set to 1024).
7997:M 01 Apr 2025 04:29:21.198 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 6.2.6 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 7997
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

7997:M 01 Apr 2025 04:29:21.199 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
7997:M 01 Apr 2025 04:29:21.199 # Server initialized
7997:M 01 Apr 2025 04:29:21.199 # 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.
7997:M 01 Apr 2025 04:29:21.200 * Ready to accept connections

后台启动

后台启动需要配置修改redis.conf , 然后以启动配置文件的方式启动redis

到目录/opt/redis-6.2.6/下找到redis.conf文件

[root@chenbaohui bin]# cd /opt/redis-6.2.6/
[root@chenbaohui redis-6.2.6]# ls
00-RELEASENOTES dump.rdb runtest tests
BUGS INSTALL runtest-cluster TLS.md
CONDUCT Makefile runtest-moduleapi utils
CONTRIBUTING MANIFESTO runtest-sentinel
COPYING README.md sentinel.conf
deps redis.conf src

执行vi命令修改redis.conf文件 ,找到damonize no 将其修改为 damonize yes 表示以守护进程的方式启动redis,不会占用主进程

# 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

然后使用命令redis-server /opt/redis-6.2.6/redis.conf 启动redis服务,此时就可以操作控制台,而不是只有redis

[root@chenbaohui redis-6.2.6]# redis-server /opt/redis-6.2.6/redis.conf 
[root@chenbaohui redis-6.2.6]#

使用redis-cli命令可以进入redis

[root@chenbaohui redis-6.2.6]# redis-cli 
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth password
OK
127.0.0.1:6379>

关于一些Redis的Tip

1.远程访问reids需要在redis.conf中将bind 127.0.0.1 -::1修改为bind 0.0.0.0 -::1不然就只能在本机上访问redis

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0 -::1

2.redis的rbd持久化文件默认会保存到当前启动redis的目录 , 如果要指定rbd保存的目录可以在redis.conf将dir ./ 改为dir “你要指定的目录” (这里我指定放在root目录下)

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
#dir ./
dir /root/