linux的redis的安裝和使用

yum安裝redis

1.yum安裝php

#前提得配置好阿里雲yum源,epel源
#查看是否有redis包
yum list redis
#安裝redis
yum install redis -y
#安裝好,啓動redis
systemctl start redis

2.檢測redis是否工做html

redis-cli    #redis 客戶端工具
#進入交互式環境後,執行ping,返回pong表示安裝成功
127.0.0.1:6379> ping
PONG

源碼安裝redis,編譯安裝

編譯安裝的優點是:node

  • 編譯安裝時能夠指定擴展的module(模塊),php、apache、nginx都是同樣有不少第三方擴展模塊,如mysql,編譯安裝時候,若是須要就定製存儲引擎(innodb,仍是MyIASM)
  • 編譯安裝能夠統一安裝路徑,linux軟件約定安裝目錄在/opt/下面
  • 軟件倉庫版本通常比較低,編譯源碼安裝能夠根據需求,安裝最新的版本
1.下載redis源碼
wget http://download.redis.io/releases/redis-4.0.10.tar.gz
2.解壓縮
tar -zxf redis-4.0.10.tar.gz
3.切換redis源碼目錄
cd redis-4.0.10.tar.gz
4.編譯源文件
make
5.編譯好後,src/目錄下有編譯好的redis指令
6.make install 安裝到指定目錄,默認在/usr/local/bin

redis可執行文件python

./redis-benchmark //用於進行redis性能測試的工具
./redis-check-dump //用於修復出問題的dump.rdb文件
./redis-cli //redis的客戶端
./redis-server //redis的服務端
./redis-check-aof //用於修復出問題的AOF文件
./redis-sentinel //用於集羣管理

啓動redis服務端mysql

啓動redis很是簡單,直接./redis-server就能夠啓動服務端了,還能夠用下面的方法指定要加載的配置文件:
./redis-server ../redis.conf
默認狀況下,redis-server會以非daemon的方式來運行,且默認服務端口爲6379。

使用redis客戶端linux

#執行客戶端命令便可進入
./redis-cli  
#測試是否鏈接上redis
127.0.0.1:6379 > ping
返回pong表明鏈接上了

//用set來設置key、value
127.0.0.1:6379 > set name "chaoge"
OK
//get獲取name的值
127.0.0.1:6379 > get name
"chaoge"

經過新的端口和密碼登陸redisnginx

redis.conf設置golang

protected-mode yes   #打開保護模式
port 6380  #更改默認啓動端口
requirepass xxxxxx   #設置redis啓動密碼,xxxx是自定義的密碼

啓動redis服務端web

redis-server /opt/redis-4.0.10/redis.conf &     #指定配置文件啓動redis,且後臺啓動

使用密碼登陸redis,使用6380端口redis

方法1,使用這個

[root@oldboy_python ~ 09:48:41]#redis-cli -p 6380
127.0.0.1:6380> auth xxxx
OK

方法2,此方案不安全,容易暴露密碼

[root@oldboy_python ~ 09:49:46]#redis-cli -p 6380 -a xxxx
Warning: Using a password with '-a' option on the command line interface may not be safe.
127.0.0.1:6380> ping
PONG

補充

檢查redis是否設置了密碼

127.0.0.1:6380> CONFIG get requirepass
1) "requirepass"
2) "xxxxxx"

若是沒有,也能夠給redis設置密碼(命令方式)

CONFIG set requirepass "xxxxxx"

發佈/訂閱 實驗

發佈訂閱的命令

PUBLISH channel msg
    將信息 message 發送到指定的頻道 channel
SUBSCRIBE channel [channel ...] 訂閱頻道,能夠同時訂閱多個頻道
UNSUBSCRIBE [channel ...] 取消訂閱指定的頻道, 若是不指定頻道,則會取消訂閱全部頻道 PSUBSCRIBE pattern [pattern ...] 訂閱一個或多個符合給定模式的頻道,每一個模式以 * 做爲匹配符,好比 it* 匹配所 有以 it 開頭的頻道( it.news 、 it.blog 、 it.tweets 等等), news.* 匹配全部 以 news. 開頭的頻道( news.it 、 news.global.today 等等),諸如此類 PUNSUBSCRIBE [pattern [pattern ...]] 退訂指定的規則, 若是沒有參數則會退訂全部規則 PUBSUB subcommand [argument [argument ...]] 查看訂閱與發佈系統狀態 注意:使用發佈訂閱模式實現的消息隊列,當有客戶端訂閱channel後只能收到後續發佈到該頻道的消息,以前發送的不會緩存,必須Provider和Consumer同時在線。

