一、基本信息
官方文檔: https://redis.io/documentation
中文網: http://www.redis.cn/
中文論壇: http://bbs.redis.cn/forum.php
Redis教程: https://www.runoob.com/redis/redis-tutorial.html
中文教程: http://www.redis.com.cn/
中文社區: https://ruby-china.org/topics/node10
Redis社區: http://www.redis.cn/community.html
二、概述
在現有企業中80%公司大部分使用的是redis單機服務,在實際的場景當中單一節點的redis容易面臨風險。
面臨問題
1、機器故障。我們部署到一臺 Redis 服務器,當發生機器故障時,需要遷移到另外一臺服務器並且要保證數據是同步的。而數據是最重要的,如果你不在乎,基本上也就不會使用 Redis 了。
2、容量瓶頸。當我們有需求需要擴容 Redis 內存時,從 16G 的內存升到 64G,單機肯定是滿足不了。當然,你可以重新買個 128G 的新機器。
解決辦法
要實現分佈式數據庫的更大的存儲容量和承受高併發訪問量,我們會將原來集中式數據庫的數據分別存儲到其他多個網絡節點上。
Redis 爲了解決這個單一節點的問題,也會把數據複製多個副本部署到其他節點上進行復制,實現 Redis的高可用,實現對數據的冗餘備份,從而保證數據和服務的高可用。
三、主從複製的作用
1、數據冗餘:主從複製實現了數據的熱備份,是持久化之外的一種數據冗餘方式。
2、故障恢復:當主節點出現問題時,可以由從節點提供服務,實現快速的故障恢復;實際上是一種服務的冗餘。
3、負載均衡:在主從複製的基礎上,配合讀寫分離,可以由主節點提供寫服務,由從節點提供讀服務(即寫Redis數據時應用連接主節點,讀Redis數據時應用連接從節點),分擔服務器負載;尤其是在寫少讀多的場景下,通過多個從節點分擔讀負載,可以大大提高Redis服務器的併發量。
4、讀寫分離:可以用於實現讀寫分離,主庫寫、從庫讀,讀寫分離不僅可以提高服務器的負載能力,同時可根據需求的變化,改變從庫的數量。
5、高可用基石:除了上述作用以外,主從複製還是哨兵和集羣能夠實施的基礎,因此說主從複製是Redis高可用的基礎。
主從複製啓用
從節點開啓主從複製,有3種方式:
1、配置文件:在從服務器的配置文件中加入 replicaof (低版本: slaveof)。
2、啓動命令:redis-server啓動命令後加入 --replicaof (低版本: slaveof)。
3、客戶端命令:Redis服務器啓動後,直接通過客戶端執行命令 replicaof (低版本: slaveof),則該Redis實例成爲從節點。
四、系統及工具
1、系統信息
系統 | IP | 內存 | CPU | 程序 |
CentOS-7-x86_64-Minimal-1810.iso | 192.168.11.18 | 2G | 1核 | redis-6.0.5.tar.gz |
CentOS-7-x86_64-Minimal-1810.iso | 192.168.11.19 | 2G | 1核 | redis-6.0.5.tar.gz |
系統鏡像下載地址:
http://archive.kernel.org/centos-vault/7.4.1708/isos/x86_64/
虛擬機安裝過程參考:
https://blog.csdn.net/llwy1428/article/details/89328381
2、VMware 版本:VMware Workstation Pro15
3、工具:xshell5
五、安裝、部署、配置
1、安裝基本工具
[root@localhost ~]# yum install gcc gcc-c++ wget vim net-tools nmap lrasz tree make tcl -y
2、修改 gcc 版本
[root@localhost ~]# yum -y install centos-release-scl
[root@localhost ~]# yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
[root@localhost ~]# scl enable devtoolset-9 bash
3、創建目錄並下載
[root@localhost ~]# mkdir /opt/redis
[root@localhost ~]# cd /opt/
[root@localhost opt]# wget http://download.redis.io/releases/redis-6.0.5.tar.gz
4、解壓文件
[root@localhost opt]# tar zxvf redis-6.0.5.tar.gz
5、編譯、安裝(我安裝在 /opt/redis 路徑下)
[root@localhost ~]# cd /opt/redis-6.0.5
[root@localhost redis-6.0.5]# make
[root@localhost redis-6.0.5]# make test
有可能會報如下錯誤
如報錯,則執行 make distclean
[root@localhost redis-6.0.5]# make distclean
繼續執行編譯安裝(我安裝在 /opt/redis 路徑下)
[root@localhost redis-6.0.5]# make PREFIX=/opt/redis install
查看安裝結果
6、複製 redis 中的 redis.conf 文件到安裝目錄下
[root@localhost ~]# cp /opt/redis-6.0.5/redis.conf /opt/redis
創建日誌目錄
[root@localhost ~]# mkdir /opt/redis/log
創建數據目錄
[root@localhost ~]# mkdir /opt/redis/data
7、把本節點(主節點)上的安裝文件發送至另一節點(從節點)上
[root@localhost ~]# scp -r /opt/redis/* 192.168.11.19:/opt/redis
8、編輯配置內容
[root@localhost ~]# vim /opt/redis/redis.conf
(1)bind 0.0.0.0 127.0.0.1
(2)daemonize yes
(3)pidfile /opt/redis/redis_6379.pid
(4)logfile /opt/redis/redis.log
(5)dir /opt/redis/data
(6)requirepass 123456 #本機密碼
(7)#<masterip> <masterport> 主節點 Redis 的 IP 和端口 注:低版本 Redis 有可能是 slaveof (只在從節點配置)
replicaof 192.168.11.18
(8)#<master-password> 主節點上的密碼 (只在從節點配置)
masterauth 123456
說明:
#daemonize no 修改爲daemonize yes 意爲開啓後臺運行
#protected-mode yes 去掉#號,意爲開啓保護模式,可以選擇綁定ip,也可以指定密碼
dir ./ 修改爲dir /opt/redis6/data 意爲把指定redis的數據存放目錄
logfile 這行修改爲 /opt/redis/log/redis-6379.log # 指定日誌存放目錄
# requirepass 本機密碼
# replicaof <masterip> <masterport> # 主從複製
# masterauth <master-password> # 主節點密碼
9、主節點啓動服務
[root@localhost ~]# /opt/redis/bin/redis-server /opt/redisredis.conf
10、主節點測試
進入命令行
[root@localhost ~]# /opt/redis/bin/redis-cli
輸入配置文件中的密碼
auth 123456
11、從節點啓動
[root@localhost ~]# /opt/redis/bin/redis-server /opt/redis/redis.conf
12、在主節點啓動的情況下,測試從節點
從節點進入命令行
[root@localhost ~]# /opt/redis/bin/redis-cli
在主節點
從節點
13、設置服務開機啓動
https://blog.csdn.net/llwy1428/article/details/106741119
至此,Centos7 安裝配置 Redis 並配置主從複製操作完畢!
六、參考地址:
Centos7.x 搭建 Redis v5.0-單機版-僞集羣
https://blog.csdn.net/llwy1428/article/details/93659772
Centos7.2 安裝單機版 Redis 5.0 (編譯安裝)
https://blog.csdn.net/llwy1428/article/details/93612187
Redis可視化工具