# 一、安装

# 0x01 CentOS 7 - yum 直接安装

  1. 安装
sudo yum update
sudo yum install redis
1
2
  1. 启动Redis
sudo systemctl start redis
1
  1. 验证是否安装成功
redis-cli ping
1

如果redis在运行,将返回PONG

  1. 配置自动启动
sudo systemctl enable redis
1
  1. 访问设置

默认只允许本地连接,需要其他地址访问需要注释/etc/redis.conf中的bind 127.0.0.1并且将protected mode改为no

# 0x02 CentOS 7 - 编译安装

  1. 安装C 语言的编译环境
sudo yum install centos-release-scl scl-utils-build scl-utils devtoolset-8-toolchain

# 注意: 执行此命令会自动切换到 root 用户
sudo  scl enable devtoolset-8 bash
1
2
3
4
  1. 编译源码
wget https://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz -C /opt/
cd /opt/redis-stable
make
make install

[root@node01 redis]# ll /usr/local/bin/redis-*
# 性能测试工具
-rwxr-xr-x. 1 root root  8282752 328 17:52 /usr/local/bin/redis-benchmark 
# 修复有问题的AOF文件
lrwxrwxrwx. 1 root root       12 328 17:52 /usr/local/bin/redis-check-aof -> redis-server
# 修复有问题的dump.rdb文件
lrwxrwxrwx. 1 root root       12 328 17:52 /usr/local/bin/redis-check-rdb -> redis-server
# Redis集群使用
-rwxr-xr-x. 1 root root  8986248 328 17:52 /usr/local/bin/redis-cli
# Redis服务器启动命令
lrwxrwxrwx. 1 root root       12 328 17:52 /usr/local/bin/redis-sentinel -> redis-server
# 客户端,操作入口
-rwxr-xr-x. 1 root root 17839984 328 17:52 /usr/local/bin/redis-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 0x03 Docker

# 下载镜像
docker pull redis

# 创建和启动容器
# redis-server --appendonly yes 设置持久化手段
docker run --name myredis -p 6379:6379 -d redis redis-server --appendonly yes

# 测试
docker exec -it myredis redis-cli
 
set name tom
get name
1
2
3
4
5
6
7
8
9
10
11
12

# 二、后台启动

cd /opt/redis
sudo cp redis.conf redis.conf.bak
1
2

修改redis.conf参数

# 修改前
bind 127.0.0.1 -::1
# 修改后
#bind 127.0.0.1 -::1

# 修改前
protected-mode yes
# 修改后
protected-mode no

# 修改前
daemonize no
# 修改后
daemonize yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14

启动redis 服务

[root@node01 redis]# /usr/local/bin/redis-server redis.conf
16200:C 28 Mar 2024 18:07:43.707 # 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.
1
2

客户端链接测试

[hadoop@node01 ~]$ redis-cli
127.0.0.1:6379> ping
PONG
1
2
3

# 三、终端使用

# 启动

bin/redis-cli -h hadoop102
1

# 显示所有key

KEYS *
1

# 查询结果显示中文

# 启动的时候要加raw参数
bin/redis-cli -h hadoop102 --raw
GET "xxxxxxxx"
1
2
3

# 查看数据还会保存多久

ttl xxxxxx
1
更新时间: 3/29/2024, 5:18:31 PM