Redis7.2.x主從復制的實現(xiàn)示例
更新時間:2024年06月07日 09:22:02 作者:夢·D·
本文主要介紹了Redis7.2.x主從復制的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
IP | 操作系統(tǒng) | 服務 | 版本 |
192.168.140.153 | CentOS 7 | remaster | 7.2.5 |
192.168.140.159 | CentOS 7 | redis-slave | 7.2.5 |
一、安裝依賴
yum -y install gcc gcc-c++
二、安裝Redis
1、下載安裝包
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
2、解壓
tar -zxvf redis-7.2.5.tar.gz -C /opt/
3、創(chuàng)建工作目錄
mkdir -p /opt/redis/ mkdir -p /opt/redis/logs/
4、編譯安裝
# 進入解壓目錄 cd /opt/redis-7.2.5 # 編譯 make # 安裝 make install PREFIX=/opt/redis/
5、修改配置文件(Master)
cp /opt/redis-7.2.5/redis.conf /opt/redis/ vi /opt/redis/redis.conf # 修改如下內(nèi)容 bind 192.168.140.153 -::1 # 允許后臺運行 daemonize yes # pid文件位置 pidfile /opr/redis/redis_6379.pid # 日志位置 logfile "/opt/redis/logs/redis.log" # 工作目錄 dir /opt/redis/ # 主從同步密碼 masterauth final123 # redis密碼 requirepass final123
6、修改配置文件(Slave)
cp /opt/redis-7.2.5/redis.conf /opt/redis/ vi /opt/redis/redis.conf # 修改如下內(nèi)容 bind 192.168.140.153 -::1 # 允許后臺運行 daemonize yes # pid文件位置 pidfile /opr/redis/redis_6379.pid # 日志位置 logfile "/opt/redis/logs/redis.log" # 工作目錄 dir /opt/redis/ # 主從同步密碼 masterauth final123 # redis密碼 requirepass final123 replilcaof 192.168.140.153 6379
7、創(chuàng)建啟動腳本
[root@localhost redis]# vi /etc/systemd/system/redis.service [Unit] Description=Redis After=network.target [Service] Type=simple User=root Group=root ExecStart=/opt/redis/bin/redis-server /opt/redis/redis.conf --daemonize no Restart=always LimitNPROC=65535 LimitNOFILE=65535 [Install] WantedBy=multi-user.target
8、啟動服務
systemctl daemon-reload systemctl start redis.service systemctl enable redis.service
三、測試
1、配置環(huán)境變量
[root@localhost redis]# vi /etc/profile # 在最后添加如下內(nèi)容 PATH=${PATH}:/opt/redis/bin/ [root@localhost redis]# source /etc/profile
2、查看redis-master
[root@localhost redis]# redis-cli -h 192.168.140.153 192.168.140.153:6379> auth final123 OK 192.168.140.153:6379> info replication # Replication role:master connected_slaves:1 slave0:ip=192.168.140.159,port=6379,state=online,offset=350,lag=1 master_failover_state:no-failover master_replid:28a3b660f11504c07b2cc4bc07a093970af1544b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:350 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:350
3、查看redis-slave
[root@localhost redis]# redis-cli -h 192.168.140.159 192.168.140.159:6379> auth final123 OK 192.168.140.159:6379> info replication # Replication role:slave master_host:192.168.140.153 master_port:6379 master_link_status:up master_last_io_seconds_ago:6 master_sync_in_progress:0 slave_read_repl_offset:406 slave_repl_offset:406 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:28a3b660f11504c07b2cc4bc07a093970af1544b master_replid2:0000000000000000000000000000000000000000 master_repl_offset:406 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:406
4、master節(jié)點插入數(shù)據(jù)
192.168.140.153:6379> set test "master" OK 192.168.140.153:6379> get test "master"
5、slave節(jié)點查看數(shù)據(jù)是否同步
192.168.140.159:6379> get test "master" # slave節(jié)點只能讀取,不能寫入。 192.168.140.159:6379> set test1 "ceshi" (error) READONLY You can't write against a read only replica.
到此這篇關(guān)于Redis7.2.x主從復制的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Redis7.2.x主從復制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
redis實現(xiàn)sentinel哨兵架構(gòu)的方法
哨兵是一個分布式系統(tǒng),可以在一個架構(gòu)中運行多個哨兵(sentinel) 進程,這些進程使用流言協(xié)議(gossip protocols)來接收關(guān)于Master主服務器是否下線的信息,這篇文章主要介紹了redis實現(xiàn)sentinel哨兵架構(gòu),需要的朋友可以參考下2022-11-11解讀Redis秒殺優(yōu)化方案(阻塞隊列+基于Stream流的消息隊列)
該文章介紹了使用Redis的阻塞隊列和Stream流的消息隊列來優(yōu)化秒殺系統(tǒng)的方案,通過將秒殺流程拆分為兩條流水線,使用Redis緩存緩解數(shù)據(jù)庫壓力,并結(jié)合Lua腳本進行原子性判斷,使用阻塞隊列和消息隊列異步處理訂單,有效提高了系統(tǒng)的并發(fā)處理能力和可用性2025-02-02redis中opsForList().range()的使用方法詳解
這篇文章主要給大家介紹了關(guān)于redis中opsForList().range()的使用方法,文中通過實例代碼以及圖文介紹的非常詳細,對大家學習或者使用redis具有一定的參考學習價值,需要的朋友可以參考下2023-03-03