發佈訂閱:

窗口1,啓動兩個redis-cli窗口,均訂閱diantai 頻道(channel)

窗口2,啓動發佈者向頻道 diantai發送消息

[root@web02 ~]# redis-cli
127.0.0.1:6379> PUBLISH diantai 'jinyewugenglaiwojia'
(integer) 2

窗口3,查看訂閱者的消息狀態

訂閱一個或者多個符合模式的頻道

窗口1,啓動兩個redis-cli窗口,均訂閱 wang*頻道(channel)

127.0.0.1:6379> PSUBSCRIBE wang*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "wang*"
3) (integer) 1

1) "pmessage" 2) "wang*" 3) "wangbaoqiang" 4) "jintian zhennanshou "

窗口2,啓動redis-cli窗口,均訂閱wang*頻道

127.0.0.1:6379> PSUBSCRIBE wang*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "wang*"
3) (integer) 1



1) "pmessage"
2) "wang*"
3) "wangbaoqiang"
4) "jintian zhennanshou "

窗口3,發佈者消息

[root@web02 ~]# redis-cli
127.0.0.1:6379> PUBLISH wangbaoqiang "jintian zhennanshou "
(integer) 2

redis持久化

Redis是一種內存型數據庫,一旦服務器進程退出,數據庫的數據就會丟失,爲了解決這個問題,Redis提供了兩種持久化的方案,將內存中的數據保存到磁盤中,避免數據的丟失。

RDB持久化

redis提供了RDB持久化的功能,這個功能能夠將redis在內存中的的狀態保存到硬盤中,它能夠手動執行。

也能夠再redis.conf中配置,按期執行

RDB持久化產生的RDB文件是一個通過壓縮二進制文件,這個文件被保存在硬盤中,redis能夠經過這個文件還原數據庫當時的狀態

RDB(持久化)
內存數據保存到磁盤
在指定的時間間隔內生成數據集的時間點快照(point-in-time snapshot)
優勢:速度快,適合作備份,主從複製就是基於RDB持久化功能實現
rdb經過再redis中使用save命令觸發 rdb


rdb配置參數:

dir /data/6379/
dbfilename  dbmp.rdb

每過900秒 有1個操做就進行持久化

save 900秒  1個修改類的操做
save 300秒  10個操做
save 60秒  10000個操做

save  900 1
save 300 10
save 60  1000

 redis持久化之RDB實踐

1.啓動redis服務端,準備配置文件

daemonize yes
port 6379
logfile /data/6379/redis.log
dir /data/6379              #定義持久化文件存儲位置
dbfilename  dbmp.rdb        #rdb持久化文件
bind 10.0.0.10  127.0.0.1    #redis綁定地址
requirepass redhat            #redis登陸密碼
save 900 1                    #rdb機制 每900秒 有1個修改記錄
save 300 10                    #每300秒        10個修改記錄
save 60  10000                #每60秒內        10000修改記錄

2.啓動redis服務端

3.登陸redis設置一個key

redis-cli -a redhat

4.此時檢查目錄,/data/6379底下沒有dbmp.rdb文件

5.經過save觸發持久化,將數據寫入RDB文件

127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> save
OK

redis持久化之AOF

AOF(append-only log file)
記錄服務器執行的全部變動操做命令(例如set del等),並在服務器啓動時,經過從新執行這些命令來還原數據集
AOF 文件中的命令所有以redis協議的格式保存,新命令追加到文件末尾。
優勢:最大程序保證數據不丟
缺點:日誌記錄很是大

redis-client   寫入數據  >  redis-server   同步命令   >  AOF文件

配置參數

AOF持久化配置,兩條參數

appendonly yes
appendfsync  always    老是修改類的操做
             everysec   每秒作一次持久化
             no     依賴於系統自帶的緩存大小機制

1.準備aof配置文件 redis.conf

daemonize yes
port 6379
logfile /data/6379/redis.log
dir /data/6379
dbfilename  dbmp.rdb
requirepass redhat
save 900 1
save 300 10
save 60  10000
appendonly yes
appendfsync everysec

2.啓動redis服務

redis-server /etc/redis.conf

3.檢查redis數據目錄/data/6379/是否產生了aof文件

