码迷,mamicode.com
首页 > 其他好文 > 详细

redis

时间:2016-04-03 07:24:30      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:redis

redis:


保存在内存中,周期的将内存写到磁盘中,数据冗余。


单线程,不如memcache。


主从架构(通过哨兵),3.0已经支持cluster.


redies:内存缓存和磁盘存储。数据结构存储服务器

KV cache and store

冗余;持久化

主从(借助于sentinel实现一定意义上的HA)

Clustering(分布式)

队列


数据结构服务器支持的类型:

String, List, Hash, Set, Sorted Set, Bitmap, HyperLoglog


www.redis.io

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

安装

[root@bas1 ~]# yum install -y redis-3.0.7-2.el6.remi.x86_64.rpm 

[root@bas1 ~]# cp /etc/redis.conf{,.bak}

[root@bas1 ~]# vim /etc/redis.conf

daemonize no  不运行守护进程

port 6379

tcp-backlog 511  等待队列,缓存满了,新增缓冲

bind  127.0.0.1 192.168.40.19    监听地址

# unixsocket /tmp/redis.sock    使用sock文件,直接通过内存,不需要tcp拆装

# unixsocketperm 700    ##权限

timeout 0            ##客户端连接不超时

tcp-keepalive 0      ##

loglevel notice

logfile /var/log/redis/redis.log

databases 16        ##


save 900 1           ##每隔900s有一个改变就快照

save 300 10        ##每隔900s有10个改变就快照

save 60 10000        ##每隔60s有10000个改变就快照

save ""              ##禁用持久化


指明从

# slaveof <masterip> <masterport>


[root@bas1 ~]# service redis start

启动 :                                                    [确定]

[root@bas1 ~]# ss -tnl|grep 6379

LISTEN     0      128               127.0.0.1:6379                     *:* 


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

使用

[root@bas1 ~]# redis-cli -h 127.0.0.1

127.0.0.1:6379>  select 0  (默认数据库,最多16个,每个库key不能同名,同名使用select) 

OK

127.0.0.1:6379> select 1

OK


@Strings:(help @string)

SET key value [EX second] [NX|XX]

         EX key过期 == setex key second value

        NX 当key不存在,设值 ==setnx key value

         XX 相反

GET

INCR

DECR

EXIST


127.0.0.1:6379[1]> set disto fedora

OK

127.0.0.1:6379[1]> get disto

"fedora"

127.0.0.1:6379[1]> set disto centos

OK

127.0.0.1:6379[1]> get disto

"centos"

127.0.0.1:6379[1]> APPEND disto slackeare

(integer) 15

127.0.0.1:6379[1]> get disto

"centosslackeare

127.0.0.1:6379[1]> STRLEN disto   ##长度

(integer) 15

127.0.0.1:6379[1]> set count 0

OK

127.0.0.1:6379[1]> incr count

(integer) 1

127.0.0.1:6379[1]> incr count

(integer) 2

127.0.0.1:6379[1]> incr count

(integer) 3

127.0.0.1:6379[1]> decr count

(integer) 2

127.0.0.1:6379[1]> decr count

(integer) 1


127.0.0.1:6379[1]> set disto gentoo NX

(nil)

127.0.0.1:6379[1]> set disto gentoo xx

OK

127.0.0.1:6379[1]> get disto

"gentoo"


127.0.0.1:6379[1]> help EXISTS


  EXISTS key [key ...]

  summary: Determine if a key exists

  since: 1.0.0

  group: generic


127.0.0.1:6379[1]> EXISTS disto

(integer) 1

127.0.0.1:6379[1]> EXISTS dist

(integer) 0


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

列表:help @list

LPUSH  左插入

RPUSH

LPOP  弹出

RPOP

LINDEX

LSET  修改索引指定的值


127.0.0.1:6379[1]> help LPUSH


  LPUSH key value [value ...]

  summary: Prepend one or multiple values to a list

  since: 1.0.0

  group: list


127.0.0.1:6379[1]> LPUSH a1 mon

(integer) 1

127.0.0.1:6379[1]> help LINDEX


  LINDEX key index

  summary: Get an element from a list by its index

  since: 1.0.0

  group: list


127.0.0.1:6379[1]> LINDEX a1 0

"mon"

127.0.0.1:6379[1]> LPUSH a1 sun

(integer) 2

127.0.0.1:6379[1]> LINDEX a1 0

"sun"

127.0.0.1:6379[1]> LINDEX a1 1

"mon"

127.0.0.1:6379[1]> RPUSH a1 tue

(integer) 3

127.0.0.1:6379[1]> LINDEX a1 2

"tue"

127.0.0.1:6379[1]> lset a1 1 fri

OK

127.0.0.1:6379[1]> LINDEX a1 1

"fri"

127.0.0.1:6379[1]> RPOP a1

"tue"

