5.Redis 一篇就够
5.1 单机
文档地址 https://blog.csdn.net/qq_30764991/article/details/81564652
安装redis需要c语言的编译环境
yum install gcc-c++
解压
tar -xvf redis-5.0.5.tar.gz
mv redis-5.0.5 redis
编译
cd redis
make

安装
make install PREFIX=/home/redis

到安装目录启动
cd /home/redis/bin
./redis-server

复制配置文件
cp /home/software/redis/redis.conf /home/redis/bin/
修改守护进程为yes
vi redis.conf
设置行码
:set nu
69 改为0.0.0.0
136 改为yes

启动
./redis-server redis.conf
查看
ps aux|grep redis
客户端
./redis-cli
设置密码:
在/home/redis/bin目录下执行 ./redis-cli 进入redis客户端
查询redis密码:config get requirepass
设置redis密码为root: config set requirepass root
认证登陆redis: auth root

5.2 redis自启动
touch /usr/lib/systemd/system/redis.service
chown prometheus2:prometheus2 /usr/lib/systemd/system/redis.service
chown -R prometheus2:prometheus2 /home/redis
vim /usr/lib/systemd/system/redis.service
[Unit]
Description=redis
After=network.target
[Service]
Type=forking
User=prometheus2
ExecStart=/home/redis/bin/redis-server /home/redis/bin/redis.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动
systemctl daemon-reload
systemctl enable redis.service
systemctl start redis.service
systemctl status redis.service
systemctl stop redis.service
systemctl restart redis.service
5.3 redis config
单位 不区分大小写

包含

网络 NETWORK
bind 127.0.0.1 #绑定

端口

保护模式

通用
daemonize yes #以守护进程模式进行
pidfile /var/run/redis_6379.pid #指定进程文件
日志级别

日志文件输出地址

logfile ''/home/logs/redis/reids.log''
databases 16 默认数据库数量
always-show-logo yes 是否显示logo
快照 SNAPSHOTTING
持久化 在规定时间内 执行了多少次操作 会持久化到.rdb .aof文件
redis是内存数据库 不持久化 数据会丢失
save 900 1 #90s内至少有一个key进行修改 我们会进行持久化
save 300 10 #300s内至少有10个key进行修改 我们会进行持久化
save 60 10000 #60s内至少有10000个key进行修改 我们会进行持久化
save 10 10000 #10s内至少有10000个key进行修改 我们会进行持久化
stop-writes-on-bgsave-error no # 持久化失败是否继续工作 yes 是
rdbcompression yes #是否压缩rdb文件
rdbchecksum yes #保存rdb文件时 是否进行错误检查校验
dir ./ #rdb 文件保存目录
REPLICATION 负责 主从复制
SECURITY 安全
requirepass root #设置密码为root
CLIENTS 客户端
maxclients 10000 #最大客户端数为10000
MEMORY MANAGEMENT 内存管理
maxmemory #最大内存数 5G 5368709120
maxmemory-policy noeviction #达到最大内存时的 策略
1、volatile-lru:只对设置了过期时间的key进行LRU(默认值)
2、allkeys-lru :删除lru算法的key
3、volatile-random:随机删除即将过期key
4、allkeys-random:随机删除
5、volatile-ttl :删除即将过期的
6、noeviction :永不过期,返回错误
APPEND ONLY MODE aof配置
appendonly no #默认关闭 大多数情况下 rdb完全够用
appendfilename "appendonly.aof" #文件存储名称
appendfsync always #每次修改同步
appendfsync everysec #每秒执行一次 可能丢失一秒的数据
appendfsync no #不同步 操作系统自动同步 最快
5.4 主从同步 读写分离
x.x.x.2 master 2G ABCabc123
x.x.x.3 slave 2G ABCabc123
x.x.x.6 slave 2G ABCabc123
①查看信息
./redis-cli info replication #查看当前库信息
Replication
role:master
connected_slaves:0 #从库为0
master_replid:b7e98c58ca40eb3cfc842bb89698172162e1aa60
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
②配置主从关系 认主
slaveof x.x.x.6 6379
[图片上传失败...(image-c3fc31-1655779504259)]
③配置后信息
Replication
role:master
connected_slaves:2
slave0:ip=x.x.x.3,port=6379,state=online,offset=127645169,lag=0
slave1:ip=x.x.x.6,port=6379,state=online,offset=127935404,lag=0
master_replid:4804c72f789afab9b9f1819df933ae74f256e503
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:128106631
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:127058056
repl_backlog_histlen:1048576
Replication
role:slave
master_host:x.x.x.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:88091145
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4804c72f789afab9b9f1819df933ae74f256e503
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:88091145
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:87042570
repl_backlog_histlen:1048576
主机可以读写 从机只能读 主机宕机后 从机不能主动变成主机
命令行配置当次运行有效 重启后无效
5.5 多哨兵sentinel
①查看sentinel.config
cat sentinel.conf | grep -v "#" | grep -v "^$"
复制一份
cat sentinel.conf | grep -v "#" | grep -v "^$" >/usr/local/redis/bin/sentinel.conf
②配置哨兵文件 sentinel.config
port 26379
daemonize yes
pidfile /home/redis/redis-sentinel.pid
logfile "/home/redis/redis-sentinel.log"
dir /home/redis
sentinel monitor mymaster x.x.x.2 6379 2
sentine1 auth-pass mymaster root
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
③运行哨兵
./redis-sentinel sentinel.conf
④成功效果
./redis-cli -p 26379 info