[root@web02 6379]# ls
appendonly.aof  dbmp.rdb  redis.log

4.登陸redis-cli,寫入數據,實時檢查aof文件信息

[root@web02 6379]# tail -f appendonly.aof

5.設置新key,檢查aof信息,而後關閉redis,檢查數據是否持久化

redis-cli -a redhat shutdown

redis-server /etc/redis.conf

redis-cli -a redhat

redis不重啓之rdb數據切換到aof數據

redis.conf服務端配置文件

daemonize yes
port 6379
logfile /data/6379/redis.log
dir /data/6379
dbfilename  dbmp.rdb
save 900 1                    #rdb機制 每900秒 有1個修改記錄
save 300 10                    #每300秒        10個修改記錄
save 60  10000                #每60秒內        10000修改記錄

啓動redis服務端

redis-server redis.conf

登陸redis-cli插入數據,手動持久化

127.0.0.1:6379> set name chaoge
OK
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> set addr shahe
OK
127.0.0.1:6379> save
OK

檢查RDB文件

[root@pyyuc /data 22:34:16]#ls 6379/
dbmp.rdb  redis.log

備份這個rdb文件,保證數據安全

[root@pyyuc /data/6379 22:35:38]#cp dbmp.rdb /opt/

執行命令,開啓AOF持久化

127.0.0.1:6379> CONFIG set appendonly yes   #開啓AOF功能
OK
127.0.0.1:6379> CONFIG SET save ""  #關閉RDB功能
OK

確保數據庫的key數量正確

127.0.0.1:6379> keys *
1) "addr"
2) "age"
3) "name"

確保插入新的key,AOF文件會記錄

127.0.0.1:6379> set title golang
OK

參考:http://www.javashuo.com/article/p-cglbaabl-eh.html

總結:

1.在linux安裝redis
經過源碼編譯安裝redis
1.下載源碼包
    wget http://download.redis.io/releases/redis-4.0.10.tar.gz
2.解壓縮redis
    tar -zxf redis-4.0.10.tar.gz 
3.進入redis源碼,直接能夠編譯且安裝
    cd redis-4.0.10
    make && make install 
    
4.能夠指定配置文件啓動redis
    vim /opt/redis-4.0.10/redis.conf 
    更改  僅修改: daemonize yes 
    啓動   redis-server redis.conf 
    使用客戶端    redis-cli
      ping    出現  PONG   成功
    
    
    vim /opt/redis-4.0.10/redis.conf
    1.更改bind參數,讓redis能夠遠程訪問
        bind 0.0.0.0
    2.更改redis的默認端口
        port 6380
    3.使用redis的密碼進行登陸
        requirepass 登陸redis的密碼
    4.指定配置文件啓動
        redis-server redis.conf 
        
5.經過新的端口和密碼登陸redis
redis-cli -p 6380
登陸後
auth 密碼
keys *   查看數據

redis還支持交互式的參數,登陸數據庫
redis-cli -p 6380  -a  redis的密碼  (這個不太安全)

6.經過登陸redis,用命令查看redis的密碼
config set  requirepass  新的密碼         #設置新密碼
config get  requirepass              #獲取當前的密碼


過濾出文件的空白行和註釋行
grep -v "^#"  redis.conf |   grep  -v "^$"

 

redis發佈訂閱

三個角色,提供的redis命令
1.發佈者
    publish  頻道  消息        給頻道發消息
2.訂閱者
    SUBSCRIBE  頻道         訂閱頻道 
    PSUBSCRIBE 頻道*          支持模糊匹配的訂閱
3.頻道
    channel  頻道名 自定義
    
redis持久化之RDB
1.在配置文件中添加參數,開啓rdb功能
進入redis工做目錄
[root@localhost redis-4.0.10]# vim redis.conf
redis.conf 寫入
    port 6379
    daemonize yes    後臺運行
    logfile /data/6379/redis.log    日誌文件
    dir /data/6379      制定數據文件
    dbfilename   s15.rdb    
    save 900 1                    #rdb機制 每900秒 有1個修>改記錄
    save 300 10                    #每300秒        10個修改記錄
    save 60  10000                #每60秒內        10000修>改記錄
2.開啓redis服務端,測試rdb功能
redis-server redis.conf 


redis持久化之aof
1.開啓aof功能,在redis.conf中添加參數
    port 6379
    daemonize yes
    logfile /data/6379/redis.log
    dir /data/6379
    appendonly yes
    appendfsync everysec
