redis安裝



redis簡介

redis是一個key-value存儲系統。和Memcached類似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)和zset(有序集合)。這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支持各種不同方式的排序。與memcached一樣,為了保證效率,數據都是緩存在內存中。區別的是redis會周期性的把更新的數據寫入磁盤或者把修改操作寫入追加的記錄文件,並且在此基礎上實現了master-slave(主從)同步。Redis 是一個高性能的key-value數據庫。 redis的出現,很大程度補償了memcached這類keyvalue存儲的不足,在部分場合可以對關系數據庫起到很好的補充作用。

單點redis 安裝

必須使用root用戶,不然會出現權限問題


1、上傳redis-2.8.19.tar.gz
2、解壓 tar -zxvf redis-2.8.19.tar.gz -C apps/
3、重命名 mv redis-2.8.19/ redis
4、進入目錄 cd redis/
5、make test 如果最后結果如下則沒有錯誤

\o/ All tests passed without errors!

Cleanup: may take some time... OK
make[1]: Leaving directory `/home/hadoop/apps/redis/src'

6、編譯 sudo make,編譯中會出現警告,可以忽略
7、安裝 sudo make install,會出現建議執行test,可以忽略
8、查看默認安裝的命令 ls /usr/local/bin/ | grep redis
9、啟動,由於/usr/local/bin/默認就在系統環境變量,所以直接可以執行redis-server就可以默認啟動了,或者 nohup redis-server &后台啟動
10、 查看redis服務進程 ps | grep redis
11、啟動客戶端 redis-cli
12、輸入info,此時可以查看redis相關信息,至此redis單機版已經安裝成功
13、停止服務redis-cli shutdown


redis主從配置

Redis的主從復制功能非常強大,一個master可以擁有多個slave,而一個slave又可以擁有多個slave,如此下去,形成了強大的多級服務器集群架構. 可以避免Redis單點故障,構建讀寫分離架構,滿足讀多寫少的應用場景.

Redis復制功能的幾個重要方面
1. 一個Master可以有多個Slave;
2. Redis使用異步復制。從2.8開始,Slave會周期性(每秒一次)發起一個Ack確認復制流(replication stream)被處理進度;
3. 不僅主服務器可以有從服務器,從服務器也可以有自己的從服務器,多個從服務器之間可以構成一個圖狀結構;
4. 復制在Master端是非阻塞模式的,這意味着即便是多個Slave執行首次同步時,Master依然可以提供查詢服務;
5.復制在Slave端也是非阻塞模式的:如果你在redis.conf做了設置,Slave在執行首次同步的時候仍可以使用舊數據集提供查詢;你也可以配置為當Master與Slave失去聯系時,讓Slave返回客戶端一個錯誤提示;
6. 當Slave要刪掉舊的數據集,並重新加載新版數據時,Slave會阻塞連接請求(一般發生在與Master斷開重連后的恢復階段);
7.復制功能可以單純地用於數據冗余(dataredundancy),也可以通過讓多個從服務器處理只讀命令請求來提升擴展性(scalability):比如說,繁重的 SORT 命令可以交給附屬節點去運行。
8. 可以通過修改Master端的redis.conf來避免在Master端執行持久化操作(Save),由Slave端來執行持久化。
9、默認情況下redis數據庫充當slave角色時是只讀的不能進行寫操作。


具體安裝配置
1、上面安裝的redis作為master,在另外一台機器上安裝一個redis
2、配置從節點有兩種方式,一種是使用配置文件redis.conf,另外一種是使用客戶端命令


命令行測試
通過命令行slaveof <masterip> <masterport>

INFO replication 查看當前redis的相關副本信息

# Replication
#當前角色為slave
role:slave
#master的地址為m1
master_host:m1
#master的端口為6379
master_port:6379
#master沒有啟動所以這里為down,否則為up
master_link_status:down

通過配置文件(redis.conf)指定
在從節點修改配置文件添加slaveof <masterip> <masterport>

# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition slaves automatically try to reconnect to masters
# and resynchronize with them.
#
slaveof m1 6379

# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.

帶配置文件啟動redis服務

redis-server redis.conf

此處也啟動master redis服務

使用 INFO replication查看master 和 slave節點的相關信息

查看master節點的信息

127.0.0.1:6379> INFO replication
# Replication
#角色master
role:master
#1 slave 連接
connected_slaves:1
# slave的相關信息
slave0:ip=192.168.19.101,port=6379,state=online,offset=141,lag=0
master_repl_offset:141
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:140

查看slave的相關信息

127.0.0.1:6379> INFO replication
# Replication
#角色slave
role:slave
#masetr m1
master_host:m1
#端口 6379
master_port:6379
# 已連接上master則顯示up
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_repl_offset:239
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>

測試數據

在master端設置數據

127.0.0.1:6379> set key value 

在slave查看

127.0.0.1:6379> get key
"value" 

ok,redis主從節點已經配置好了

Redis哨兵模式

如果我們在使用主從復制的情況下,Master服務器進行了down機的情況,我們的系統就不能再進行寫的操作,所以此時redis在2.6版本引入了哨兵模式,但是並不穩定,2.8版本之后哨兵模式才穩定了起來。
顧名思義Redis的哨兵模式就是對redis系統進行實時的監控,其主要功能有下面兩點
1.監測主數據庫和從數據庫是否正常運行。
2.當我們的主數據庫出現故障的時候,可以自動將從數據庫轉換為主數據庫,實現自動的切換。


具體安裝配置

1、上面我們已經安裝了兩台redis服務一台master和一台slave,現在需要安裝另外一台slave
2、安裝成功后修改redis.conf配置配置文件
3、redis-server redis.conf啟動redis服務
4、查看maser主節點的信息 INFO replication

127.0.0.1:6379> INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.19.101,port=6379,state=online,offset=2381,lag=0
slave1:ip=192.168.19.102,port=6379,state=online,offset=2381,lag=0
master_repl_offset:2381
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:2380
127.0.0.1:6379>

5、修改哨兵模式配置文件

此處只修改一下幾個參數

#哨兵使用的端口
port 26379

# 哨兵日志所在的目錄
dir /home/hadoop/apps/redis/logs

#對應的分別是名稱 ip 端口 至少選舉通過的票數

# 名稱 master節點 端口 票數
sentinel monitor mymaster m1 6379 1

# 代表主節點多少毫秒沒有響應就表示主節點掛掉了
sentinel down-after-milliseconds mymaster 5000

#哨兵進程重新配置從slave中選舉一個master,有多少個通節點可以和新的master進行數據同步,為了避免所有的從節點不能進行查詢服務應該設置一個小的數
sentinel parallel-syncs mymaster 2

注意票數不能大於slave的數量

6、啟動哨兵進程

nohup redis-server sentinel.conf --sentinel &

7、查看哨兵信息,redis-cli -h m3 -p 26379 info sentinel, -h指定哨兵啟動所在的機器的地址,哨兵可以啟動多個,這里只啟動一個

[hadoop@m3 redis]$ redis-cli -h m3 -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
#master 正常,所以status為ok
master0:name=mymaster,status=ok,address=192.168.19.100:6379,slaves=2,sentinels=1

8、停止master,查看

[hadoop@m3 redis]$ redis-cli -h m3 -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
#master 停止,所以status為sdown
master0:name=mymaster,status=sdown,address=192.168.19.100:6379,slaves=2,sentinels=1

此時master ip是192.168.19.100

9、停止master后不會立即切換的,因為默認超時時間是180000,3分鍾,所以需要等三分鍾后再次查看狀態信息

10、3分鍾后,查看狀態

[hadoop@m3 redis]$ redis-cli -h m3 -p 26379 info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
master0:name=mymaster,status=ok,address=192.168.19.102:6379,slaves=2,sentinels=1

此時master ip是192.168.19.102

 

錯誤1
運行 make test時報錯
You need tcl 8.5 or newer in order to run the Redis test
解決辦法,安裝新的tcl sudo yum install -y tcl

錯誤2
make install報下面錯誤:
install: cannot create regular file `/usr/local/bin/redis-server': Permission denied

原因是沒有使用root用戶,導致權限問題,使用root用戶權限即可

錯誤3
/bin/sh: cc: command not found
原因是沒有編譯的時候需要gcc環境,因此只要安裝gcc即可

錯誤4
error: #error "Newer version of jemalloc required"

原因是分配器問題,指定分分配器即可

make MALLOC=libc

 

 

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM