redis主从复制和集群搭建

来源:互联网 发布:方正字体排查软件 编辑:程序博客网 时间:2024/05/18 12:28

Redis主从复制

配置从服务器:
到redis安装目录

$cp redis.conf redis-s1.conf

更改redis-s1.conf, 进行下面改动

port 6380slaveof 127.0.0.1 6379

slaveof后面就是主服务器的地址和端口。
启动:

$redis-server redis-s1.conf1174:S 24 Dec 15:57:59.619 * Connecting to MASTER 127.0.0.1:63791174:S 24 Dec 15:57:59.619 * MASTER <-> SLAVE sync started1174:S 24 Dec 15:57:59.619 * Non blocking connect for SYNC fired the event.1174:S 24 Dec 15:57:59.619 * Master replied to PING, replication can continue...1174:S 24 Dec 15:57:59.620 * Trying a partial resynchronization (request 2f7bd6f4987a7a6b93f1faa4822311f85c98d3e2:1).1174:S 24 Dec 15:57:59.620 * Full resync from master: 3cfd21b61e1304bb8c5194f5cbcece34080ed4e7:01174:S 24 Dec 15:57:59.620 * Discarding previously cached master state.1174:S 24 Dec 15:57:59.639 * MASTER <-> SLAVE sync: receiving 773 bytes from master1174:S 24 Dec 15:57:59.673 * MASTER <-> SLAVE sync: Flushing old data1174:S 24 Dec 15:57:59.673 * MASTER <-> SLAVE sync: Loading DB in memory1174:S 24 Dec 15:57:59.673 * MASTER <-> SLAVE sync: Finished with success

测试一下,连接到从服务器,看数据有没有同步过来。

localhost:~ yuchenghong$ redis-cli -h 127.0.0.1 -p 6380127.0.0.1:6380> hvals user:zhangsan1) "zhangsan"2) "123456"

Redis集群搭建

参考官方文档: https://redis.io/topics/cluster-tutorial
要让集群正常运作至少需要三个主节点, 建议使用六个节点: 其中三个为主节点, 而其余三个则是各个主节点的从节点。

集群需要ruby,先安装下ruby
然后执行:

$sudo gem install redis

我这儿测试,是在同一台机器,所以需要有6个不同端口。
进入一个新目录, 并创建六个以端口号为名字的子目录

localhost:redis-4.0.0 yuchenghong$ mkdir cluster-testlocalhost:redis-4.0.0 yuchenghong$ cd cluster-testlocalhost:cluster-test yuchenghong$ mkdir 7000 7001 7002 7003 7004 7005

复制redis.conf到每一个目录,并更改配置,把端口号改成和目录名一致

port 7000cluster-enabled yescluster-config-file nodes.confcluster-node-timeout 5000appendonly yes

cluster-enabled 选项用于开实例的集群模式;
cluster-conf-file 选项则设定了保存节点配置文件的路径,默认值为 nodes.conf. 节点配置文件无须人为修改,它由Redis集群在启动时创建,并在有需要时自动进行更新。

到每一个目录下启动redis

localhost:cluster-test yuchenghong$ cd 7000localhost:7000 yuchenghong$ redis-server redis.conf

因为 nodes.conf 文件不存在, 所以每个节点都为它自身指定了一个新的ID.实例会一直使用同一个 ID ,从而在集群中保持一个独一无二的名字。

1299:M 24 Dec 17:14:22.348 * No cluster configuration found, I'm 78c3e6402e7a7cf83f3ebb3cc4737fc386a2104a

现在我们已经有了六个正在运行中的 Redis 实例, 接下来我们需要使用这些实例来创建集群.
到src目录下面,启动集群,redis-trib.rb是一个ruby程序。