2.啓動redis服務端,指定aof功能,測試持久化數據 


redis不重啓之rdb數據切換到aof數據
1.準備rdb的redis服務端
    redis-server   s15-redis.conf (註明這是在rdb持久化模式下)

2.切換rdb到aof
redis-cli  登陸redis,而後經過命令,激活aof持久化
127.0.0.1:6379>  CONFIG set appendonly yes                #用命令激活aof持久化(臨時生效,注意寫入到配置文件)
OK
127.0.0.1:6379> 
127.0.0.1:6379> 
127.0.0.1:6379>  CONFIG SET save ""             #關閉rdb持久化

2.5 將aof操做,寫入到配置文件,永久生效,下次重啓後生效
    port 6379
    daemonize yes 
    logfile /data/6379/redis.log
    dir /data/6379   

    #dbfilename   s15.rdb
    #save 900 1  
    #save 300 10 
    #save 60  10000 
    appendonly yes
    appendfsync everysec

3.測試aof數據持久化 ,殺掉redis,從新啓動
kill 
redis-server s15-redis.conf 

4.寫入數據,檢查aof文件

redis的主從同步

1.檢查redis數據庫信息,主從狀態的命令
redis-cli  -p 6379  info  檢查數據庫信息
redis-cli  -p 6379  info  replication  檢查數據庫主從信息
  

1.準備三個redis配置文件,經過端口的區分,啓動三個redis數據庫實例,而後配置主從複製
redis-6379.conf 
    port 6379
    daemonize yes
    pidfile /data/6379/redis.pid
    loglevel notice
    logfile "/data/6379/redis.log"
    dbfilename dump.rdb
    dir /data/6379
    
redis-6380.conf 
#經過命令快速生成配置文件
    sed "s/6379/6380/g" redis-6379.conf > redis-6380.conf 
    在redis-6380.conf中寫入
    slaveof  127.0.0.1  6379   #指明主庫的身份ip 和端口

redis-6381.conf 
#經過命令快速生成配置文件
    sed "s/6379/6381/g" redis-6379.conf > redis-6381.conf 
    在redis-6381.conf中寫入
    slaveof  127.0.0.1  6379

2.啓動三個數據庫實例,檢測redis主從同步方案


3.redis主從賦值,故障手動切換,
    1.殺死6379的主庫實例
    kill 主庫
    
    2.手動切換主從身份
        1.登陸 redis-6380 ,經過命令,去掉本身的從庫身份,等待鏈接
            slaveof no one  
        2.登陸redis-6381 ,經過命令,生成新的主任
            slaveof 127.0.0.1 6380  
    
    3.測試新的主從數據同步
    
    
redis哨兵
1.什麼是哨兵呢?保護redis主從集羣,正常運轉,當主庫掛掉以後,自動的在從庫中挑選新的主庫,進行同步

2.redis哨兵的安裝配置
    1. 準備三個redis數據庫實例(三個配置文件,經過端口區分)
        [root@localhost redis-4.0.10]# redis-server redis-6379.conf 
        [root@localhost redis-4.0.10]# redis-server redis-6380.conf 
        [root@localhost redis-4.0.10]# redis-server redis-6381.conf 
    2.準備三個哨兵,準備三個哨兵的配置文件(僅僅是端口的不一樣26379,26380,26381)
        -rw-r--r--  1 root root    227 Jan  2 18:44 redis-sentinel-26379.conf
    
        touch redis-sentinel-26379.conf
        vim redis-sentinel-26379.conf
        寫入如下
        port 26379  
        dir /var/redis/data/
        logfile "26379.log"

        sentinel monitor s15master 127.0.0.1 6379 2

        sentinel down-after-milliseconds s15master 30000

        sentinel parallel-syncs s15master 1

        sentinel failover-timeout s15master 180000
        daemonize yes


    -rw-r--r--  1 root root    227 Jan  2 18:45 redis-sentinel-26380.conf
        快速生成配置文件
        sed "s/26379/26380/g" redis-sentinel-26379.conf >  redis-sentinel-26380.conf 
    -rw-r--r--  1 root root    227 Jan  2 18:46 redis-sentinel-26381.conf
        sed "s/26379/26381/g" redis-sentinel-26379.conf >  redis-sentinel-26381.conf 

    3.添加後臺運行參數,使得三個哨兵進程,後臺運行
        [root@localhost redis-4.0.10]# echo "daemonize yes" >> redis-sentinel-26379.conf 
        [root@localhost redis-4.0.10]# echo "daemonize yes" >> redis-sentinel-26380.conf 
        [root@localhost redis-4.0.10]# echo "daemonize yes" >> redis-sentinel-26381.conf 

    4.啓動三個哨兵
          redis-sentinel redis-sentinel-26379.conf 
          redis-sentinel redis-sentinel-26380.conf 
          redis-sentinel redis-sentinel-26381.conf 
        
    5.檢查哨兵的通訊狀態
    redis-cli -p 26379  info sentinel 
    查看結果以下以後,表示哨兵正常
        [root@localhost redis-4.0.10]# redis-cli -p 26379  info sentinel 
        # Sentinel
        sentinel_masters:1
        sentinel_tilt:0
        sentinel_running_scripts:0
        sentinel_scripts_queue_length:0
        sentinel_simulate_failure_flags:0
        master0:name=s15master,status=ok,address=127.0.0.1:6381,slaves=2,sentinels=3

    6.殺死一個redis主庫,6379節點,等待30s之內,檢查6380和6381的節點狀態
    kill 6379主節點
    redis-cli -p 6380 info replication 
    redis-cli -p 6381 info replication 
    若是切換的主從身份以後,(原理就是更改redis的配置文件,切換主從身份)
    
    7.恢復6379節點的數據庫,查看是否將6379添加爲新的slave身份
    
 