127.0.0.1:6379[1]> RPOP a1

"fri"

127.0.0.1:6379[1]> RPOP a1

"sun"

127.0.0.1:6379[1]> RPOP a1

(nil)


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

集合 @Sets:

SADD------集合添加一个或多个元素

SINTER-----------求交集

SUNION-----

SPOP

SISMEMBER

SCARD -----------获取集合的个数

SDIFFSTORE destination key [key ...]----------将多个元素抽取出来作并集

SINTERSTORE--------------交后的结果存储下来

SISMEMBER key member--------指定键中存在某个


127.0.0.1:6379> SADD w1 mon tue wed thu fri sat sun   ##添加

(integer) 7

127.0.0.1:6379> SADD w2 tue thu day

(integer) 3

127.0.0.1:6379> SINTER w1 w2          ##交集

1) "thu"

2) "tue"

127.0.0.1:6379> SUNION w1 w2          ##并集

1) "sat"

2) "tue"

3) "fri"

4) "day"

5) "thu"

6) "wed"

7) "mon"

8) "sun"

127.0.0.1:6379> SPOP w1           ##随机弹出

"mon"

127.0.0.1:6379> SPOP w1 

"sun"

127.0.0.1:6379> SISMEMBER w1 mon      ##查看元素

(integer) 0

127.0.0.1:6379> SISMEMBER w1 wed

(integer) 1


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

有序集合:Sorted Sets:

ZADD

ZRANGE

ZCARD

ZRANK


127.0.0.1:6379> ZADD weekday1 1 mon 2 tue 3 wed

(integer) 3

127.0.0.1:6379> ZCARD weekday1

(integer) 3

127.0.0.1:6379> zrank weekday1 tue  score是score index是index编号

(integer) 1

127.0.0.1:6379> zrank weekday1 wed

(integer) 2

127.0.0.1:6379> zrank weekday1 mon

(integer) 0

127.0.0.1:6379> ZSCORE weekday1 2

(nil)

127.0.0.1:6379> ZSCORE weekday1 tue

"2"

127.0.0.1:6379> ZRANGE weekday1 0 2

1) "mon"

2) "tue"

3) "wed"

127.0.0.1:6379> ZRANGE weekday1 0 1

1) "mon"

2) "tue"



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

@Hashes:

HSET

HSETNX

HGET

HKEYS

HVALS

HDEL


127.0.0.1:6379> hset h1 a mon

(integer) 1

127.0.0.1:6379> hget h1 a

"mon"

127.0.0.1:6379> hset h1 b tue

(integer) 1

127.0.0.1:6379> hset h1 b tue

(integer) 0

127.0.0.1:6379> hget h1 a

"mon"

127.0.0.1:6379> hget h1 b

"tue"

127.0.0.1:6379> HKEYS h1

1) "a"

2) "b"

127.0.0.1:6379> HVALS h1

1) "mon"

2) "tue"

127.0.0.1:6379> HLEN h1

(integer) 2

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

实现数据统计

@Bitmaps,    通过位图的方式实现位置映射,获取数据集合  

@HyperLogLog       

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

认证实现方法:

(1) redis.conf

requirepass PASSWORD


配置文件:

# requirepass foobared

 requirepass redhat

[root@bas1 ~]# service redis restart

停止 redis-server:                                        [确定]

启动 :                                                    [确定]


127.0.0.1:6379> select 0

(error) NOAUTH Authentication required.

127.0.0.1:6379> auth

(error) ERR wrong number of arguments for ‘auth‘ command

127.0.0.1:6379> select 1

(error) NOAUTH Authentication required.

127.0.0.1:6379> auth redhat

OK

127.0.0.1:6379> SELECT 0

OK


root@bas1 ~]# service redis stop

停止 redis-server:                                        [确定]

[root@bas1 ~]# service redis start

启动 :                                                    [确定]

[root@bas1 ~]# redis-cli -h 127.0.0.1

127.0.0.1:6379> SELECT 0

OK

127.0.0.1:6379> SELECT 1

OK


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


清空数据库

FLUSHDB:清空当前库

FLUSHALL:清空所有库


[root@bas1 ~]# redis-cli -h 127.0.0.1

127.0.0.1:6379> FLUSHDB

OK


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


事务:

通过MULTI, EXEC, WATCH等命令实现事务功能;将一个或多个命令归并为一个操作提请服务器按顺序执行的机制;不支持回滚操作;


MULTI:启动一个事务;

EXEC: 执行事务;

一次性将事务中的所有操作执行完成后返回给客户端


127.0.0.1:6379> MULTI

OK

127.0.0.1:6379> set ip 192.168.0.1

QUEUED

127.0.0.1:6379> get ip

QUEUED

127.0.0.1:6379> set port 8080

QUEUED

127.0.0.1:6379> get port

QUEUED

127.0.0.1:6379> exec

1) OK

2) "192.168.0.1"

3) OK

4) "8080"


WATCH:乐观锁;在EXEC命令执行之前,用于监视指定数据键;如果监视中的某任意数据被修改则服务器拒绝执行事务



在watch与exec之间,如果watch的内容发生改变,将报错。

127.0.0.1:6379> watch ip

OK

127.0.0.1:6379> MULTI

OK

127.0.0.1:6379> set ip 10.0.0.1

QUEUED

127.0.0.1:6379> get ip

QUEUED

127.0.0.1:6379> exec(下面执行)

(nil)


在exec之前

127.0.0.1:6379> get ip

"192.168.0.1"

127.0.0.1:6379> set ip 172.16.10.1

OK


执行
127.0.0.1:6379> exec

(nil)



当命令入队列发生错误,不执行。

监控的建,在事务中没有执行结束的时候,exec前改数据就报错,拒绝。


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

命令发生错误,拒绝执行


127.0.0.1:6379> MULTI

OK

127.0.0.1:6379> get ip

QUEUED

127.0.0.1:6379> set port 6379

QUEUED

127.0.0.1:6379> setttt

(error) ERR unknown command ‘setttt‘

127.0.0.1:6379> exec

(error) EXECABORT Transaction discarded because of previous errors.



持久功能,服务器端开启


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

Connection相关的命令:@connection

  • AUTH



  • ECHO 显示

127.0.0.1:6379> help echo   


  ECHO message

  summary: Echo the given string

  since: 1.0.0

  group: connection


127.0.0.1:6379> echo "i am zhuyouen"

"i am zhuyouen"



  • PING——##查看服务器是否在线

[root@bas1 ~]# redis-cli

127.0.0.1:6379> help ping


  PING -

  summary: Ping the server

  since: 1.0.0

  group: connection


127.0.0.1:6379> ping

PONG


  • QUIT


  • SELECT





Server相关的命令:

CLIENT SETNAME --------------连接设定name

CLIENT GETNAME----------------获取当前的连接名

CLIENT KILL ip:port-----------删除客户端


INFO------服务器的相信息

127.0.0.1:6379> info cpu

# CPU

used_cpu_sys:53.02

used_cpu_user:20.90

used_cpu_sys_children:0.08

used_cpu_user_children:0.00



CONFIG GET

CONFIG RESETSTAT-----------------重置info信息

CONFIG SET parameter value-------运行时修改参数,保存在内存

CONFIG REWRITE--------------------将内存的内容复制到磁盘


DBSIZE-----------显示db数据库键的数量

127.0.0.1:6379> dbsize

(integer) 2


BGSAVE------------异步同步到磁盘上去

SAVE

LASTSAVE--------最近一次保存在磁盘上的时间戳


monitor--------实时监控



127.0.0.1:6379> help shutdown---------------是否保存到磁盘后的关闭redis


  SHUTDOWN [NOSAVE] [SAVE]

  summary: Synchronously save the dataset to disk and then shut down the server

  since: 1.0.0

  group: server


SLAVEOF host port############################创建从节点

  summary: Make the server a slave of another instance, or promote it as master

  since: 1.0.0


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

发布与订阅(publish/subscribe)

频道:消息队列


SUBSCRIBE: 到指定的频道订阅一个或多个队列;支持模式匹配


PUBLISH: 向频道发布消息;


UNSUBSCRIBE:退订此前订阅的频道;


PSUBSCRIBE:模式订阅

技术分享



127.0.0.1:6379> help subscribe


  SUBSCRIBE channel [channel ...]

  summary: Listen for messages published to the given channels

  since: 2.0.0

  group: pubsub


127.0.0.1:6379> help @pubsub


  PSUBSCRIBE pattern [pattern ...]

  summary: Listen for messages published to channels matching the given patterns

  since: 2.0.0


  PUBLISH channel message

  summary: Post a message to a channel

  since: 2.0.0


  PUBSUB subcommand [argument [argument ...]]

  summary: Inspect the state of the Pub/Sub subsystem

  since: 2.8.0


  PUNSUBSCRIBE [pattern [pattern ...]]

  summary: Stop listening for messages posted to channels matching the given patterns

  since: 2.0.0


  SUBSCRIBE channel [channel ...]

  summary: Listen for messages published to the given channels

  since: 2.0.0


  UNSUBSCRIBE [channel [channel ...]]

  summary: Stop listening for messages posted to the given channels

  since: 2.0.0


实例:

127.0.0.1:6379> SUBSCRIBE new========================订阅消息

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "new"

3) (integer) 1


127.0.0.1:6379> PUBLISH new hello==================发布消息

(integer) 1


127.0.0.1:6379> SUBSCRIBE new=========================显示消息

Reading messages... (press Ctrl-C to quit)

1) "subscribe"

2) "new"

3) (integer) 1

1) "message"

2) "new"