./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005>>> Creating cluster>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:7000127.0.0.1:7001127.0.0.1:7002Adding replica 127.0.0.1:7003 to 127.0.0.1:7000Adding replica 127.0.0.1:7004 to 127.0.0.1:7001Adding replica 127.0.0.1:7005 to 127.0.0.1:7002M: 2da67f756f9b3a5f5381c5a2fe790aa76898a12d 127.0.0.1:7000   slots:0-5460 (5461 slots) masterM: 78c3e6402e7a7cf83f3ebb3cc4737fc386a2104a 127.0.0.1:7001   slots:5461-10922 (5462 slots) masterM: a79979813b191a9b922907e72aca881d8b33339a 127.0.0.1:7002   slots:10923-16383 (5461 slots) masterS: 03ce0a0d976199f04cb6ec0fbae8aa0a2c435a04 127.0.0.1:7003   replicates 2da67f756f9b3a5f5381c5a2fe790aa76898a12dS: b5ec9455653ba3b61488e96a4fa3de3d51ac89b3 127.0.0.1:7004   replicates 78c3e6402e7a7cf83f3ebb3cc4737fc386a2104aS: da5354db107279333802c87b8abf52d65ad4b179 127.0.0.1:7005   replicates a79979813b191a9b922907e72aca881d8b33339aCan I set the above configuration? (type 'yes' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join...>>> Performing Cluster Check (using node 127.0.0.1:7000)M: 2da67f756f9b3a5f5381c5a2fe790aa76898a12d 127.0.0.1:7000   slots:0-5460 (5461 slots) master   1 additional replica(s)S: da5354db107279333802c87b8abf52d65ad4b179 127.0.0.1:7005   slots: (0 slots) slave   replicates a79979813b191a9b922907e72aca881d8b33339aM: 78c3e6402e7a7cf83f3ebb3cc4737fc386a2104a 127.0.0.1:7001   slots:5461-10922 (5462 slots) master   1 additional replica(s)M: a79979813b191a9b922907e72aca881d8b33339a 127.0.0.1:7002   slots:10923-16383 (5461 slots) master   1 additional replica(s)S: b5ec9455653ba3b61488e96a4fa3de3d51ac89b3 127.0.0.1:7004   slots: (0 slots) slave   replicates 78c3e6402e7a7cf83f3ebb3cc4737fc386a2104aS: 03ce0a0d976199f04cb6ec0fbae8aa0a2c435a04 127.0.0.1:7003   slots: (0 slots) slave   replicates 2da67f756f9b3a5f5381c5a2fe790aa76898a12d[OK] All nodes agree about slots configuration.

成功了。

连接redis集群测试一下:

localhost:src yuchenghong$ redis-cli -h 127.0.0.1 -c -p 7000127.0.0.1:7000> set name helloworld-> Redirected to slot [5798] located at 127.0.0.1:7001OK127.0.0.1:7001> get name"helloworld"localhost:src yuchenghong$ redis-cli -h 127.0.0.1 -c -p 7001127.0.0.1:7001> get name"helloworld"127.0.0.1:7001> set hello helloredis-> Redirected to slot [866] located at 127.0.0.1:7000OKlocalhost:src yuchenghong$ redis-cli -h 127.0.0.1 -c -p 7003127.0.0.1:7003> set hh nn-> Redirected to slot [12077] located at 127.0.0.1:7002OK127.0.0.1:7002> get nn-> Redirected to slot [9549] located at 127.0.0.1:7001(nil)127.0.0.1:7001> get name"helloworld"

搞定,如果是不同的机器,原理差不多。配置好IP和端口就好了。

阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 黄镇子女 黄门 黄门宴黄珂 黄门宴 黄泉便利店 巫门老九 黄门老灶火锅 黄阅 折子戏黄阅 黄陂 黄陂热线 黄陂论坛 黄陂区 黎黄陂路 黄陂豆丝 黄陂景点 黎黄陂 黄陂木兰 黄陂邮编 黄陂区邮编 武汉黄陂区 黄陂南路 黄陂旅馆 黄陂旅游 湖北黄陂 黄陂区15天预报 黄陂木兰草原 黄陂旅游景点 黎黄陂路街头博物馆 武汉市黄陂区邮编 黄陂热线论坛 武汉黄陂区邮编 黄陂木兰天池 黄陂旅游景点大全 黄陂有什么好玩的地方 武汉市黄陂区 2019年10月1号黄陂火灾事故 黄陵 黄陵县 西安黄陵 黄陵集团 陕西黄陵