01 redis基礎入門
#安裝redis
[root@node1 ~]# rpm -ivh epel-release-latest-6.noarch.rpm
[root@node2 ~]# yum localinstall redis-3.0.7-4.el6.art.i686.rpm
[root@node2 ~]# cp /etc/redis.conf{,.orgi}
[root@node2 ~]# vim /etc/redis.conf
修改
bind 127.0.0.1
為
bind 127.0.0.1 192.168.1.122
[root@node2 ~]# service redis start
#連接配接
[root@node2 ~]# redis-cli -h 192.168.1.122
192.168.1.122:6379> exit
[root@node2 ~]# redis-cli
127.0.0.1:6379>
#設定變量值
127.0.0.1:6379> SET disto fedora
OK
#獲得變更值
127.0.0.1:6379> GET disto
"fedora"
#在變量後面追加值
127.0.0.1:6379> append disto slackware
(integer) 15
"centosslackware"
#取得變更的長度
127.0.0.1:6379> STRLEN disto
#增加變量的值
127.0.0.1:6379> SET count 0
127.0.0.1:6379> INCR count
(integer) 1
(integer) 2
(integer) 3
(integer) 4
(integer) 5
#減少變量的值
127.0.0.1:6379> DECR count
#鍵不存在時才設定其值
127.0.0.1:6379> SET disto gentoo NX
(nil)
#鍵存在時才設定其值
127.0.0.1:6379> SET boo bar XX
127.0.0.1:6379> LPUSH l1 mon
127.0.0.1:6379> LINDEX l1 0
"mon"
127.0.0.1:6379> LPUSH l1 sun
"sun"
127.0.0.1:6379> LINDEX l1 1
127.0.0.1:6379> RPUSH l1 tue
127.0.0.1:6379> LINDEX l1 2
"tue"
127.0.0.1:6379> LSET l1 1 fri
"fri"
127.0.0.1:6379> RPOP l1
#集合
#建立集合
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
#交集
127.0.0.1:6379> SINTER w1 w2
1) "tue"
2) "thu"
#并集
127.0.0.1:6379> SUNION w1 w2
3) "sat"
4) "day"
5) "sun"
6) "fri"
7) "wed"
8) "mon"
#彈出元素
127.0.0.1:6379> SPOP w1
"sat"
#判斷元素mon是否在集合w1中
127.0.0.1:6379> SISMEMBER w1 mon
(integer) 1 #1表示在集合中
127.0.0.1:6379> SISMEMBER w1 sat
(integer) 0 #0表示不在集合中
127.0.0.1:6379> ZADD weekday1 1 mon 2 tue 3 wed
127.0.0.1:6379> ZCARD weekday1
127.0.0.1:6379> ZRANK weekday1 tue
127.0.0.1:6379> ZRANK weekday1 wed
127.0.0.1:6379> ZRANK weekday1 mon
(integer) 0
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
127.0.0.1:6379> HSET h1 a mon
127.0.0.1:6379> HGET h1 a
127.0.0.1:6379> HSET h1 b tue
127.0.0.1:6379> HGET h1 b
127.0.0.1:6379> HKEYS h1
1) "a"
2) "b"
127.0.0.1:6379> HVALS h1
127.0.0.1:6379> HLEN h1
02 redis應用進階
配置環境
Master 192.168.1.122 node2
Slave 192.168.1.121 node1
#添加認證功能
# requirepass foobared
requirepass mageedu
[root@node2 ~]# service redis restart
192.168.1.122:6379> SELECT 0
(error) NOAUTH Authentication required.
192.168.1.122:6379> AUTH mageedu
#清空目前庫
192.168.1.122:6379> FLUSHDB
#事務
192.168.1.122:6379> MULTI
192.168.1.122:6379> SET ip 192.168.1.1
QUEUED
192.168.1.122:6379> GET ip
192.168.1.122:6379> SET port 8080
192.168.1.122:6379> GET port
192.168.1.122:6379> EXEC
1) OK
2) "192.168.1.1"
3) OK
4) "8080"
在第一個終端執行
192.168.1.122:6379> WATCH ip
192.168.1.122:6379> SET ip 10.0.0.1
在第二個終端執行:
127.0.0.1:6379> GET ip
"192.168.1.1"
127.0.0.1:6379> SET ip 172.16.100.99
"172.16.100.99"
#傳回第一個終端執行
192.168.1.122:6379> SET port 6379
192.168.1.122:6379> SETTTTT
(error) ERR unknown command 'SETTTTT'
(error) EXECABORT Transaction discarded because of previous errors.
192.168.1.122:6379> PING
PONG
192.168.1.122:6379> ECHO "hello redis"
"hello redis"
127.0.0.1:6379> CLIENT GETNAME
127.0.0.1:6379> CLIENT SETNAME location
"location"
127.0.0.1:6379> DBSIZE
#在第一個終端執行
192.168.1.122:6379> SUBSCRIBE news
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "news"
3) (integer) 1
#在第二個終端執行:
127.0.0.1:6379> PUBLISH news hello
傳回第一個終端顯示:
1) "message"
3) "hello"
#第二個終端發送
127.0.0.1:6379> PUBLISH news redis
#第一個終端顯示
3) "redis"
#第一個終端執行
192.168.1.122:6379> PSUBSCRIBE "news.i[to]"
1) "psubscribe"
2) "news.i[to]"
#第二個終端執行
127.0.0.1:6379> PUBLISH news.io hello
#此時第一個終端顯示
1) "pmessage"
3) "news.io"
4) "hello"
127.0.0.1:6379> PUBLISH news.it hello
3) "news.it"
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis"
127.0.0.1:6379> CONFIG SET appendonly yes
#啟動複制功能
#配置從伺服器
[root@node1 ~]# yum localinstall redis-3.0.7-4.el6.art.i686.rpm -y
[root@node1 ~]# service redis start
[root@node1 ~]# vim /etc/redis.conf
bind 127.0.0.1 192.168.1.121
[root@node1 ~]# service redis restart
127.0.0.1:6379> SLAVEOF 192.168.1.122 6379
127.0.0.1:6379> KEYS *
1) "ip"
2) "port"
[root@node2 ~]# tail /var/log/redis/redis.log
13408:M 20 Jan 21:18:18.261 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
13408:M 20 Jan 21:18:18.262 * Background AOF rewrite finished successfully
13408:M 20 Jan 21:50:33.502 * Slave 192.168.1.121:6379 asks for synchronization
13408:M 20 Jan 21:50:33.502 * Full resync requested by slave 192.168.1.121:6379
13408:M 20 Jan 21:50:33.502 * Starting BGSAVE for SYNC with target: disk
13408:M 20 Jan 21:50:33.509 * Background saving started by pid 17459
17459:C 20 Jan 21:50:33.631 * DB saved on disk
17459:C 20 Jan 21:50:33.632 * RDB: 4 MB of memory used by copy-on-write
13408:M 20 Jan 21:50:33.699 * Background saving terminated with success
13408:M 20 Jan 21:50:33.700 * Synchronization with slave 192.168.1.121:6379 succeeded
[root@node1 ~]# redis-cli
127.0.0.1:6379> INFO REPLICATION
# Replication
role:slave
master_host:192.168.1.122
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:2115
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
#1、同一主機上啟動多個redis服務,并配置主從服務
[root@node1 ~]# killall redis-server
[root@node1 ~]# mkdir /etc/redis -p
[root@node1 ~]# cp /etc/redis.conf /etc/redis
[root@node1 ~]# cd /etc/redis
[root@node1 redis]# ls
redis.conf
[root@node1 redis]# cp redis.conf{,.2}
[root@node1 redis]# cp redis.conf{,.3}
redis.conf redis.conf.2 redis.conf.3
[root@node1 redis]# mkdir -p /redis/db{1,2,3}
[root@node1 redis]# chown -R redis.redis /redis/db*
[root@node1 redis]# vim redis.conf
bind 0.0.0.0
daemonize no
daemonize yes
[root@node1 redis]# vim redis.conf.2
pidfile /var/run/redis/redis.pid
pidfile /var/run/redis/redis2.pid
port 6379
port 6380
logfile /var/log/redis/redis.log
logfile /var/log/redis/redis2.log
dir /var/lib/redis/
dir /redis/db2
[root@node1 redis]# cp redis.conf.{2,3}
[root@node1 redis]# vim redis.conf.3
pidfile /var/run/redis/redis3.pid
port 6381
logfile /var/log/redis/redis3.log
dir /redis/db3
[root@node1 redis]# redis-server /etc/redis/redis.conf
[root@node1 redis]# redis-server /etc/redis/redis.conf.2
[root@node1 redis]# redis-server /etc/redis/redis.conf.3
#預設為主伺服器
[root@node1 redis]# redis-cli -h 192.168.1.121 -p 6379
192.168.1.121:6379> info replication
role:master
#設定為從伺服器
[root@node1 ~]# redis-cli -h 192.168.1.121 -p 6380
192.168.1.121:6380> info replication
192.168.1.121:6380> slaveof 192.168.1.121 6379
master_host:192.168.1.121
master_last_io_seconds_ago:5
slave_repl_offset:43
#主伺服器
connected_slaves:1
slave0:ip=192.168.1.121,port=6380,state=online,offset=85,lag=1
master_repl_offset:85
repl_backlog_active:1
repl_backlog_first_byte_offset:2
repl_backlog_histlen:84
#第二個從伺服器
[root@node1 ~]# redis-cli -h 192.168.1.121 -p 6381
192.168.1.121:6381> slaveof 192.168.1.121 6379
192.168.1.121:6381> info replication
master_last_io_seconds_ago:7
slave_repl_offset:197
connected_slaves:2
slave0:ip=192.168.1.121,port=6380,state=online,offset=281,lag=0
slave1:ip=192.168.1.121,port=6381,state=online,offset=281,lag=0
master_repl_offset:281
repl_backlog_histlen:280
192.168.1.121:6379> SET ip 1.1.1.1
192.168.1.121:6379> keys *
192.168.1.121:6379> SET cache varnish
#第一個從伺服器測試
192.168.1.121:6380> keys *
1) "cache"
2) "ip"
3) "port"
#第二個從伺服器測試
192.168.1.121:6381> keys *
1) "port"
2) "cache"
3) "ip"
結果:資料同步成功
#2、配置SENTINEL實作高可用
[root@node1 ~]# cp /etc/redis-sentinel.conf /etc/redis
[root@node1 redis]# vim redis-sentinel.conf
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel monitor mymaster 192.168.1.121 6379 1
sentinel down-after-milliseconds mymaster 30000
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 180000
sentinel failover-timeout mymaster 60000
[root@node1 redis]# redis-sentinel /etc/redis/redis-sentinel.conf
[root@node1 ~]# redis-cli -h 192.168.1.121 -p 26379
192.168.1.121:26379> info
# Server
redis_version:3.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:dec06ea72e680a6e
redis_mode:sentinel
os:Linux 2.6.32-573.el6.x86_64 x86_64
arch_bits:32
multiplexing_api:epoll
gcc_version:4.4.7
process_id:17275
run_id:5b209e48291bb6421865d633142928b450e9810c
tcp_port:26379
uptime_in_seconds:89
uptime_in_days:0
hz:10
lru_clock:8570474
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.1.121:6379,slaves=2,sentinels=1
192.168.1.121:26379> SENTINEL masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.1.121"
5) "port"
6) "6379"
7) "runid"
8) "17edeb0c1c6547dc08a2447a4b15f83752227619"
9) "flags"
10) "master"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "231"
17) "last-ping-reply"
18) "231"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "3443"
23) "role-reported"
24) "master"
25) "role-reported-time"
26) "225408"
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.1.121:26379> SENTINEL slaves mymaster
2) "192.168.1.121:6381"
6) "6381"
8) "b63391c22c2ba7e8a0b64f3584c0c0681b60e0e6"
10) "slave"
16) "353"
18) "353"
22) "7511"
24) "slave"
26) "409168"
27) "master-link-down-time"
29) "master-link-status"
30) "ok"
31) "master-host"
32) "192.168.1.121"
33) "master-port"
34) "6379"
35) "slave-priority"
36) "100"
37) "slave-repl-offset"
38) "29592"
2) 1) "name"
2) "192.168.1.121:6380"
6) "6380"
8) "ec7be925efaec88712836d12682e498ddac15ffd"
26) "409197"
#停掉主伺服器
[root@node1 ~]# ps -ef | grep redis-server
root 17120 1 0 09:56 ? 00:00:05 redis-server 0.0.0.0:6379
root 17127 1 0 09:57 ? 00:00:05 redis-server 0.0.0.0:6380
root 17134 1 0 09:58 ? 00:00:05 redis-server 0.0.0.0:6381
root 17350 17325 0 10:35 pts/5 00:00:00 grep redis-server
[root@node1 ~]# kill 17120
新的主伺服器變為6381
uptime_in_seconds:836
hz:13
lru_clock:8571221
master0:name=mymaster,status=ok,address=192.168.1.121:6381,slaves=2,sentinels=1
16) "782"
18) "782"
22) "9017"
26) "149664"
28) "1"
#6380從伺服器,其主伺服器也自動變為6381
master_port:6381
master_last_io_seconds_ago:0
slave_repl_offset:32393
重新啟動原來的主伺服器
uptime_in_seconds:1401
lru_clock:8571786
#其主節點仍然是6381
16) "319"
18) "319"
22) "9957"
26) "672844"
34) "6381"
38) "46232"
2) "192.168.1.121:6379"
8) "879035ca537f48727c21f455cc1e69c396d436fb"
22) "1140"
26) "91410"
38) "46951"
#原來的主節點重新開機之後變為從節點