柚子快報(bào)邀請碼778899分享:緩存 【Redis 集群搭建】
柚子快報(bào)邀請碼778899分享:緩存 【Redis 集群搭建】
Redis 主從系統(tǒng)搭建【一主亮從】
目錄一、 同主機(jī)搭建redis 集群1、 環(huán)境介紹
一臺(tái)機(jī)器 一主兩從集群搭建2、 環(huán)境前準(zhǔn)備工作3、 安裝 Redis 7.2.54、redis 配置修改并且啟動(dòng)4.1 修改配置文件4.2 編寫啟動(dòng)腳本
5、開啟主從5.1 開啟5.2 主庫實(shí)例查看主從信息5.3 從庫實(shí)例查看主從信息5.4 驗(yàn)證主從配置是否生效
6、 解除 192.168.2.1:7003 實(shí)例主從
二、 跨節(jié)點(diǎn)部署Redis主從1. 環(huán)境介紹2. 修改配置文件2.1 master2.2 slave 12.3 slave 2
3.啟動(dòng)Redis3.1 master3.2 slave 13.3 slave 2
4. 開啟主從4.1 slave 14.2 slave 24.3 master
5. 驗(yàn)證主從配置是否生效
結(jié)束
目錄
一、 同主機(jī)搭建redis 集群
1、 環(huán)境介紹
一臺(tái)機(jī)器 一主兩從集群搭建
操作系統(tǒng)Centos 7內(nèi)核版本3.10.0-957.21.3.el7.x86_64IP地址master: 7001slave1: 7002 slave2: 7003Redis7.2.5
2、 環(huán)境前準(zhǔn)備工作
# 關(guān)閉防火墻
systemctl stop firewalld
systemctl disable firewalld
# 修改 hostname
hostnamectl set-hostname xxxx # 修改后退出當(dāng)前終端重新連接即可
# 更新下軟件源
# 地址 https://developer.aliyun.com/mirror/
# 時(shí)區(qū)調(diào)整,時(shí)間校準(zhǔn)
date -R
timedatectl set-timezone Asia/Shanghai
yum -y install ntp
ntpdate ntp1.aliyun.com
# 關(guān)閉 selinux:
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
3、 安裝 Redis 7.2.5
# 下載地址
http://download.redis.io/releases/
# 下載
wget http://download.redis.io/releases/redis-7.2.5.tar.gz
# 解壓
tar -xvf redis-7.2.5.tar.gz -C /opt/redis
# 進(jìn)入目錄
cd /opt/redis/redis-7.2.5
# 編譯
make MALLOC=libc
# 安裝 注意如果安裝提示python3不存在 執(zhí)行 yum install -y python3
make install PREFIX=/usr/local/redis
# 配置環(huán)境變量
vi /etc/profile.d/redis.sh
#!/bin/bash
export REDIS_HOME=/usr/local/redis
export PATH=$PATH:$REDIS_HOME/bin
# 使其配置生效
source /etc/profile.d/redis.sh
4、redis 配置修改并且啟動(dòng)
4.1 修改配置文件
# 創(chuàng)建redis數(shù)據(jù)存儲(chǔ)目錄
mkdir -p /usr/local/redis/data/700{1..3}
# 創(chuàng)建redis配置文件存儲(chǔ)目錄
mkdir -p /usr/local/redis/conf
# 創(chuàng)建redis日志存儲(chǔ)目錄
mkdir -p /usr/local/redis/log/700{1..3}
# 創(chuàng)建redis pid存儲(chǔ)目錄
mkdir /usr/local/redis/run
# redis配置文件 7001
# 開啟保護(hù)模式
protected-mode yes
# 添加本機(jī)的ip
bind 192.168.2.1
# 端口
port 7001
# pid存儲(chǔ)目錄
pidfile /usr/local/redis/run/redis_7001.pid
# 日志存儲(chǔ)目錄
logfile /usr/local/redis/log/16370/redis_7001.log
# 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好
dir /usr/local/redis/data/7001
# 設(shè)置實(shí)例的驗(yàn)證口令
requirepass 你的密碼
# 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。
# 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致
masterauth 你的密碼
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 守護(hù)進(jìn)程
daemonize yes
# redis配置文件 7002# 開啟保護(hù)模式
protected-mode yes
# 添加本機(jī)的ip
bind 192.168.2.1
# 端口
port 7002
# pid存儲(chǔ)目錄
pidfile /usr/local/redis/run/redis_7002.pid
# 日志存儲(chǔ)目錄
logfile /usr/local/redis/log/16370/redis_7002.log
# 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好
dir /usr/local/redis/data/7002
# 設(shè)置實(shí)例的驗(yàn)證口令
requirepass 你的密碼
# 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。
# 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致
masterauth 你的密碼
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 守護(hù)進(jìn)程
daemonize yes
# redis配置文件 7003
# 開啟保護(hù)模式
protected-mode yes
# 添加本機(jī)的ip
bind 192.168.2.1
# 端口
port 7003
# pid存儲(chǔ)目錄
pidfile /usr/local/redis/run/redis_7003.pid
# 日志存儲(chǔ)目錄
logfile /usr/local/redis/log/16370/redis_7003.log
# 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好
dir /usr/local/redis/data/7003
# 設(shè)置實(shí)例的驗(yàn)證口令
requirepass 你的密碼
# 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。
# 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致
masterauth 你的密碼
# 配置RDB持久化策略
save 900 1
save 300 10
save 60 10000
# 配置AOF持久化
appendonly yes
appendfsync everysec
# 守護(hù)進(jìn)程
daemonize yes
# 復(fù)制下面命令即可
cat > /usr/local/redis/conf/redis_7001.conf < # 開啟保護(hù)模式 protected-mode yes # 添加本機(jī)的ip bind 192.168.2.1 # 端口 port 7001 # pid存儲(chǔ)目錄 pidfile /usr/local/redis/run/redis_7001.pid # 日志存儲(chǔ)目錄 logfile /usr/local/redis/log/16370/redis_7001.log # 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好 dir /usr/local/redis/data/7001 # 設(shè)置實(shí)例的驗(yàn)證口令 requirepass 你的密碼 # 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。 # 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致 masterauth 你的密碼 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec # 守護(hù)進(jìn)程 daemonize yes EOF cat > /usr/local/redis/conf/redis_7002.conf < # 開啟保護(hù)模式 protected-mode yes # 添加本機(jī)的ip bind 192.168.2.1 # 端口 port 7002 # pid存儲(chǔ)目錄 pidfile /usr/local/redis/run/redis_7002.pid # 日志存儲(chǔ)目錄 logfile /usr/local/redis/log/16370/redis_7002.log # 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好 dir /usr/local/redis/data/7002 # 設(shè)置實(shí)例的驗(yàn)證口令 requirepass 你的密碼 # 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。 # 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致 masterauth 你的密碼 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec # 守護(hù)進(jìn)程 daemonize yes EOF cat > /usr/local/redis/conf/redis_7003.conf < # 開啟保護(hù)模式 protected-mode yes # 添加本機(jī)的ip bind 192.168.2.1 # 端口 port 7003 # pid存儲(chǔ)目錄 pidfile /usr/local/redis/run/redis_7003.pid # 日志存儲(chǔ)目錄 logfile /usr/local/redis/log/16370/redis_7003.log # 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好 dir /usr/local/redis/data/7003 # 設(shè)置實(shí)例的驗(yàn)證口令 requirepass 你的密碼 # 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。 # 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致 masterauth 你的密碼 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec # 守護(hù)進(jìn)程 daemonize yes EOF 4.2 編寫啟動(dòng)腳本 # 啟動(dòng)腳本 cat > /usr/local/redis/bin/master-slave_start.sh << EOF #!/bin/bash redis-server /usr/local/redis/conf/redis_7001.conf redis-server /usr/local/redis/conf/redis_7002.conf redis-server /usr/local/redis/conf/redis_7003.conf EOF # 賦予可執(zhí)行權(quán)限 chmod +x /usr/local/redis/bin/redis_cluster_start.sh # 關(guān)閉腳本 vi /usr/local/redis/bin/redis_cluster_shutdown.sh #!/bin/bash kill -9 $(ps -ef | grep 1637 | grep -v grep | awk '{print $2}') # 賦予可執(zhí)行權(quán)限 chmod +x /usr/local/redis/bin/master-slave_shutdown.sh 5、開啟主從 5.1 開啟 # 以"192.168.2.1:7001"實(shí)例為主庫,以"192.168.2.1:7002"實(shí)例和"192.168.2.1:7003"實(shí)例為從庫。 # -a 參數(shù)指定密碼 # 從庫實(shí)例開啟主從 redis-cli -h 192.168.2.1 -p 7002 -a 你的密碼 slaveof 192.168.2.1 7001 redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 slaveof 192.168.2.1 7001 # Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # 警告:在命令行界面上使用帶有“-a”或“-u”選項(xiàng)的密碼可能不安全。 可以忽略 # 解決方式 # redis-cli -h 192.168.2.1 -p 7002 -a 你的密碼 --no-auth-warning slaveof 192.168.2.1 7001 5.2 主庫實(shí)例查看主從信息 redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.2.1,port=7002,state=online,offset=8428,lag=0 slave1:ip=192.168.2.1,port=7003,state=online,offset=8428,lag=1 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:8428 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:8428 5.3 從庫實(shí)例查看主從信息 # 192.168.2.1:7002 查看主從信息 redis-cli -h 192.168.2.1 -p 7002 -a 你的密碼 info replication [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7002 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.2.1 master_port:7001 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_read_repl_offset:8680 slave_repl_offset:8680 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:8680 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:8666 5.4 驗(yàn)證主從配置是否生效 # "192.168.2.1:7001"實(shí)例主庫的11號數(shù)據(jù)庫創(chuàng)建測試數(shù)據(jù) redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 -n 11 [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.2.1:7001[11]> keys * (empty array) 192.168.2.1:7001[11]> set name xiaoxiao OK 192.168.2.1:7001[11]> set age 18 OK 192.168.2.1:7001[11]> keys * 1) "age" 2) "name" # "192.168.2.1:7002"實(shí)例從庫的11號數(shù)據(jù)庫查看是否同步主庫的11號數(shù)據(jù)庫 redis-cli -h 192.168.2.1 -p 7002 -a 你的密碼 -n 11 [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7002 -a 你的密碼 -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.2.1:7002[11]> keys * 1) "name" 2) "age" 192.168.2.1:7002[11]> get name "zhangsan" 192.168.2.1:7002[11]> get age "18" 192.168.2.1:7002[11]> # "192.168.2.1:7003"實(shí)例從庫的11號數(shù)據(jù)庫查看是否同步主庫的11號數(shù)據(jù)庫 redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 -n 11 [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.2.1:7003[11]> keys * 1) "age" 2) "name" 192.168.2.1:7003[11]> get name "zhangsan" 192.168.2.1:7003[11]> get age "18" 192.168.2.1:7003[11]> 6、 解除 192.168.2.1:7003 實(shí)例主從 # 192.168.2.1:7003 查看主從信息 redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 info replication [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.2.1 master_port:7001 master_link_status:up master_last_io_seconds_ago:4 master_sync_in_progress:0 slave_read_repl_offset:10003 slave_repl_offset:10003 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10003 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:155 repl_backlog_histlen:9849 # 192.168.2.1:7001 查看主從信息 redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.2.1,port=7002,state=online,offset=10157,lag=0 slave1:ip=192.168.2.1,port=7003,state=online,offset=10157,lag=0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10157 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:10157 # 解除 192.168.2.1:7003 主從信息 redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 slaveof no one [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 slaveof no one Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 再次查看 192.168.2.1:7001 查看主從信息 redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:1 slave0:ip=192.168.2.1,port=7002,state=online,offset=10731,lag=0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10731 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:10731 # 只剩下一個(gè) 從節(jié)點(diǎn)了 # 把192.168.2.1:7003 添加回來 redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 slaveof 192.168.2.1 7001 [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7003 -a 你的密碼 slaveof 192.168.2.1 7001 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 此時(shí)再看192.168.2.1:7001 查看主從信息 redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication [root@master-slave ~]# redis-cli -h 192.168.2.1 -p 7001 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.2.1,port=7002,state=online,offset=10941,lag=0 slave1:ip=192.168.2.1,port=7003,state=online,offset=10941,lag=0 master_failover_state:no-failover master_replid:a292603e2c96a6f451055e6d84a60795f81f2928 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:10941 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:10941 # slaves 變成2了 二、 跨節(jié)點(diǎn)部署Redis主從 1. 環(huán)境介紹 操作系統(tǒng)Centos 7Centos 7Centos 7內(nèi)核版本3.10.0-957.21.3.el7.x86_643.10.0-957.21.3.el7.x86_643.10.0-957.21.3.el7.x86_64IP地址master: 16370 slave1: 16371 slave2: 16372master: 16370 slave1: 16371 slave2: 16372master: 16370 slave1: 16371 slave2: 16372Redis7.2.57.2.57.2.5 2. 修改配置文件 2.1 master # 創(chuàng)建redis數(shù)據(jù)存儲(chǔ)目錄 mkdir -p /usr/local/redis/data/16380 # 創(chuàng)建redis配置文件存儲(chǔ)目錄 mkdir -p /usr/local/redis/conf/16380 # 創(chuàng)建redis日志存儲(chǔ)目錄 mkdir -p /usr/local/redis/log/16380 # 創(chuàng)建redis pid存儲(chǔ)目錄 mkdir /usr/local/redis/run # 復(fù)制下面命令即可 cat > /usr/local/redis/conf/redis_16380.conf< # 開啟保護(hù)模式 protected-mode yes # 添加本機(jī)的ip bind 192.168.1.100 # 端口 port 16380 # pid存儲(chǔ)目錄 pidfile /usr/local/redis/run/redis_16380.pid # 日志存儲(chǔ)目錄 logfile /usr/local/redis/log/redis_16380.log # 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好 dir /usr/local/redis/data/16380 # 設(shè)置實(shí)例的驗(yàn)證口令 requirepass 你的密碼 # 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。 # 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致 masterauth 你的密碼 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec # 守護(hù)進(jìn)程 daemonize yes EOF 2.2 slave 1 # 創(chuàng)建redis數(shù)據(jù)存儲(chǔ)目錄 mkdir -p /usr/local/redis/data/16381 # 創(chuàng)建redis配置文件存儲(chǔ)目錄 mkdir -p /usr/local/redis/conf/16381 # 創(chuàng)建redis日志存儲(chǔ)目錄 mkdir -p /usr/local/redis/log/16381 # 創(chuàng)建redis pid存儲(chǔ)目錄 mkdir /usr/local/redis/run # 復(fù)制下面命令即可 cat > /usr/local/redis/conf/redis_16381.conf < # 開啟保護(hù)模式 protected-mode yes # 添加本機(jī)的ip bind 192.168.1.200 # 端口 port 16381 # pid存儲(chǔ)目錄 pidfile /usr/local/redis/run/redis_16381.pid # 日志存儲(chǔ)目錄 logfile /usr/local/redis/log/redis_16381.log # 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好 dir /usr/local/redis/data/16381 # 設(shè)置實(shí)例的驗(yàn)證口令 requirepass 你的密碼 # 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。 # 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致 masterauth 你的密碼 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec # 守護(hù)進(jìn)程 daemonize yes EOF 2.3 slave 2 # 創(chuàng)建redis數(shù)據(jù)存儲(chǔ)目錄 mkdir -p /usr/local/redis/data/16382 # 創(chuàng)建redis配置文件存儲(chǔ)目錄 mkdir -p /usr/local/redis/conf/16382 # 創(chuàng)建redis日志存儲(chǔ)目錄 mkdir -p /usr/local/redis/log/16382 # 創(chuàng)建redis pid存儲(chǔ)目錄 mkdir /usr/local/redis/run # 復(fù)制下面命令即可 cat > /usr/local/redis/conf/redis_16382.conf < # 開啟保護(hù)模式 protected-mode yes # 添加本機(jī)的ip bind 192.168.1.250 # 端口 port 16382 # pid存儲(chǔ)目錄 pidfile /usr/local/redis/run/redis_16382.pid # 日志存儲(chǔ)目錄 logfile /usr/local/redis/log/redis_16382.log # 數(shù)據(jù)存儲(chǔ)目錄,目錄要提前創(chuàng)建好 dir /usr/local/redis/data/16382 # 設(shè)置實(shí)例的驗(yàn)證口令 requirepass 你的密碼 # 以認(rèn)證的方式連接到master。 如果master中使用了“密碼保護(hù)”,slave必須交付正確的授權(quán)密碼,才能連接成功。 # 此配置項(xiàng)中值需要和master機(jī)器的“requirepass”保持一致 masterauth 你的密碼 # 配置RDB持久化策略 save 900 1 save 300 10 save 60 10000 # 配置AOF持久化 appendonly yes appendfsync everysec # 守護(hù)進(jìn)程 daemonize yes EOF 3.啟動(dòng)Redis 3.1 master redis-server /usr/local/redis/conf/redis_16380.conf 3.2 slave 1 redis-server /usr/local/redis/conf/redis_16381.conf 3.3 slave 2 redis-server /usr/local/redis/conf/redis_16382.conf 4. 開啟主從 4.1 slave 1 # 從庫開啟主從 redis-cli -h 192.168.1.200 -p 16381 -a 你的密碼 slaveof 192.168.1.100 16380 [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a 你的密碼 slaveof 192.168.1.100 16380 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 從庫查看主從信息 redis-cli -h 192.168.1.200 -p 16381 -a 你的密碼 info replication [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.1.100 master_port:16380 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_read_repl_offset:532 slave_repl_offset:532 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:7330881025ee2709ee6c9c32ea3fcc9b6649893d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:532 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:518 4.2 slave 2 # 從庫開啟主從 redis-cli -h 192.168.1.250 -p 16382 -a 你的密碼 slaveof 192.168.1.100 16380 [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a 你的密碼 slaveof 192.168.1.100 16380 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. OK # 從庫查看主從信息 redis-cli -h 192.168.1.250 -p 16382 -a 你的密碼 info replication [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:slave master_host:192.168.1.100 master_port:16380 master_link_status:up master_last_io_seconds_ago:10 master_sync_in_progress:0 slave_read_repl_offset:560 slave_repl_offset:560 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:7330881025ee2709ee6c9c32ea3fcc9b6649893d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:560 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:211 repl_backlog_histlen:350 4.3 master # 查看 主從信息 redis-cli -h 192.168.1.100 -p 16380 -a 你的密碼 info replication [root@master ~]# redis-cli -h 192.168.1.100 -p 16380 -a 你的密碼 info replication Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. # Replication role:master connected_slaves:2 slave0:ip=192.168.1.200,port=16381,state=online,offset=336,lag=0 slave1:ip=192.168.1.250,port=16382,state=online,offset=336,lag=1 master_failover_state:no-failover master_replid:7330881025ee2709ee6c9c32ea3fcc9b6649893d master_replid2:0000000000000000000000000000000000000000 master_repl_offset:336 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:336 5. 驗(yàn)證主從配置是否生效 # "master主機(jī)名"實(shí)例主庫的11號數(shù)據(jù)庫創(chuàng)建測試數(shù)據(jù) redis-cli -h 192.168.1.100 -p 16380 -a 你的密碼 -n 11 [root@master ~]# redis-cli -h 192.168.1.100 -p 16380 -a 你的密碼 -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.100:16380[11]> keys * (empty array) 192.168.1.100:16380[11]> set name zhangsan OK 192.168.1.100:16380[11]> set age 18 OK 192.168.1.100:16380[11]> keys * 1) "name" 2) "age" 192.168.1.100:16380[11]> # "slave1主機(jī)名"實(shí)例從庫的11號數(shù)據(jù)庫查看是否同步主庫的11號數(shù)據(jù)庫 redis-cli -h 192.168.1.200 -p 16381 -a 你的密碼 -n 11 [root@slave1 ~]# redis-cli -h 192.168.1.200 -p 16381 -a 你的密碼 -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.200:16381[11]> get name "zhagnsan" 192.168.1.200:16381[11]> get age "18" 192.168.1.200:16381[11]> key * (error) ERR unknown command 'key', with args beginning with: '*' 192.168.1.200:16381[11]> keys * 1) "age" 2) "name" 192.168.1.200:16381[11]> # "slave2主機(jī)名"實(shí)例從庫的11號數(shù)據(jù)庫查看是否同步主庫的11號數(shù)據(jù)庫 redis-cli -h 192.168.1.250 -p 16382 -a 你的密碼 -n 11 [root@slave2 ~]# redis-cli -h 192.168.1.250 -p 16382 -a 你的密碼 -n 11 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.1.250:16382[11]> get name "zhangsan" 192.168.1.250:16382[11]> get age "18" 192.168.1.250:16382[11]> keys * 1) "age" 2) "name" 192.168.1.250:16382[11]> # 如果針對 slave1 或者 slave2 進(jìn)行寫入操作 則會(huì)報(bào)錯(cuò) 錯(cuò)誤信息 只能在master操作哦 (error) READONLY You can't write against a read only replica. 結(jié)束 柚子快報(bào)邀請碼778899分享:緩存 【Redis 集群搭建】 參考文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。