Author QiuRiMangCao 秋日芒草
server01
server02 } redis 单节点
server03
server01
server02 } redis (master,slave)[数据备份][读写分离] slave减轻master的压力,当master挂了,slave不支持写,只支持读,所以服务还是不可用
server03
server01 } { node2 (master,slave)数据备份][读写分离] slave减轻master的压力,当master挂了,slave不支持写,只支持读,所以服务还是不可用,整个节点不可用
server02 } redis { node1 (master,slave)
server03 } { node3 (master,slave)
server01
server02 } redis (master,slave,redis-sentinel)sentinel用于切换master或slave,这样都可以写了
server03
./redis-server &
./redis-server /etc/redis/6379.conf
redis-cli -p 6380
redis redis-server /etc/myredis/redis.config
redis-cli ping
redis-cli -p 8888 shutdown
[root@localhost bin]# ./redis-server -v
跳转到文件尾
输入冒号(:),打开命令输入框
输入命令:$跳转到文件头
输入冒号(:),打开命令输入框
输入命令1,是“一”的阿拉伯数字,不是小写的L
3311:M 20 Oct 15:37:58.872 * Ready to accept connections
3311:M 20 Oct 15:39:45.855 * Slave 127.0.0.1:1001 asks for synchronization
3311:M 20 Oct 15:39:45.855 * Full resync requested by slave 127.0.0.1:1001
3311:M 20 Oct 15:39:45.855 * Starting BGSAVE for SYNC with target: disk
3311:M 20 Oct 15:39:45.855 * Background saving started by pid 3325
3325:C 20 Oct 15:39:45.859 * DB saved on disk
3325:C 20 Oct 15:39:45.860 * RDB: 0 MB of memory used by copy-on-write
3311:M 20 Oct 15:39:45.932 * Background saving terminated with success
3311:M 20 Oct 15:39:45.933 * Synchronization with slave 127.0.0.1:1001 succeeded
3321:S 20 Oct 15:39:45.854 * Connecting to MASTER 127.0.0.1:1000
3321:S 20 Oct 15:39:45.854 * MASTER <-> SLAVE sync started
3321:S 20 Oct 15:39:45.855 * Non blocking connect for SYNC fired the event.
3321:S 20 Oct 15:39:45.855 * Master replied to PING, replication can continue…
3321:S 20 Oct 15:39:45.855 * Partial resynchronization not possible (no cached master)
3321:S 20 Oct 15:39:45.855 * Full resync from master: 1a326d8a3bc1af413789dfa9dca65954072418d5:0
3321:S 20 Oct 15:39:45.933 * MASTER <-> SLAVE sync: receiving 175 bytes from master
3321:S 20 Oct 15:39:45.933 * MASTER <-> SLAVE sync: Flushing old data
3321:S 20 Oct 15:39:45.933 * MASTER <-> SLAVE sync: Loading DB in memory
3321:S 20 Oct 15:39:45.933 * MASTER <-> SLAVE sync: Finished with success
127.0.0.1:1000> keys *
[root@localhost redis]# ./bin/redis-cli -p 1000
127.0.0.1:1000> set password 123456
[root@localhost redis]# ./bin/redis-cli -p 1001
127.0.0.1:1001> keys *
1) “password”
127.0.0.1:1001> set user:password zhangsan:123456
(error) READONLY You can’t write against a read only slave.
127.0.0.1:1000> info
redis_version:4.0.1
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:92e72a18d61bfe4f
redis_mode:standalone
os:Linux 3.10.0-693.el7.x86_64 x86_64
# Replication
role:master #角色
connected_slaves:1
# slave0:ip=127.0.0.1,port=1001 - slave信息
slave0:ip=127.0.0.1,port=1001,state=online,offset=1084,lag=1
master_replid:1a326d8a3bc1af413789dfa9dca65954072418d5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1084
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1084
# Replication
role:slave
master_host:127.0.0.1
master_port:1000
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:1602
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:1a326d8a3bc1af413789dfa9dca65954072418d5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1602
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1602
mv ./sentinel.conf ./sentinel
mv sentinel/ ../
[root@localhost bin]# ./redis-sentinel ../redis-pub/sentinel/sentinel.conf
[root@localhost bin]# ps -ef | grep redis
root 3311 1 0 15:37 ? 00:00:05 ./bin/redis-server 127.0.0.1:1000
root 3321 1 0 15:39 ? 00:00:05 ./bin/redis-server 127.0.0.1:1001
root 3336 1726 0 15:44 pts/1 00:00:00 ./bin/redis-cli -p 1000
root 3532 1802 0 16:55 pts/2 00:00:00 ./redis-sentinel *:26379 [sentinel]
root 3537 2678 0 16:56 pts/3 00:00:00 grep –color=auto redis
3532:X 20 Oct 16:55:58.458 # Sentinel ID is ddcf5dd45ac986e979558ce338948d3bc463a9d5
3532:X 20 Oct 16:55:58.458 # +monitor master mymaster 127.0.0.1 1000 quorum 1
3532:X 20 Oct 16:55:58.459 * +slave slave 127.0.0.1:1001 127.0.0.1 1001 @ mymaster 127.0.0.1 1000
[root@localhost bin]# ./redis-cli -p 1001
127.0.0.1:1001> get password
“123456”
127.0.0.1:1001> set username zhaojian
(error) READONLY You can’t write against a read only slave.
[root@localhost redis]# ./bin/redis-cli -p 1000 shutdown
[root@localhost redis]# ps -ef | grep redis
root 3321 1 0 15:39 ? 00:00:05 ./bin/redis-server 127.0.0.1:1001
root 3532 1802 0 16:55 pts/2 00:00:00 ./redis-sentinel *:26379 [sentinel]
root 3538 1373 0 17:00 pts/0 00:00:00 ./redis-cli -p 1001
root 3578 1726 0 17:02 pts/1 00:00:00 grep –color=auto redis
3532:X 20 Oct 16:55:58.455 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3532:X 20 Oct 16:55:58.458 # Sentinel ID is ddcf5dd45ac986e979558ce338948d3bc463a9d5
3532:X 20 Oct 16:55:58.458 # +monitor master mymaster 127.0.0.1 1000 quorum 1
3532:X 20 Oct 16:55:58.459 * +slave slave 127.0.0.1:1001 127.0.0.1 1001 @ mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:11.847 # +sdown master mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:11.847 # +odown master mymaster 127.0.0.1 1000 #quorum 1/1
3532:X 20 Oct 17:03:11.847 # +new-epoch 1
3532:X 20 Oct 17:03:11.847 # +try-failover master mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:11.850 # +vote-for-leader ddcf5dd45ac986e979558ce338948d3bc463a9d5 1
3532:X 20 Oct 17:03:11.850 # +elected-leader master mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:11.850 # +failover-state-select-slave master mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:11.927 # +selected-slave slave 127.0.0.1:1001 127.0.0.1 1001 @ mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:11.927 * +failover-state-send-slaveof-noone slave 127.0.0.1:1001 127.0.0.1 1001 @ mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:11.999 * +failover-state-wait-promotion slave 127.0.0.1:1001 127.0.0.1 1001 @ mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:12.006 # +promoted-slave slave 127.0.0.1:1001 127.0.0.1 1001 @ mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:12.007 # +failover-state-reconf-slaves master mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:12.065 # +failover-end master mymaster 127.0.0.1 1000
3532:X 20 Oct 17:03:12.066 # +switch-master mymaster 127.0.0.1 1000 127.0.0.1 1001
3532:X 20 Oct 17:03:12.066 * +slave slave 127.0.0.1:1000 127.0.0.1 1000 @ mymaster 127.0.0.1 1001
3532:X 20 Oct 17:03:42.085 # +sdown slave 127.0.0.1:1000 127.0.0.1 1000 @ mymaster 127.0.0.1 1001
# Replication
role:master
connected_slaves:0
master_replid:41997ee7b0607cda24f609821af975cd5b3c802f
master_replid2:1a326d8a3bc1af413789dfa9dca65954072418d5
master_repl_offset:32899
second_repl_offset:32900
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:32899
127.0.0.1:1001> set username zhaojian
OK
[root@localhost bin]# ./redis-server ../redis-pub/master/redis.conf
# Replication
role:slave
master_host:127.0.0.1
master_port:1001
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:51801
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7ad3eb737bc808a362da9b06c2943f4b35711de5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:51801
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:32900
repl_backlog_histlen:18902
127.0.0.1:1000> keys *
1) “password”
2) “username”
127.0.0.1:1000> set age 11
(error) READONLY You can’t write against a read only slave.
[root@localhost sentinel]# cat sentinel.conf
sentinel myid ddcf5dd45ac986e979558ce338948d3bc463a9d5
# Default is 30 seconds.
sentinel monitor mymaster 127.0.0.1 1001 1
sentinel known-slave mymaster 127.0.0.1 1000
sentinel current-epoch 1
[root@localhost redis]# ./bin/redis-cli -p 1000 shutdown
[root@localhost redis]# ./bin/redis-cli -p 1001 shutdow
[root@localhost bin]# ./redis-server ../redis-pub/master/redis.conf
[root@localhost bin]# ./redis-server ../redis-pub/slave/redis.conf
[root@localhost bin]# ps -ef | grep redis
root 3532 1802 0 16:55 pts/2 00:00:05 ./redis-sentinel *:26379 [sentinel]
root 3665 1 0 17:37 ? 00:00:00 ./redis-server 127.0.0.1:1000
root 3670 1 0 17:37 ? 00:00:00 ./redis-server 127.0.0.1:1001
root 3676 1373 0 17:37 pts/0 00:00:00 grep –color=auto redis
[root@localhost redis]# ./bin/redis-cli -p 1000
127.0.0.1:1000> info
# Replication
role:slave
master_host:127.0.0.1
master_port:1001
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:96505
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:8c54c599d796c83615067b1b665502a81061285a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:96505
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576