这里自己用自己私有的uclude云服务器当做master
[root@10-23-117-188 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1452 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:fe:c2:c8 brd ff:ff:ff:ff:ff:ff
inet 11.23.119.188/16 brd 10.23.255.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fefe:c2c8/64 scope link
valid_lft forever preferred_lft forever
[root@10-23-117-188 ~]#
可以看到ip为: 11.23.119.188(为云服务器默认配的内网ip)
[root@10-23-117-188 ~]# cd /usr/local/redis-6.0.8/bin/
[root@10-23-117-188 bin]# ls
backup.db dump.rdb redis-benchmark redis-check-aof redis-check-rdb redis-cli redis.conf redis-sentinel redis-server
[root@10-23-117-188 bin]#
vim service.conf
[root@10-23-117-188 bin]# ps -aux |grep redis
root 7048 0.1 0.5 173316 9688 ? Ssl 14:35 0:09 ./redis-server 10.23.117.188:6379
root 11323 0.0 0.1 112828 2296 pts/0 R+ 16:25 0:00 grep --color=auto redis
[root@10-23-117-188 bin]# kill -9 7048
[root@10-23-117-188 bin]# ./redis-server ./redis.conf
You have new mail in /var/spool/mail/root
[root@10-23-117-188 bin]#
这里用的本地虚拟机centos中的redis服务器
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:24:9c:4e brd ff:ff:ff:ff:ff:ff
inet 192.168.0.111/24 brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::7e0f:4e94:3afd:ae55/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]#
可以看到ip为: 192.168.0.111
[root@localhost ~]# cd /usr/local/redis-5.0.4/bin/
[root@localhost bin]# ls
dump.rdb redis-benchmark redis-check-aof redis-check-rdb redis-cli redis.conf redis-sentinel redis-server
[root@localhost bin]# vim redis.conf
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=111.19.42.76,port=6379,state=online,offset=3458,lag=0
master_replid:22dc694b36c6634f14ef9f1718fd0c8f0b81e1a1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:3458
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:3458
You have new mail in /var/spool/mail/root
[root@10-23-117-188 bin]#
[root@localhost bin]# ./redis-cli -h 192.168.0.111 -p 6379 -a 123456 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:106.75.226.106
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:4494
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:22dc694b36c6634f14ef9f1718fd0c8f0b81e1a1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:4494
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:4494
[root@localhost bin]#
[root@10-23-117-188 bin]# redis
127.0.0.1:6379> set name laowang
(error) NOAUTH Authentication required.
127.0.0.1:6379>
提示没有验证,输入密码即可 auth 123456
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> set name laowang
OK
127.0.0.1:6379> get name
"laowang"
127.0.0.1:6379>
[root@localhost bin]# redis
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> get name
"laowang"
127.0.0.1:6379> set age 18
(error) READONLY You can't write against a read only replica.
127.0.0.1:6379>
由上验证了slave端只能读不能写,master端可读可写。
redis主从
主服务器修改配置文件
添加
requirepass redis2020
从服务器修改配置文件
添加
masterauth redis2020
requirepass redis2020
重启主从服务
redis集群
验证