redis主從複製

1.簡單主從複製


redis-server /redis_node_test/6382/redis.conf       //啓動6382redis進程


redis-server /redis_node_test/6383/redis.conf       //啓動6383redis進程


ss -ntulp|grep redis                         

tcp    LISTEN     0      128            127.0.0.1:6382                  *:*      users:(("redis-server",20961,4))

tcp    LISTEN     0      128            127.0.0.1:6383                  *:*      users:(("redis-server",21013,4))


redis-cli -p 6382           //客戶端登錄6382

127.0.0.1:6382>

redis-cli -p 6383           //客戶端登錄6383

127.0.0.1:6383>


將6382設爲主庫,可以在6383命令行上面設置

127.0.0.1:6383>

127.0.0.1:6383> slaveof 127.0.0.1 6382

OK

查看下自身信息

# Replication

role:slave

master_host:127.0.0.1

master_port:6382

master_link_status:up

在6382上面查看

127.0.0.1:6382> info

# Replication

role:master

connected_slaves:1

slave0:ip=127.0.0.1,port=6383,state=online,offset=71,lag=1

主從關係已經建好,但是一旦重啓便會失效,如果想永久生效需要寫在配置文件中(redis.conf)

現在可以進行簡單主從複製測試


在6382主上寫數據


127.0.0.1:6382> set name helloworld

OK


在6383從上面擦汗看數據


127.0.0.1:6383> get name

"helloworld"


2.加密的主從複製


vim /redis_node_test/6382/redis.conf

requirepass 123456           //主服務器連接密碼

vim /redis_node_test/6383/redis.conf

slaveof 127.0.0.1 6382

masterauth 123456

redis-server /redis_node_test/6382/redis.conf

redis-server /redis_node_test/6383/redis.conf

redis-cli -p 6382

127.0.0.1:6382> info

NOAUTH Authentication required.

報錯

redis-cli -p 6382 -a 123456

127.0.0.1:6382> info

# Replication

role:master

connected_slaves:1

slave0:ip=127.0.0.1,port=6383,state=online,offset=729,lag=0

這樣就不報錯了

有時候主從數據可能會不一致,因爲redis是異步寫入磁盤的,此時有可能部分數據還在內存中,沒有同步到磁盤,要想現在強制同步到磁盤,需要執行如下命令

redis-cli -p 6382 save

redis-cli -p 6383 save

如果想讓redis變成內存型數據庫只需要做如下操作


去掉配置文件的如下內容


# Save the DB on disk:

#

#   save <seconds> <changes>

#

#   Will save the DB if both the given number of seconds and the given

#   number of write operations against the DB occurred.

#

#   In the example below the behaviour will be to save:

#   after 900 sec (15 min) if at least 1 key changed

#   after 300 sec (5 min) if at least 10 keys changed

#   after 60 sec if at least 10000 keys changed

#

#   Note: you can disable saving completely by commenting out all "save" lines.

#

#   It is also possible to remove all the previously configured save

#   points by adding a save directive with a single empty string argument

#   like in the following example:

#

#   save ""


save 900 1

save 300 10

save 60 10000


改成如下格式


save 10000000000 10000000000       前面一個數字是表示數據發生改變後多久寫入硬盤,後面數字表示有多少個key發生改變才寫入硬盤


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章