redis集羣

 redis簡介

資源:java

  • https://redis.io/
    https://redis.io/download/
    http://redisdoc.com/python

特色:web

  • 速度快
  • 支持多種數據結構
  • 持久化
  • 主從複製
  • 支持過時時間
  • 支持事務
  • 消息訂閱
  • 官方不支持windows,可是有第三方版本

應用場景:redis

  • 數據緩存                         --提升訪問性能,使用的方式與memcache相同
  • 會話緩存(Session Cache)  --保存web會話信息,與cookie,session相比,由於有slb的緣由,用redis更適合
  • 排行榜/計數器                    --Nginx+lua+Redis計數器進行ip自動封禁
  • 消息隊列                             --構建實時消息系統,聊天,羣聊

 

 

redis安裝與多實例

安裝配置:數據庫

yum安裝,若是起不來,請參考日誌信息windows

[root@Poppy ~]# yum install redis
[root@Poppy ~]# systemctl status redis.service
[root@Poppy ~]# vi /etc/redis.conf
[root@Poppy ~]# cat /var/log/redis/redis.log
[root@Poppy ~]# chown -R redis.redis /var/log/redis/redis.log
[root@Poppy ~]# systemctl start redis.service
[root@Poppy ~]# systemctl status redis

編譯安裝,能夠設置多目錄,改不一樣端口可實現多redis實例共存緩存

[root@Poppy ~]# wget http://download.redis.io/releases/redis-3.2.6.tar.gz
[root@Poppy ~]# mv redis-3.2.6.tar.gz /data/
[root@Poppy ~]# cd /data/
[root@Poppy ~]# mkdir redis6380
[root@Poppy ~]# tar -zxf redis-3.2.6.tar.gz -C redis6380/
[root@Poppy ~]# cd redis6380/
[root@Poppy ~]# cd redis-3.2.6/
[root@Poppy ~]# make
[root@Poppy ~]# cp redis.conf ../
[root@Poppy ~]# cp src/redis-server ../

配置文件redis.conf裏面修改安全

啓動redis服務器

[root@Poppy redis6380]# ./redis-server ./redis.conf 
[root@Poppy redis6380]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 60.205.188.107:6666     0.0.0.0:*               LISTEN      14584/java          
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      18969/redis-server  
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      25154/./redis-serve 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2213/sshd 

訪問rediscookie

[root@Poppy ~]# redis-cli 6379
[root@Poppy ~]# ./redis-cli -p 6380 編譯目錄下src下有這個命令
[root@Poppy ~]# redis-cli -h 127.0.0.1 -p 6380 經過netstart -ntlp查看綁定在哪一個ip上

退出訪問終端和在終端中止redis

[root@Poppy redis6380]# redis-cli -p 6380           
127.0.0.1:6380> exit
[root@Poppy redis6380]# redis-cli -p 6380 
127.0.0.1:6380> SHUTDOWN
not connected> exit

 

Bind 保護模式,認證機制

  • Redis 3.2新特性                     -解決訪問安全
  • Bind                                     -指定ip進行監聽bind 192.168.1.xxx
  • 啓用保護模式protected-mode              -protected-mode yes/no
  • 增長requirepass {password}     -requirepass poppy
  • 在redis-cli中使用                   -auth {password}進行認證

客戶端登陸

[root@Poppy redis6380]# kill -9 25378
[root@Poppy redis6380]# ./redis-server ./redis.conf 
[root@Poppy redis6380]# redis-cli -p 6380           
127.0.0.1:6380> get foo
(error) NOAUTH Authentication required.
127.0.0.1:6380> auth poppy
OK

運行配置

  • 獲取當前配置  config get*
  • 變動運行配置  config set loglevel 'notice'

客戶端進入後,可輸入上述命令查看,運行配置變動重啓失效

redis數據存儲

持久化

持久化策略

壓縮

同步

常規操做與數據類型

字符串操做

哈希操做

列表操做

集合操做

生產消費模型

list數據類型生產包子,刪除包子

redis發佈訂閱

訂閱發佈實例

redis事務

事務命令

服務器命令

慢日誌查詢

數據備份

主從複製

在官方文檔裏面查找方法

危險 Redis 命令

Redis 稍微有點使用經驗的人都知道線上是不能執行 keys * 相關命令的,雖然其模糊匹配功能使用很是方便也很強大,在小數據量狀況下使用沒什麼問題,數據量大會致使 Redis 鎖住及 CPU 飆升,在生產環境建議禁用或者重命名!

還有哪些危險命令?

Redis 的危險命令主要有如下幾個:

  • keys

客戶端可查詢出全部存在的鍵。

  • flushdb

Delete all the keys of the currently selected DB. This command never fails.

刪除 Redis 中當前所在數據庫中的全部記錄,而且此命令從不會執行失敗。

  • flushall

Delete all the keys of all the existing databases, not just the currently selected one. This command never fails.

刪除 Redis 中全部數據庫中的全部記錄,不僅是當前所在數據庫,而且此命令從不會執行失敗。

  • config

客戶端可修改 Redis 配置。

怎麼禁用或重命名危險命令?

看下 redis.conf 默認配置文件,找到 SECURITY 區域,如如下所示。

################################## SECURITY ###################################

# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared

# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
#
# Example:
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possible to completely kill a command by renaming it into
# an empty string:
#
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to slaves may cause problems.

看說明,添加 rename-command 配置便可達到安全目的。

1)禁用命令

rename-command KEYS     ""
rename-command FLUSHALL ""
rename-command FLUSHDB  ""
rename-command CONFIG   ""

2)重命名命令

rename-command KEYS     "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
rename-command FLUSHALL "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
rename-command FLUSHDB  "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
rename-command CONFIG   "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

上面的 XX 能夠定義新命令名稱,或者用隨機字符代替。

通過以上的設置以後,危險命令就不會被客戶端執行了。

相關文章
相關標籤/搜索