3) "hello"


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

Redis的持久化:

RDB和AOF

RDB:调用系统调用forlk,创建一个子进程,子进程将内存中的数据快照到磁盘上去 

snapshot,二进制格式;按事先定制的策略,周期性地将数据保存至磁盘;数据文件默认为dump.rdb; 

技术分享

技术分享

RDB恢复不是最新的。


客户端也可显式使用SAVA或BGSAVE命令动快照保存机制

    SAVE: 同步,在主线程中保存快照;此时会阻塞所有客户端请求;

    BGSAVE:异步,不会阻塞主线程


AOF:Append Only File

记录每一次写操作至指定的文件尾部实现持久化;当redis重启时,可通过重新执行文件中的命令在内存重建数据库;

BGREWRITEAOF:完成AOF文件重写;通过重写将文件变小。

不会读取正在使用AOF文件,通过快照而通过将内存中的数据以命令的方式保存到临时文件中,完成之后替换原来的AOF文件;

技术分享RDB:(rdb的配置)

配置文件:

SAVE 900 1=============900s发生一个键改变

SAVE 300 10

SAVE 60 10000


stop-writes-on-bgsave-error yes==============

rdbcompression yes ==========================rdb压缩

rdbchecksum yes==============================校验

dbfilename dump.rdb==========================

dir /var/lib/redis============================


127.0.0.1:6379> CONFIG GET dir

1) "dir"

2) "/var/lib/redis"


关闭RDB============save ""

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

AOF:

重写过程

(1) redis主进程通过fork创建子进程;

(2) 子进程根据redis内存中的数据创建数据库重建命令序列于临时文件中;

(3) 父进程继承client的请求,并会把这些请求中的写操作继续追加至原来AOF文件;额外地,这些新的写请求还会被放置于一个缓冲队列中;

(4) 子进程重写完成,会通知父进程;父进程把缓冲中的命令写到临时文件中

(5) 父进程用临时文件替换老的aof文件;


技术分享



aof配置文件相关参数:

appendonly no===========================没有开启aof

appendfilename "appendonly.aof"    

appendfsync {always|everysec|no}==========写操作写入文件,每秒写入文件(推荐)

no-appendfsync-on-rewrite no========重写的时候对新写的操作不做fsync,存到cache

auto-aof-rewrite-percentage 100==========是上次重写的2倍,自动操作重写

auto-aof-rewrite-min-size 64mb============64m以上重写


注意:持久本身不能取代备份;还应该制定备份策略,对redis数据库定期进行备份;


RDB与AOF同时启用:

(1) BGSAVE和BGREWRITEAOF不会同时执行,两者选一;

(2) 在Redis服务器启动用于恢复数据时,会优先使用AOF;


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


复制:

特点:

一个Master可以有多个Slave;支持链式复制;

技术分享

Master以非阻塞方式同步数据至slave;

技术分享


创建从数据库


格式:

slave:

> SLAVAOF MASTER_IP MASTER_PORT



[root@des3 baginstall]# yum install redis-3.0.7-2.el6.remi.x86_64.rpm -y

[root@des3 baginstall]# vim /etc/redis.conf 

# bind 192.168.1.100 10.0.0.1

# bind 127.0.0.1

bind 127.0.0.1 192.168.145.13    ===========(0.0.0.0)

[root@des3 baginstall]# service redis start

启动 :                                                    [确定]

[root@des3 baginstall]# redis-cli 

127.0.0.1:6379> SLAVEOF 192.168.145.10 6379

OK

127.0.0.1:6379> get ip

"192.168.10.1



或者:

[root@des3 baginstall]# vim /etc/redis.conf 

# bind 192.168.1.100 10.0.0.1

# bind 127.0.0.1

bind 192.168.145.13    ===========(0.0.0.0)

[root@des3 baginstall]# service redis start

启动 :                                                    [确定]

[root@des3 baginstall]# redis-cli -h 192.168.145.13 


192.168.145.13:6379> SLAVEOF 192.168.145.10 6379

OK

192.168.145.13:6379> info replication

# Replication

role:slave

master_host:192.168.145.10

master_port:6379

master_link_status:up

master_last_io_seconds_ago:9

master_sync_in_progress:0

slave_repl_offset:3372

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0



192.168.145.13:6379> get ip

"192.168.10.1"


192.168.145.13:6379> KEYS *

1) "ip1"

2) "ip"

3) "port"


[root@des3 ~]# tail /var/log/redis/redis.log 

2982:S 03 Apr 03:37:44.939 * Connecting to MASTER 192.168.145.10:6379

2982:S 03 Apr 03:37:44.940 * MASTER <-> SLAVE sync started

2982:S 03 Apr 03:37:44.940 * Non blocking connect for SYNC fired the event.

2982:S 03 Apr 03:37:44.941 * Master replied to PING, replication can continue...

