redis使用入门

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis使用入门相关的知识,希望对你有一定的参考价值。

redis介绍:

redis是一个key-value的内存存储系统,和memcache类似,但能够支持数据持久化。redis支持存储的数据类型非常多,包括:字符串、哈希表、链表、集合、有序集合以及基于这些数据类型的相关操作。redis使用C语言开发,针对内存分配方面做了优化。Redis支持的客户端语言也非常丰富,常用的计算机语言如C、C#、C++、Object-C、php、Python、 Java、Perl、Lua、Erlang等均有可用的客户端来访问Redis服务器。目前redis无论在大公司还是小公司都有着广泛的应用。

 

redis安装:

redis安装比较简单,直接下载源码编译安装即可,当时事先要安装gcc,以redhat系列系统为例:

# yum install -y gcc
# tar -czvf redis.tar.gz
# cd redis
# make && make test && make install

 make test这一步是测试redis的基本功能,确保redis能在你的机器上正常运行。

 

启动redis:

# redis-server /etc/redis/redis.conf

 配置文件的具体意义等下介绍

一个具体的配置文件如下:

daemonize yes
pidfile "/var/run/redis.pid"
port 6381
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel notice
logfile "/var/log/redis.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/var/lib/redis/"
masterauth "go-redis"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
repl-backlog-size 1mb
slave-priority 100
requirepass "go-redis"
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

 

设置redis的主备关系:

# redis-cli -a testpass -p 6381 
> slaveof 10.1.1.2 6381
OK

  或者执行:

# redis-cli -a testpass -p 6381 slaveof 10.1.1.2 6381

  查看主备配对关系:

# redis-cli -p 6381 info replication
# Replication
role:slave
master_host:1.1.1.2
master_port:6381
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:0
master_link_down_since_seconds:1507368352
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:da725ce3153f5e4bf544924293259aaa31b728f2
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

  取消主备关系:

[[email protected] redis]# redis-cli -p 6381 slaveof NO ONE
OK
[[email protected] redis]# redis-cli -p 6381 info replication
# Replication
role:master
connected_slaves:0
master_replid:c4090d642878080ab4d59bbe847e94aab294d6b8
master_replid2:da725ce3153f5e4bf544924293259aaa31b728f2
master_repl_offset:0
second_repl_offset:1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

  配置sentinel:

 sentinel是来监控redis的组件,sentinel本身是一个集群,一般配置3个或者5个节点以保证高可用性,类似zookeeper集群。一个sentinel的典型的配置文件如下:

port 26380
daemonize yes
logfile "/var/log/sentinel_log.log"
dir "/tmp"
sentinel monitor master0 10.1.1.1 6381 2
sentinel down-after-milliseconds master0 15000
sentinel auth-pass master0 go-redis
sentinel config-epoch master0 156
sentinel leader-epoch master0 156
sentinel known-slave master0 10.1.1.1 6381
sentinel known-sentinel master0 10.1.1.1 26380 5970281bfa14f40f3ba8b75a2f87d570aac99bc8
sentinel known-sentinel master0 10.1.1.3 26380 59ee62deddd88597e591c288a65b20d865df12d6
sentinel monitor master2 10.1.1.3 6381 2
sentinel down-after-milliseconds master2 15000
sentinel auth-pass master2 go-redis
sentinel config-epoch master2 158
sentinel leader-epoch master2 158
sentinel known-slave master2 10.1.1.1 6381
sentinel known-slave master2 10.1.1.4 6381
sentinel known-sentinel master2 10.1.1.1 26380 5970281bfa14f40f3ba8b75a2f87d570aac99bc8
sentinel known-sentinel master2 10.1.1.3 26380 59ee62deddd88597e591c288a65b20d865df12d6
sentinel monitor master1 10.1.1.1 6382 2
sentinel down-after-milliseconds master1 15000
sentinel auth-pass master1 go-redis
sentinel config-epoch master1 153
sentinel leader-epoch master1 153
sentinel known-slave master1 10.1.1.2 6382
sentinel known-sentinel master1 10.1.1.1 26380 5970281bfa14f40f3ba8b75a2f87d570aac99bc8
sentinel known-sentinel master1 10.1.1.3 26380 59ee62deddd88597e591c288a65b20d865df12d6
sentinel monitor master3 10.1.1.3 6382 2
sentinel down-after-milliseconds master3 15000
sentinel auth-pass master3 go-redis
sentinel config-epoch master3 139
sentinel leader-epoch master3 135
sentinel known-slave master3 10.1.1.4 6382
sentinel known-sentinel master3 10.1.1.1 26380 5970281bfa14f40f3ba8b75a2f87d570aac99bc8
sentinel known-sentinel master3 10.1.1.3 26380 59ee62deddd88597e591c288a65b20d865df12d6
sentinel current-epoch 158

  

  这个例子中,10.1.1.1,10.1.1.2,10.1.1.3,这三台机器都部署sentinel,来监控10.1.1.1和10.1.1.2这两台redis服务器。

 

启动sentinel:

# redis-sentinel  /etc/redis/sentinel.conf

  

 




















































以上是关于redis使用入门的主要内容,如果未能解决你的问题,请参考以下文章

redis入门教程3-客户端

Cg入门20:Fragment shader - 片段级模型动态变色(实现汽车动态换漆)

Atom编辑器入门到精通 Atom使用进阶

Atom编辑器入门到精通 Atom使用进阶

Atom编辑器入门到精通 Atom使用进阶

[ Redis ] 小白入门 —— Redis在真实工作中是如何使用的?