redis
Posted _裸奔到月球
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis相关的知识,希望对你有一定的参考价值。
1、启动:[[email protected] bin]# ./redis-server redis.conf
进入客户端:./redis-cli(redis内置客户端)
2、常用数据类型:String、Hash、List、Set、SortedSet
3、redis集群:
集群有16384个槽,由几个redis平分;
根据要存进来的key通过算法计算得到结果,再除以16384得到余数,放入相应槽中;
master节点挂掉:如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉
如果集群任意master挂掉,且当前master没有slave.集群进入fail状态。集群有一主一备redis
整个集群fail:如果集群超过半数以上master挂掉,无论是否有slave集群进入fail状态
4、搭建集群:
搭建集群需要的环境
搭建集群需要使用到官方提供的ruby脚本。
需要安装ruby的环境。安装ruby
yum install ruby
yum install rubygems
redis集群管理工具redis-trib.rb
[[email protected] ~]# cd redis-3.0.0
[[email protected] redis-3.0.0]# cd src
[[email protected] src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 07:01 redis-trib.rb
脚本需要的ruby包:redis-3.0.0.gem
需要上传到linux服务。
安装ruby的包:
gem install redis-3.0.0.gem
[[email protected] ~]# gem install redis-3.0.0.gem
1.1 集群的搭建
第一步:创建6个redis实例,端口号从7001~7006
第二步:修改redis的配置文件 (cd /usr/local/redis)(cp -r bin ../redis-cluster/redis01)
cd redis01、rm -f dump.rdb、vim redis.conf
1、修改端口号
2、打开cluster-enable前面的注释。(记得去掉前面的#)
第三步:把创建集群的ruby脚本复制到 /usr/local/redis-cluster目录下(自己建的目录)。
cp -r redis01/ redis02
把redis-trib.rb的ruby脚本拷贝到cluster里面(redis3.0/src)
第四步:启动6个redis实例:chmod +x startall.sh
[cluster] vim startall.sh
cd redis01
./redis-server redis.conf
cd ..
......
第五步:创建集群。[cluster]
./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006 |
[[email protected] redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006
>>> Creating cluster
Connecting to node 192.168.25.153:7001: OK
Connecting to node 192.168.25.153:7002: OK
Connecting to node 192.168.25.153:7003: OK
Connecting to node 192.168.25.153:7004: OK
Connecting to node 192.168.25.153:7005: OK
Connecting to node 192.168.25.153:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.25.153:7001
192.168.25.153:7002
192.168.25.153:7003
Adding replica 192.168.25.153:7004 to 192.168.25.153:7001
Adding replica 192.168.25.153:7005 to 192.168.25.153:7002
Adding replica 192.168.25.153:7006 to 192.168.25.153:7003
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.25.153:7001
slots:0-5460 (5461 slots) master
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.25.153:7002
slots:5461-10922 (5462 slots) master
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.25.153:7003
slots:10923-16383 (5461 slots) master
S: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.25.153:7004
replicates 5a8523db7e12ca600dc82901ced06741b3010076
S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.25.153:7005
replicates bf6f0929044db485dea9b565bb51e0c917d20a53
S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.25.153:7006
replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
Can 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 cluster
Waiting for the cluster to join.....
>>> Performing Cluster Check (using node 192.168.25.153:7001)
M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.25.153:7001
slots:0-5460 (5461 slots) master
M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.25.153:7002
slots:5461-10922 (5462 slots) master
M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.25.153:7003
slots:10923-16383 (5461 slots) master
M: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.25.153:7004
slots: (0 slots) master
replicates 5a8523db7e12ca600dc82901ced06741b3010076
M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.25.153:7005
slots: (0 slots) master
replicates bf6f0929044db485dea9b565bb51e0c917d20a53
M: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.25.153:7006
slots: (0 slots) master
replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[[email protected] redis-cluster]#
测试集群
[[email protected] redis-cluster]# redis01/redis-cli -h 192.168.25.153 -p 7002 -c
关闭redis
单个:原状态为客户端(usr/local/redis/bin/redis-cli) shutdown
多个:
1、 [redis]# bin/redis-cli -p 7001 shutdown 这样关?
2、[cluster]# vim shutdown.sh
redis01/redis-cli -p 7001 shutdown
redis01/redis-cli -p 7002 shutdown
[cluster]# chmod +x shutdown.sh \ ./shutdown.sh
以上是关于redis的主要内容,如果未能解决你的问题,请参考以下文章