2982:S 03 Apr 03:37:44.942 * Partial resynchronization not possible (no cached master)

2982:S 03 Apr 03:37:44.945 * Full resync from master: 9f36db1a059399e31174b89076d5afcf9db17594:1

2982:S 03 Apr 03:37:45.042 * MASTER <-> SLAVE sync: receiving 71 bytes from master

2982:S 03 Apr 03:37:45.042 * MASTER <-> SLAVE sync: Flushing old data

2982:S 03 Apr 03:37:45.043 * MASTER <-> SLAVE sync: Loading DB in memory

2982:S 03 Apr 03:37:45.043 * MASTER <-> SLAVE sync: Finished with success


例外:

[root@des3 ~]# redis-server --help

Usage: ./redis-server [/path/to/redis.conf] [options]

       ./redis-server - (read config from stdin)

       ./redis-server -v or --version

       ./redis-server -h or --help

       ./redis-server --test-memory <megabytes>


Examples:

       ./redis-server (run the server with default conf)

       ./redis-server /etc/redis/6379.conf

       ./redis-server --port 7777

       ./redis-server --port 7777 --slaveof 127.0.0.1 8888

       ./redis-server /etc/myredis.conf --loglevel verbose


Sentinel mode:

       ./redis-server /etc/sentinel.conf --sentinel



配置文件:

slave-serve-stale-data yes

slave-serve-stale-data yes

# With slow disks and fast (large bandwidth) networks, diskless replication

# works better.

repl-diskless-sync no==============当网络快磁盘慢改为yes

repl-diskless-sync-delay 5

# repl-ping-slave-priod 10

# repl-timeout 60

repl-disable-tcp-nodelay no

# By default the priority is 100.

slave-priority 100

# By default the priority is 100.

slave-priority 100

# For example to require at least 3 slaves with a lag <= 10 seconds use:

#

# min-slaves-to-write 3================小于3台从服务器,主服务器将解决写操作

# min-slaves-max-lag 10===============大于10秒;主服务器将解决写操作

# Setting one or the other to 0 disables the feature.

# By default min-slaves-to-write is set to 0 (feature disabled) and

# min-slaves-max-lag is set to 10.



注意:如果master使用requirepass开启了认证功能,从服务器要使用masterauth <PASSWORD>来连入服务请求使用此密码进行认证;


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


sentinel:哨兵


技术分享

用于管理多个redis服务实现HA,一般服务器需要单数1、3、5台等。




程序:

redis-sentinel /path/to/file.conf

redis-server  /path/to/file.conf --sentinel


(1) 服务器自身初始化,运行redis-server中专用于sentinel功能的代码;

(2) 初始化sentinel状态,根据给定的配置文件,初始化监控的master服务器列表;

(3) 创建连向master的连接;


专用配置文件:/etc/redis-sentinel.conf==============最好有3个

[root@des3 ~]# vim /etc/redis-sentinel.conf

(1) # sentinel monitor <master-name> <ip> <redis-port> <quorum>


sentinel monitor mymaster 127.0.0.1 6379 2===============可以多次


(2) sentinel down-after-milliseconds <master-name> <milliseconds>


sentinel down-after-milliseconds mymaster 30000=================主节点健康状态


(3) sentinel parallel-syncs <master-name> <numslaves>


sentinel parallel-syncs mymaster 1==============================新主服务器上线要多少从服务器同步请求


(4) sentinel failover-timeout <master-name> <milliseconds>


sentinel failover-timeout mymaster 180000======================提升新主服务器超时时间 

 

 主观下线,客观下线:

 主观下线:一个sentinel实例判断出某节点下线;

 客观下线:多个sentinel节点协商后判断出某节点下线;


 专用命令:

 SENTINEL masters======================================列出所有的主服务器

 SENTINEL slaves <master name>=========================列出主服务器下面的从节点

 SENTINEL get-master-addr-by-name <master name>===============根据名字获取地址

 SENTINEL reset================================================重置

 SENTINEL failover <master name>================================手动,强制做主服务器

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


一主二从 + 1监控

准备

[root@des3 ~]# ss -tnl|grep 6379

LISTEN     0      128          192.168.145.13:6379                     *:*     

[root@des3 ~]# killall redis-server

[root@des3 ~]# ss -tnl|grep 6379


创建redis目录技术分享

root@des3 ~]# mkdir /etc/redis -pv

mkdir: 已创建目录 "/etc/redis"

[root@des3 ~]# cd /etc/redis

[root@des3 redis]# ls

[root@des3 redis]# cp /etc/redis.conf ./

[root@des3 redis]# ls

redis.conf

[root@des3 redis]# cp redis.conf redis.conf.2

[root@des3 redis]# cp redis.conf redis.conf.2

cp:是否覆盖"redis.conf.2"? y

[root@des3 redis]# cp redis.conf redis.conf.3

