Centos7 yum 安装的redis最新版本
wget http://download.redis.io/releases/redis-6.0.8.tar.gz
tar xzf redis-6.0.8.tar.gz
cd redis-6.0.8
make
执行完 make 命令后,redis-6.0.8 的 src 目录下会出现编译后的 redis 服务程序 redis-server,还有用于测试的客户端程序 redis-cli:
下面启动 redis 服务:
# cd src
# ./redis-server
注意这种方式启动 redis 使用的是默认配置。也可以通过启动参数告诉 redis 使用指定配置文件使用下面命令启动。
# cd src
# ./redis-server ../redis.conf
redis.conf 是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
启动 redis 服务进程后,就可以使用测试客户端程序 redis-cli 和 redis 服务交互了。 比如:
# cd src
# ./redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
集群分类
环境 centos7.9
Redis 6.2.8
建立三个端口分别为6379 6380 6381的redis服务
[root@iZuf64res1ona9ad2ddsejZ /]# redis-server --port 6379
[root@iZuf64res1ona9ad2ddsejZ /]# redis-server --port 6380
[root@iZuf64res1ona9ad2ddsejZ /]# redis-server --port 6381
分别进入6380与6381端口的redis服务,设置作为6379的slavor
[root@iZuf64res1ona9ad2ddsejZ ~]# redis-cli -p 6380
127.0.0.1:6380> SLAVEOF 127.0.0.1 6379
OK
127.0.0.1:6380>
在6380 6381两个服务中分别查看replication主从复制的信息
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:1183
slave_repl_offset:1183
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4ef113ed826063106b632758203b1c5cf5f9d33c
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1183
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:211
repl_backlog_histlen:973
127.0.0.1:6380> exit
exit
从服务只能读,不能写
127.0.0.1:6380> set test 123
(error) READONLY You can't write against a read only replica.
127.0.0.1:6379> set test 123
OK
127.0.0.1:6379> get test
"123"
127.0.0.1:6379>
主服务写入test后,在从服务也能读出
127.0.0.1:6380> get test
"123"
127.0.0.1:6380> get test # 取消前,有数据
"123"
127.0.0.1:6380> slaveof no one # 取消从的关系
OK
127.0.0.1:6380> get test # 取消后,同步的数据任然不会消失
"123"
127.0.0.1:6380> set test 321 # 成功可以写数据
OK
127.0.0.1:6380> get test
"321"
创建sentinel.conf文件(注意,这里我建立一个sentinel集群,有三个sentinel)
文件内容为如下,表示监听master 1277.17.0.1 6379端口,当master出问题时,只需要一个sentinel发现就可以切换master(mymaster为命名,可任意)。
sentinel monitor mymaster 127.0.0.1 6379 1
[root@iZuf64res1ona9ad2ddsejZ /]# cd / && touch /etc/my_sentinel1.conf
[root@iZuf64res1ona9ad2ddsejZ /]# vim /etc/my_sentinel1.conf
[root@iZuf64res1ona9ad2ddsejZ /]# cd / && touch /etc/my_sentinel2.conf
[root@iZuf64res1ona9ad2ddsejZ /]# vim /etc/my_sentinel2.conf
[root@iZuf64res1ona9ad2ddsejZ /]# cd / && touch /etc/my_sentinel3.conf
[root@iZuf64res1ona9ad2ddsejZ /]# vim /etc/my_sentinel3.conf
然后写入内容
[root@iZuf64res1ona9ad2ddsejZ /]# redis-sentinel /etc/my_sentinel1.conf --port 26379
[root@iZuf64res1ona9ad2ddsejZ /]# redis-sentinel /etc/my_sentinel2.conf --port 26380
[root@iZuf64res1ona9ad2ddsejZ /]# redis-sentinel /etc/my_sentinel3.conf --port 26381
24339:X 01 Jan 2022 23:46:03.316 # +sdown master mymaster 127.0.0.1 6379
24339:X 01 Jan 2022 23:46:03.316 # +odown master mymaster 127.0.0.1 6379 #quorum 1/1
24339:X 01 Jan 2022 23:46:03.316 # +new-epoch 2
24339:X 01 Jan 2022 23:46:03.316 # +try-failover master mymaster 127.0.0.1 6379
24339:X 01 Jan 2022 23:46:03.320 # +vote-for-leader 7f4446747d0f19acbacb64ee186715813a68878a 2
24339:X 01 Jan 2022 23:46:04.095 # +config-update-from sentinel 12bac0fc4cd01834c2aa5309f467e5e19446de0d 127.0.0.1 26380 @ mymaster 127.0.0.1 6379
24339:X 01 Jan 2022 23:46:04.095 # +switch-master mymaster 127.0.0.1 6379 127.0.0.1 6381
24339:X 01 Jan 2022 23:46:04.095 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
24339:X 01 Jan 2022 23:46:04.095 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
关闭后发现到6379服务挂了,所以选举switch投票 6381为master
查看6381服务确实是master
[root@iZuf64res1ona9ad2ddsejZ ~]# redis-cli -p 6381
127.0.0.1:6381> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=6380,state=online,offset=16599,lag=1
master_failover_state:no-failover
master_replid:c7088a17d6b7e4d3c174f3caa7eaf43d51b1f171
master_replid2:dc13697526c0351b88756835914597b6852d8f61
master_repl_offset:16865
second_repl_offset:2247
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:16865
127.0.0.1:6381>
redis-server --port 6379
查看信息发现其master是6381的服务,自己是slaver
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:31749
slave_repl_offset:31749
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:c7088a17d6b7e4d3c174f3caa7eaf43d51b1f171
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:31749
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:25847
repl_backlog_histlen:5903
127.0.0.1:6379>