5.6 sentinel自启动
touch /usr/lib/systemd/system/redis_sentinel.service
chown prometheus2:prometheus2 /usr/lib/systemd/system/redis_sentinel.service
chown -R prometheus2:prometheus2 /home/redis
vim /usr/lib/systemd/system/redis_sentinel.service
[Unit]
Description=redis_sentinel
After=network.target
[Service]
Type=forking
User=prometheus2
ExecStart=/home/redis/bin/redis-sentinel /home/redis/bin/sentinel.conf
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动
systemctl daemon-reload
systemctl enable redis_sentinel.service
systemctl start redis_sentinel.service
systemctl status redis_sentinel.service
systemctl stop redis_sentinel.service
systemctl restart redis_sentinel.service
5.7 SpringBoot整合redis集群
spring:
redis:
database: ${REDIS_BASEDATA:10}
password: ${REDIS_PASSWORD:root}
timeout: ${REDIS_TIMEOUT:86400}
cluster:
nodes:
- x.x.x.2:6379
- x.x.x.3:6379
- x.x.x.6:6379
sentinel:
master: mymaster
nodes:
- x.x.x.2:26379
- x.x.x.3:26379
- x.x.x.6:26379
jedis:
pool:
max-active: 1000
max-wait: -1
max-idle: 10
min-idle: 5
问题一:redis 6379端口不通解决方法
1.reids务器的6379端口telnet不通

- 查看reids进程和端口,都是存在的。只是ip地址是127.0.0.1而不是0.0.0.0,只是本机能使用
一般我们在服务端绑定端口的时候可以选择绑定到0.0.0.0,这样我的服务访问方就可以通过我的多个ip地址访问我的服务

3.查找redis的配置文件redis.conf 使用命令:find / -name redis.conf

4.查找bind 127.0.0.1所在的行数
使用命令:cat /home/redis/bin/redis.conf|grep bin -n

5.在vi下 set nu 显示行号 直接输入行数 调整
-
编辑配置文件vim /home/redis/bin/redis.conf
bind 127.0.0.1修改为bind 0.0.0.0

7.重启redis-server (kill -9 PID 先杀掉之前的实例)


问题****二****:redis****写入缓慢
Redis大批量插入数据导致报
io.lettuce.core.RedisCommandExecutionException: MISCONF Redis is configured to save RDB snapshots错误