[root@des3 redis]# ls

redis.conf  redis.conf.2  redis.conf.3


[root@des3 redis]# mkdir -pv /redis/db{1,2,3}

mkdir: 已创建目录 "/redis/db1"

mkdir: 已创建目录 "/redis/db2"

mkdir: 已创建目录 "/redis/db3"

[root@des3 redis]# chown -R redis.redis ./*

[root@des3 redis]# ll

总用量 12

drwxr-xr-x. 2 redis redis 4096 4月   3 06:44 db1

drwxr-xr-x. 2 redis redis 4096 4月   3 06:44 db2

drwxr-xr-x. 2 redis redis 4096 4月   3 06:44 db3


[root@des3 redis]# vim /etc/redis/redis.conf

# bind 192.168.1.100 10.0.0.1

# bind 127.0.0.1

bind  0.0.0.0


# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.

daemonize yes


# slaveof <masterip> <masterport>

               


[root@des3 redis]# vim /etc/redis/redis.conf.2

# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.

daemonize yes


# If port 0 is specified Redis will not listen on a TCP socket.

port 6380


# default. You can specify a custom pid file location here.

pidfile /var/run/redis2.pid


# output for logging but daemonize, logs will be sent to /dev/null

logfile /var/log/redis/redis2.log


# Note that you must specify a directory here, not a file name.

dir /redis/db2


# bind 127.0.0.1

bind  0.0.0.0


# slaveof <masterip> <masterport>

               




[root@des3 redis]# cp /etc/redis/redis.conf.2 /etc/redis/redis.conf.3

# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.

daemonize yes


# If port 0 is specified Redis will not listen on a TCP socket.

port 6381


# default. You can specify a custom pid file location here.

pidfile /var/run/redis3.pid


# output for logging but daemonize, logs will be sent to /dev/null

logfile /var/log/redis/redis3.log



# Note that you must specify a directory here, not a file name.

dir /redis/db3


# slaveof <masterip> <masterport>

               


启动3个服务器

[root@des3 redis]# killall redis-server

[root@des3 redis]# redis-server /etc/redis/redis.conf

[root@des3 redis]# ss -tnl|grep 6379

LISTEN     0      128                       *:6379                     *:*     

[root@des3 redis]# redis-server /etc/redis/redis.conf.2

[root@des3 redis]# ss -tnl|grep 63

LISTEN     0      128                       *:6379                     *:*     

LISTEN     0      128                       *:6380                     *:*     

LISTEN     0      128               127.0.0.1:631                      *:*     

LISTEN     0      128                     ::1:631                     :::*     

[root@des3 redis]# redis-server /etc/redis/redis.conf.3

[root@des3 redis]# ss -tnl|grep 63

LISTEN     0      128                       *:6379                     *:*     

LISTEN     0      128                       *:6380                     *:*     

LISTEN     0      128                       *:6381                     *:*     

LISTEN     0      128               127.0.0.1:631                      *:*     

LISTEN     0      128                     ::1:631                     :::*  


主节点

root@des3 baginstall]# redis-cli -h 192.168.145.13 -p 6379

192.168.145.13:6379> info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

192.168.145.13:6379>


从节点

[root@des3 redis]# redis-cli -h 192.168.145.13 -p 6380

192.168.145.13:6380> info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

192.168.145.13:6380> SLAVEOF 192.168.145.13 6379

OK

192.168.145.13:6380> info replication

# Replication

role:slave

master_host:192.168.145.13

master_port:6379

master_link_status:up

master_last_io_seconds_ago:2

master_sync_in_progress:0

slave_repl_offset:1

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

192.168.145.13:6380> 


[root@des3 ~]# redis-cli -h 192.168.145.13 -p 6381

192.168.145.13:6381> info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

192.168.145.13:6381> SLAVEOF 192.168.145.13 6379

OK

192.168.145.13:6381> info replication

# Replication

role:slave

master_host:192.168.145.13

master_port:6379

master_link_status:up

master_last_io_seconds_ago:4

master_sync_in_progress:0

slave_repl_offset:253

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

192.168.145.13:6381>


主节点

root@des3 baginstall]# redis-cli -h 192.168.145.13 -p 6379

192.168.145.13:6379> info replication

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

192.168.145.13:6379> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.145.13,port=6380,state=online,offset=15,lag=0

master_repl_offset:15

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:14

192.168.145.13:6379> info replication

# Replication

role:master

connected_slaves:2

slave0:ip=192.168.145.13,port=6380,state=online,offset=477,lag=1

slave1:ip=192.168.145.13,port=6381,state=online,offset=477,lag=1

master_repl_offset:477

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:476

192.168.145.13:6379> 


192.168.145.13:6379> set cache varnish

OK

192.168.145.13:6379> KEYS *

1) "port"

2) "cache"

3) "ip1"

4) "ip"