redis-cluster安裝配置
1.準備6個redis數據庫實例,準備6個配置文件redis-{7000....7005}配置文件

    [root@localhost redis-4.0.10]# mkdir s15rediscluster
    cd s15rediscluster
    touch redis-7000.conf
    vim redis-7000.conf
    寫入如下
        port 7000
        daemonize yes
        dir "/opt/redis/data"
        logfile "7000.log"
        dbfilename "dump-7000.rdb"
        cluster-enabled yes
        cluster-config-file nodes-7000.conf
    建立六個
        [root@localhost s15rediscluster]# sed "s/7000/7001/g" redis-7000.conf >redis-7001.conf
        [root@localhost s15rediscluster]# sed "s/7000/7002/g" redis-7000.conf >redis-7002.conf
        [root@localhost s15rediscluster]# sed "s/7000/7003/g" redis-7000.conf >redis-7003.conf
        [root@localhost s15rediscluster]# sed "s/7000/7004/g" redis-7000.conf >redis-7004.conf
        [root@localhost s15rediscluster]# sed "s/7000/7005/g" redis-7000.conf >redis-7005.conf

2.啓動6個redis數據庫實例
    [root@localhost s15rediscluster]# redis-server redis-7000.conf 
    [root@localhost s15rediscluster]# redis-server redis-7001.conf 
    [root@localhost s15rediscluster]# redis-server redis-7002.conf 
    [root@localhost s15rediscluster]# redis-server redis-7003.conf 
    [root@localhost s15rediscluster]# redis-server redis-7004.conf 
    [root@localhost s15rediscluster]# redis-server redis-7005.conf 

3.配置ruby語言環境,腳本一鍵啓動redis-cluster 
    1.下載ruby語言的源碼包,編譯安裝
        wget https://cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
    2.解壓縮   tar -zxvf ruby-2.3.1.tar.gz
        ./configure --prefix=/opt/ruby/       釋放makefile
        make && make install     編譯且安裝
    3.下載安裝ruby操做redis的模塊包
        cd  /opt 
        wget http://rubygems.org/downloads/redis-3.3.0.gem
        
        /opt/ruby/bin/gem install -l redis-3.3.0.gem
    4.配置ruby的環境變量
    echo $PATH
    
    vim /etc/profile
    寫入最底行
    PATH=$PATH:/opt/ruby/bin/
    讀取文件
    source /etc/profile 
    
    5.經過ruby的包管理工具去安裝redis包,安裝後會生成一個redis-trib.rb這個命令
    一鍵建立redis-cluster 其實就是分配主從關係 以及 槽位分配 slot槽位分配
    /opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
    
    6.檢查節點主從狀態
    redis-cli -p 7000  info replication 
    
    7.向redis集羣寫入數據,查看數據流向
    redis-cli -p 7000    #這裏會將key自動的重定向,放到某一個節點的slot槽位中
    set  name  s15 
    set  addr shahe 
相關文章
相關標籤/搜索