61 redis基础入门、redis应用进阶
01redis基础入门#安装redis
# rpm -ivh epel-release-latest-6.noarch.rpm
# yum localinstall redis-3.0.7-4.el6.art.i686.rpm
# cp /etc/redis.conf{,.orgi}
# vim /etc/redis.conf
修改
bind 127.0.0.1
为
bind 127.0.0.1 192.168.1.122
# service redis start
#连接
# redis-cli -h 192.168.1.122
192.168.1.122:6379> exit
# 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
127.0.0.1:6379> GET disto
"centosslackware"
#取得变更的长度
127.0.0.1:6379> STRLEN disto
(integer) 15
#增加变量的值
127.0.0.1:6379> SET count 0
OK
127.0.0.1:6379> INCR count
(integer) 1
127.0.0.1:6379> INCR count
(integer) 2
127.0.0.1:6379> INCR count
(integer) 3
127.0.0.1:6379> INCR count
(integer) 4
127.0.0.1:6379> INCR count
(integer) 5
#减少变量的值
127.0.0.1:6379> DECR count
(integer) 4
127.0.0.1:6379> DECR count
(integer) 3
127.0.0.1:6379> DECR count
(integer) 2
#键不存在时才设定其值
127.0.0.1:6379> SET disto gentoo NX
(nil)
127.0.0.1:6379> GET disto
"centosslackware"
#键存在时才设定其值
127.0.0.1:6379> SET boo bar XX
(nil)
127.0.0.1:6379> LPUSH l1 mon
(integer) 1
127.0.0.1:6379> LINDEX l1 0
"mon"
127.0.0.1:6379> LPUSH l1 sun
(integer) 2
127.0.0.1:6379> LINDEX l1 0
"sun"
127.0.0.1:6379> LINDEX l1 1
"mon"
127.0.0.1:6379> RPUSH l1 tue
(integer) 3
127.0.0.1:6379> LINDEX l1 2
"tue"
127.0.0.1:6379> LSET l1 1 fri
OK
127.0.0.1:6379> LINDEX l1 1
"fri"
127.0.0.1:6379> RPOP l1
"tue"
127.0.0.1:6379> RPOP l1
"fri"
127.0.0.1:6379> RPOP l1
"sun"
127.0.0.1:6379> RPOP l1
(nil)
#集合
#创建集合
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) "tue"
2) "thu"
#并集
127.0.0.1:6379> SUNION w1 w2
1) "tue"
2) "thu"
3) "sat"
4) "day"
5) "sun"
6) "fri"
7) "wed"
8) "mon"
#弹出元素
127.0.0.1:6379> SPOP w1
"sun"
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
(integer) 3
127.0.0.1:6379> ZCARD weekday1
(integer) 3
127.0.0.1:6379> ZRANK weekday1 tue
(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 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"
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> 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
02redis应用进阶
配置环境
Master 192.168.1.122 node2
Slave 192.168.1.121 node1
#添加认证功能
# vim /etc/redis.conf
修改
# requirepass foobared
为
requirepass mageedu
# service redis restart
# redis-cli -h 192.168.1.122
192.168.1.122:6379> SELECT 0
(error) NOAUTH Authentication required.
192.168.1.122:6379> AUTH mageedu
OK
192.168.1.122:6379> SELECT 0
OK
192.168.1.122:6379> exit
#清空当前库
# redis-cli -h 192.168.1.122
192.168.1.122:6379> FLUSHDB
OK
#事务
192.168.1.122:6379> MULTI
OK
192.168.1.122:6379> SET ip 192.168.1.1
QUEUED
192.168.1.122:6379> GET ip
QUEUED
192.168.1.122:6379> SET port 8080
QUEUED
192.168.1.122:6379> GET port
QUEUED
192.168.1.122:6379> EXEC
1) OK
2) "192.168.1.1"
3) OK
4) "8080"
在第一个终端执行
192.168.1.122:6379> WATCH ip
OK
192.168.1.122:6379> MULTI
OK
192.168.1.122:6379> SET ip 10.0.0.1
QUEUED
192.168.1.122:6379> GET ip
QUEUED
在第二个终端执行:
# redis-cli
127.0.0.1:6379> GET ip
"192.168.1.1"
127.0.0.1:6379> SET ip 172.16.100.99
OK
127.0.0.1:6379> GET ip
"172.16.100.99"
#返回第一个终端执行
192.168.1.122:6379> EXEC
(nil)
192.168.1.122:6379> MULTI
OK
192.168.1.122:6379> GET ip
QUEUED
192.168.1.122:6379> SET port 6379
QUEUED
192.168.1.122:6379> SETTTTT
(error) ERR unknown command 'SETTTTT'
192.168.1.122:6379> EXEC
(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
(nil)
127.0.0.1:6379> CLIENT SETNAME location
OK
127.0.0.1:6379> CLIENT GETNAME
"location"
127.0.0.1:6379> DBSIZE
(integer) 2
#在第一个终端执行
192.168.1.122:6379> SUBSCRIBE news
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "news"
3) (integer) 1
#在第二个终端执行:
# redis-cli
127.0.0.1:6379> PUBLISH news hello
(integer) 1
返回第一个终端显示:
1) "message"
2) "news"
3) "hello"
#第二个终端发送
127.0.0.1:6379> PUBLISH news redis
(integer) 1
#第一个终端显示
1) "message"
2) "news"
3) "redis"
#第一个终端执行
192.168.1.122:6379> PSUBSCRIBE "news.i"
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "news.i"
3) (integer) 1
#第二个终端执行
127.0.0.1:6379> PUBLISH news.io hello
(integer) 1
#此时第一个终端显示
1) "pmessage"
2) "news.i"
3) "news.io"
4) "hello"
#第二个终端发送
127.0.0.1:6379> PUBLISH news.it hello
(integer) 1
#此时第一个终端显示
1) "pmessage"
2) "news.i"
3) "news.it"
4) "hello"
# redis-cli
127.0.0.1:6379> CONFIG GET dir
1) "dir"
2) "/var/lib/redis"
127.0.0.1:6379> CONFIG SET appendonly yes
OK
#启动复制功能
#配置从服务器
# yum localinstall redis-3.0.7-4.el6.art.i686.rpm -y
# service redis start
# vim /etc/redis.conf
修改
bind 127.0.0.1
为
bind 127.0.0.1 192.168.1.121
# service redis restart
127.0.0.1:6379> SLAVEOF 192.168.1.122 6379
OK
127.0.0.1:6379> GET ip
"172.16.100.99"
127.0.0.1:6379> KEYS *
1) "ip"
2) "port"
# 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
# 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
127.0.0.1:6379> KEYS *
1) "ip"
2) "port"
#1、同一主机上启动多个redis服务,并配置主从服务
# killall redis-server
# mkdir /etc/redis -p
# cp /etc/redis.conf /etc/redis
# cd /etc/redis
# ls
redis.conf
# cp redis.conf{,.2}
# cp redis.conf{,.3}
# ls
redis.confredis.conf.2redis.conf.3
# mkdir -p /redis/db{1,2,3}
# chown -R redis.redis /redis/db*
# vim redis.conf
修改
bind 127.0.0.1
为
bind 0.0.0.0
修改
daemonize no
为
daemonize yes
# 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
修改
bind 127.0.0.1
为
bind 0.0.0.0
修改
daemonize no
为
daemonize yes
# cp redis.conf.{2,3}
# vim redis.conf.3
修改
pidfile /var/run/redis/redis2.pid
为
pidfile /var/run/redis/redis3.pid
修改
port 6380
为
port 6381
修改
logfile /var/log/redis/redis2.log
为
logfile /var/log/redis/redis3.log
修改
dir /redis/db2
为
dir /redis/db3
# redis-server /etc/redis/redis.conf
# redis-server /etc/redis/redis.conf.2
# redis-server /etc/redis/redis.conf.3
#默认为主服务器
# redis-cli -h 192.168.1.121 -p 6379
192.168.1.121: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
#设置为从服务器
# redis-cli -h 192.168.1.121 -p 6380
192.168.1.121: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.1.121:6380> slaveof 192.168.1.121 6379
OK
192.168.1.121:6380> info replication
# Replication
role:slave
master_host:192.168.1.121
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:43
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.1.121:6379> info replication
# Replication
role:master
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_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:84
#第二个从服务器
# redis-cli -h 192.168.1.121 -p 6381
192.168.1.121:6381> slaveof 192.168.1.121 6379
OK
192.168.1.121:6381> info replication
# Replication
role:slave
master_host:192.168.1.121
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:197
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.1.121:6379> info replication
# Replication
role:master
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_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:280
192.168.1.121:6379> SET ip 1.1.1.1
OK
192.168.1.121:6379> keys *
1) "ip"
2) "port"
192.168.1.121:6379> SET cache varnish
OK
#第一个从服务器测试
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实现高可用
# cp /etc/redis-sentinel.conf /etc/redis
# cd /etc/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
# redis-sentinel /etc/redis/redis-sentinel.conf
# 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
1)1) "name"
2) "192.168.1.121:6381"
3) "ip"
4) "192.168.1.121"
5) "port"
6) "6381"
7) "runid"
8) "b63391c22c2ba7e8a0b64f3584c0c0681b60e0e6"
9) "flags"
10) "slave"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "353"
17) "last-ping-reply"
18) "353"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "7511"
23) "role-reported"
24) "slave"
25) "role-reported-time"
26) "409168"
27) "master-link-down-time"
28) "0"
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"
3) "ip"
4) "192.168.1.121"
5) "port"
6) "6380"
7) "runid"
8) "ec7be925efaec88712836d12682e498ddac15ffd"
9) "flags"
10) "slave"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "353"
17) "last-ping-reply"
18) "353"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "7511"
23) "role-reported"
24) "slave"
25) "role-reported-time"
26) "409197"
27) "master-link-down-time"
28) "0"
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"
#停掉主服务器
# ps -ef | grep redis-server
root 17120 10 09:56 ? 00:00:05 redis-server 0.0.0.0:6379
root 17127 10 09:57 ? 00:00:05 redis-server 0.0.0.0:6380
root 17134 10 09:58 ? 00:00:05 redis-server 0.0.0.0:6381
root 17350 173250 10:35 pts/5 00:00:00 grep redis-server
# kill 17120
新的主服务器变为6381
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:836
uptime_in_days:0
hz:13
lru_clock:8571221
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:6381,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) "6381"
7) "runid"
8) "b63391c22c2ba7e8a0b64f3584c0c0681b60e0e6"
9) "flags"
10) "master"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "782"
17) "last-ping-reply"
18) "782"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "9017"
23) "role-reported"
24) "master"
25) "role-reported-time"
26) "149664"
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"
#6380从服务器,其主服务器也自动变为6381
192.168.1.121:6380> info replication
# Replication
role:slave
master_host:192.168.1.121
master_port:6381
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:32393
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
重新启动原来的主服务器
# redis-server /etc/redis/redis.conf
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:1401
uptime_in_days:0
hz:10
lru_clock:8571786
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:6381,slaves=2,sentinels=1
#其主节点仍然是6381
192.168.1.121:26379> SENTINEL slaves mymaster
1)1) "name"
2) "192.168.1.121:6380"
3) "ip"
4) "192.168.1.121"
5) "port"
6) "6380"
7) "runid"
8) "ec7be925efaec88712836d12682e498ddac15ffd"
9) "flags"
10) "slave"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "319"
17) "last-ping-reply"
18) "319"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "9957"
23) "role-reported"
24) "slave"
25) "role-reported-time"
26) "672844"
27) "master-link-down-time"
28) "0"
29) "master-link-status"
30) "ok"
31) "master-host"
32) "192.168.1.121"
33) "master-port"
34) "6381"
35) "slave-priority"
36) "100"
37) "slave-repl-offset"
38) "46232"
2)1) "name"
2) "192.168.1.121:6379"
3) "ip"
4) "192.168.1.121"
5) "port"
6) "6379"
7) "runid"
8) "879035ca537f48727c21f455cc1e69c396d436fb"
9) "flags"
10) "slave"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "319"
17) "last-ping-reply"
18) "319"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "1140"
23) "role-reported"
24) "slave"
25) "role-reported-time"
26) "91410"
27) "master-link-down-time"
28) "0"
29) "master-link-status"
30) "ok"
31) "master-host"
32) "192.168.1.121"
33) "master-port"
34) "6381"
35) "slave-priority"
36) "100"
37) "slave-repl-offset"
38) "46951"
#原来的主节点重启之后变为从节点
页:
[1]