192.168.145.13:6379> get cache

"varnish"


从节点

192.168.145.13:6380> KEYS *

1) "cache"

2) "ip"

3) "ip1"

4) "port"


192.168.145.13:6381> KEYS *

1) "ip"

2) "ip1"

3) "cache"

4) "port"


创建监控

[root@des3 ~]# cp /etc/redis-sentinel.conf /etc/redis/

[root@des3 ~]# vim /etc/redis/redis-sentinel.conf 

# The valid charset is A-z 0-9 and the three characters ".-_".

sentinel monitor mymaster 192.168.145.13 6379 1

sentinel down-after-milliseconds mymaster 5000

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 60000


启动

[root@des3 ~]# redis-sentinel /etc/redis/redis-sentinel.conf 


另外一台

[root@des3 ~]# ss -tnl|grep 26379

LISTEN     0      128                       *:26379                    *:*     

LISTEN     0      128                      :::26379                   :::*  


[root@des3 ~]# redis-cli -h 192.168.145.13 -p 26379

192.168.145.13:26379> INFO

# Server

redis_version:3.0.7

redis_git_sha1:00000000

redis_git_dirty:0

redis_build_id:5ece30e075eed8d2

redis_mode:sentinel

os:Linux 2.6.32-573.el6.x86_64 x86_64

arch_bits:64

multiplexing_api:epoll

gcc_version:4.4.7

process_id:53414

run_id:96adac8dcd0b2576ce179fdbb2c883963b7e4381

tcp_port:26379

uptime_in_seconds:150

uptime_in_days:0

hz:13

lru_clock:23604

config_file:/etc/redis/redis-sentinel.conf


# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

master0:name=mymaster,status=ok,address=192.168.145.13:6379,slaves=2,sentinels=1



192.168.145.13:26379> sentinel masters=======================主节点的信息

1)  1) "name"

    2) "mymaster"

    3) "ip"

    4) "192.168.145.13"

    5) "port"

    6) "6379"

    7) "runid"

    8) "bd6b0fd3a60fc4eac64c251d4bb32672a9186827"

    9) "flags"

   10) "master"

   11) "pending-commands"

   12) "0"

   13) "last-ping-sent"

   14) "0"

   15) "last-ok-ping-reply"

   16) "222"

   17) "last-ping-reply"

   18) "222"

   19) "down-after-milliseconds"

   20) "5000"

   21) "info-refresh"

   22) "9854"

   23) "role-reported"

   24) "master"

   25) "role-reported-time"

   26) "290906"

   27) "config-epoch"

   28) "0"

   29) "num-slaves"

   30) "2"

   31) "num-other-sentinels"

   32) "0"

   33) "quorum"

   34) "1"

   35) "failover-timeout"

   36) "60000"

   37) "parallel-syncs"

   38) "1"


192.168.145.13:26379> sentinel slaves  mymaster=======================从节点

1)  1) "name"

    2) "192.168.145.13:6381"

    3) "ip"

    4) "192.168.145.13"

    5) "port"

    6) "6381"

    7) "runid"

    8) "1da5e649451a68551917ac07c315299cb19991de"

    9) "flags"

   10) "slave"

   11) "pending-commands"

   12) "0"

   13) "last-ping-sent"

   14) "0"

   15) "last-ok-ping-reply"

   16) "151"

   17) "last-ping-reply"

   18) "151"

   19) "down-after-milliseconds"

   20) "5000"

   21) "info-refresh"

   22) "5519"

   23) "role-reported"

   24) "slave"

   25) "role-reported-time"

   26) "447259"

   27) "master-link-down-time"

   28) "0"

   29) "master-link-status"

   30) "ok"

   31) "master-host"

   32) "192.168.145.13"

   33) "master-port"

   34) "6379"

   35) "slave-priority"

   36) "100"

   37) "slave-repl-offset"

   38) "33421"

2)  1) "name"

    2) "192.168.145.13:6380"

    3) "ip"

    4) "192.168.145.13"

    5) "port"

    6) "6380"

    7) "runid"

    8) "73c3e8142dbe8c76d74969f7935816d378bf0a98"

    9) "flags"

   10) "slave"

   11) "pending-commands"

   12) "0"

   13) "last-ping-sent"

   14) "0"

   15) "last-ok-ping-reply"

   16) "151"

   17) "last-ping-reply"

   18) "151"

   19) "down-after-milliseconds"

   20) "5000"

   21) "info-refresh"

   22) "5519"

   23) "role-reported"

   24) "slave"

   25) "role-reported-time"

   26) "447261"

   27) "master-link-down-time"

   28) "0"

   29) "master-link-status"

   30) "ok"

   31) "master-host"

   32) "192.168.145.13"

   33) "master-port"

   34) "6379"

   35) "slave-priority"

   36) "100"

   37) "slave-repl-offset"

   38) "33421"


主节点宕机

