redis主从复制,实现读写分离

作者:吴泽鑫 分类: Redis 发布于:2015-11-2 0:51 ė1759次浏览 60条评论

redis也像mysql一样拥有强大的主从复制,读写分离的分布式集群应用,下面整理了配置流程

1、添加配置文件,添加2台redis服务器和默认的6379服务器做测试

cp redis.conf redis6380.conf

cp redis.conf redis6381.conf

2、杀死所有进程:pkill -9 redis

3、修改配置redis6380.conf和redis6381.conf为:

vim redis6380.conf:

找到配置文件中的以下配置并修改:

pidfile /var/run/redis6380.pid

port 6380

slaveof <masterip> <masterport> 改为 slaveof localhost 6379 #修改为6379的slave

dbfilename dump.rdb 改为 dbfilename dump6380.rdb #修改rdb名(根据需要而定,可选)

slave-read-only yes #只读

关闭rdb和aof的产生(根据需要而定,可选)

#save 900 1 #注释掉

#save 300 10 #注释掉

#save 60 10000 #注释掉

append only no #改为no

第二个配置文件也一样,找到配置文件中的以下配置并修改:

vim redis6381.conf

pidfile /var/run/redis6381.pid

port 6381

slaveof <masterip> <masterport>改为slaveof localhost 6379

slave-read-only yes #只读

dbfilename dump.rdb 改为 dbfilename dump6381.rdb #修改rdb名(根据需要而定,可选)

关闭rdb和aof的产生(根据需要而定,可选)

#save 900 1 #注释掉

#save 300 10 #注释掉

#save 60 10000 #注释掉

append only no #改为no

4、启动进程

redis-server /usr/local/webserver/redis_conf/redis.conf &

redis-server /usr/local/webserver/redis_conf/redis6380.conf &

redis-server /usr/local/webserver/redis_conf/redis6381.conf &

5、测试

连接master:

[root@promote redis_conf]# redis-cli

127.0.0.1:6379> set title test

OK

连接slave:

[root@promote ~]# redis-cli -p 6380

127.0.0.1:6380> get title

"test"

连接slave:

[root@promote ~]# redis-cli -p 6381

127.0.0.1:6381> get title

"test"

测试成功!

测试只读:

127.0.0.1:6381> set title snow

(error) READONLY You can't write against a read only slave.

6、如果想安全点,为master设定密码

vim redis.conf

requirepass wuzexin #设定密码

vim redis6380.conf

#masterauth <master-password>改为

masterauth wuzexin #master密码

vim redis6381.conf

#masterauth <master-password>改为

masterauth wuzexin #master密码

测试:

[root@promote redis_conf]# redis-cli

127.0.0.1:6379> get title

(error) NOAUTH Authentication required.

127.0.0.1:6379> auth wuzexin

OK

127.0.0.1:6379> set title hot

OK

----------------------------------------------------

[root@promote ~]# redis-cli -p 6380

127.0.0.1:6380> set title hot

(error) READONLY You can't write against a read only slave.

127.0.0.1:6380> get title

"hot"

------------------------------------------------------

[root@promote ~]# redis-cli -p 6381

127.0.0.1:6381> get title

"hot"

测试成功

本文出自 Jensen-吴泽鑫的博客,转载时请注明出处及相应链接:http://www.wuzexin.cn/post-13.html

发表评论

电子邮件地址不会被公开。必填项已用*标注


Ɣ回顶部