1. 程式人生 > >redis的主從伺服器配置

redis的主從伺服器配置

1. redis的主從配置:

(1)redis的配置檔案(reids.conf)拷貝2

[[email protected] redis]# cp redis.conf redis6380.conf

[[email protected] redis]# cp redis.conf redis6381.conf

(殺掉redis程序)

(2)配置從伺服器redis6380.conf

 [[email protected] redis]# vi redis6380.conf    #需要修改的內容如下:

#pidfile /var/run/redis_6379.pid

pidfile /var/run/redis_6380.pid

#port 6379

port 6380

#dbfilename dump.rdb

dbfilename dump6380.rdb

# slaveof <masterip> <masterport>

slaveof 127.0.0.1 6379    

slave-read-only yes

 

(3)配置從伺服器redis6381.conf

[[email protected] redis]# vi redis6381.conf    #需要修改的內容如下:

#pidfile /var/run/redis_6379.pid

pidfile /var/run/redis_6381.pid

#port 6379

port 6381

#save 900 1      #全部註釋掉,不產生rdb檔案,2臺從伺服器,1臺產生rdb即可

#save 300 10

#save 60 10000

appendonly no   #也不讓它產生aof

# slaveof <masterip> <masterport>

slaveof 127.0.0.1 6379

slave-read-only yes   #只讀

(4)配置主伺服器redis.conf

[[email protected] redis]# vi redis.conf

#save 900 1     #禁用rdb,因為有從伺服器生成了

#save 300 10

#save 60 10000

#appendonly no

appendonly yes     #aof的配置檔案可要可不要

appendfilename "appendonly.aof"

 

(5)啟動主、從伺服器:

[[email protected] redis]# pwd

/usr/local/redis

[[email protected] redis]# ./bin/redis-server ./redis.conf    #啟動主伺服器

[[email protected] redis]# ./bin/redis-server ./redis6380.conf   #啟動從伺服器

[[email protected] redis]# ./bin/redis-server ./redis6381.conf   #啟動從伺服器

(6)客戶端連線主伺服器:

[[email protected] redis]# ./bin/redis-cli

127.0.0.1:6379>

127.0.0.1:6379> info replication

# Replication

role:master

connected_slaves:2   #連線的從伺服器的個數

slave0:ip=127.0.0.1,port=6380,state=online,offset=392,lag=0

slave1:ip=127.0.0.1,port=6381,state=online,offset=392,lag=1

127.0.0.1:6379> set animal cat  #主伺服器設定值,從伺服器可讀取值

OK

(7)客戶端連線從伺服器:

---slave1

[[email protected] ~]# cd /usr/local/redis/

[[email protected] redis]# ./bin/redis-cli -p 6380

127.0.0.1:6381> info replication

# Replication

role:slave

master_host:127.0.0.1

master_port:6379

master_link_status:up    #up連線正常,down連線失敗

 

---slave2

[[email protected] ~]# cd /usr/local/redis/

[[email protected] redis]# ./bin/redis-cli -p 6381

127.0.0.1:6381> info replication  #檢視從伺服器的連線狀態

# Replication

role:slave

master_host:127.0.0.1

master_port:6379

master_link_status:up    #up連線正常,down連線失敗

 

注:

從伺服器可以不通過密碼連線主從伺服器,不安全,如果想用密碼也是可以的:

加上密碼:(主、從伺服器配置檔案都需要加上密碼)

1)殺掉所有的redis程序:

[[email protected] redis]# pkill -9 redis

2)主伺服器加密碼:

[[email protected] redis]# vi redis.conf   #修改主伺服器內容如下:

# requirepass foobared

requirepass passwd

(1)從伺服器加密碼:(2個從伺服器都要修改如下內容:)

[[email protected] redis]# vi redis6380.conf

# masterauth <master-password>

masterauth passwd

 

[[email protected] redis]# vi redis6381.conf

# masterauth <master-password>

masterauth passwd

 

4)此時啟動redis主、從服務端,客戶端連線redis需要輸入密碼,如下:

[[email protected] ~]# cd /usr/local/redis/

[[email protected] redis]# ./bin/redis-cli

127.0.0.1:6379> set age 21

(error) NOAUTH Authentication required.

127.0.0.1:6379> auth passwd

OK

127.0.0.1:6379> set age 21

OK