[root@des3 ~]# ps aux|grep 6379

root      53089  0.0  0.7 139380  7308 ?        Ssl  07:22   0:02 redis-server 0.0.0.0:6379         

root      53137  0.0  0.4  21304  4924 pts/1    S+   07:24   0:00 redis-cli -h 192.168.145.13 -p 6379

root      53414  0.2  0.5 139380  5584 pts/4    Sl+  07:54   0:01 redis-sentinel *:26379 [sentinel]            

root      53421  0.0  0.4  21304  4924 pts/5    S+   07:56   0:00 redis-cli -h 192.168.145.13 -p 26379

root      53492  0.0  0.0 103320   864 pts/6    S+   08:06   0:00 grep 6379

[root@des3 ~]# kill 53089


回到monitor

192.168.145.13:26379> INFO sentinel

# Sentinel

sentinel_masters:1

sentinel_tilt:0

sentinel_running_scripts:0

sentinel_scripts_queue_length:0

master0:name=mymaster,status=ok,address=192.168.145.13:6381,slaves=2,sentinels=1


192.168.145.13:26379> sentinel masters

1)  1) "name"

    2) "mymaster"

    3) "ip"

    4) "192.168.145.13"

    5) "port"

    6) "6381"

    7) "runid"

    8) "1da5e649451a68551917ac07c315299cb19991de"

    9) "flags"

   10) "master"

   11) "pending-commands"

   12) "0"

   13) "last-ping-sent"

   14) "0"

   15) "last-ok-ping-reply"

   16) "337"

   17) "last-ping-reply"

   18) "337"

   19) "down-after-milliseconds"

   20) "5000"

   21) "info-refresh"

   22) "2853"

   23) "role-reported"

   24) "master"

   25) "role-reported-time"

   26) "223832"

   27) "config-epoch"

   28) "1"

   29) "num-slaves"

   30) "2"

   31) "num-other-sentinels"

   32) "0"

   33) "quorum"

   34) "1"

   35) "failover-timeout"

   36) "60000"

   37) "parallel-syncs"

   38) "1"

192.168.145.13:26379> 


回到6380端口的节点

192.168.145.13:6380> INFO replication

# Replication

role:slave

master_host:192.168.145.13

master_port:6381

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:27140

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0


启动宕机节点

[root@des3 ~]# redis-server /etc/redis/redis.conf


[root@des3 ~]# ss -tnlp|grep 6379

LISTEN     0      128                       *:6379                     *:*      users:(("redis-server",53521,4))

LISTEN     0      128                       *:26379                    *:*      users:(("redis-sentinel",53414,5))

LISTEN     0      128                      :::26379                   :::*      users:(("redis-sentinel",53414,4))



监控

192.168.145.13:26379> sentinel slaves mymaster

1)  1) "name"

    2) "192.168.145.13:6379"

    3) "ip"

    4) "192.168.145.13"

    5) "port"

    6) "6379"

    7) "runid"

    8) "4420df145b311608a99453960946f8f593039c3c"

    9) "flags"

   10) "slave"

   11) "pending-commands"

   12) "0"

   13) "last-ping-sent"

   14) "0"

   15) "last-ok-ping-reply"

   16) "211"

   17) "last-ping-reply"

   18) "211"

   19) "down-after-milliseconds"

   20) "5000"

   21) "info-refresh"

   22) "6801"

   23) "role-reported"

   24) "slave"

   25) "role-reported-time"

   26) "187261"

   27) "master-link-down-time"

   28) "0"

   29) "master-link-status"

   30) "ok"

   31) "master-host"

   32) "192.168.145.13"

   33) "master-port"

   34) "6381"

   35) "slave-priority"

   36) "100"

   37) "slave-repl-offset"

   38) "52692"

2)  1) "name"

    2) "192.168.145.13:6380"

    3) "ip"

    4) "192.168.145.13"

    5) "port"

    6) "6380"

    7) "runid"

    8) "73c3e8142dbe8c76d74969f7935816d378bf0a98"

    9) "flags"

   10) "slave"

   11) "pending-commands"

   12) "0"

   13) "last-ping-sent"

   14) "0"

   15) "last-ok-ping-reply"

   16) "211"

   17) "last-ping-reply"

   18) "211"

   19) "down-after-milliseconds"

   20) "5000"

   21) "info-refresh"

   22) "6800"

   23) "role-reported"

   24) "slave"

   25) "role-reported-time"

   26) "749648"

   27) "master-link-down-time"

   28) "0"

   29) "master-link-status"

   30) "ok"

   31) "master-host"

   32) "192.168.145.13"

   33) "master-port"

   34) "6381"

   35) "slave-priority"

   36) "100"

   37) "slave-repl-offset"

   38) "52692"





redis

标签:redis

原文地址:http://youenstudy.blog.51cto.com/6722910/1759752

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!