redis
与memcached的不同
持久化
Redis的组件
安装Redis
[root@localhost Downloads]# curl -O http://download.redis.io/releases/redis-3.2.8.tar.gz
tar -xf redis-3.2.8.tar.gz -C /usr/local/
cd /usr/local/redis-3.2.8/
make
make install
/usr/local/redis-3.2.8/utils/install_server.sh
redis的配置文件
[root@localhost redis]# pwd
/etc/redis
[root@localhost redis]# ls
6379.conf
bind 127.0.0.1 ::1 192.168.217.17 #绑定的IP
unixsocket /tmp/redis.sock #unixsock文件的位置
redis的客户端命令
[root@localhost init.d]# redis-cli
127.0.0.1:6379> 127.0.0.1:6379> help
redis-cli 3.2.5
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli perferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
使用select 1,去启用第一个数据库,而最多是16个。(或者说是名称空间)
127.0.0.1:6379[1]> help set
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
group: string
set去设置一个键值对。get去获取键值对。
127.0.0.1:6379> set name redhat
OK
127.0.0.1:6379> get name
"redhat"
127.0.0.1:6379> set name centos
OK
127.0.0.1:6379> get name
"centos"
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> set key value [EX seconds] [PX milliseconds] [NX|XX]
127.0.0.1:6379> lpush list 1
(integer) 1
127.0.0.1:6379> lindex list 0
"1"
set的操作
redis 的认证方法
[root@localhost redis]# redis-cli
127.0.0.1:6379> get 1
(error) NOAUTH Authentication required.
127.0.0.1:6379> AUTH redis
OK
127.0.0.1:6379>
清空数据库
redis的事务
[root@localhost redis]# redis-cli
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set host1 centos
QUEUED
127.0.0.1:6379> get host1
QUEUED
127.0.0.1:6379> set host2 redhat
QUEUED
127.0.0.1:6379> get host2
QUEUED
127.0.0.1:6379> exec
1) OK
2) "centos"
3) OK
4) "redhat"
"opensuse"
OK
127.0.0.1:6379> multi
OK
OK
127.0.0.1:6379> get host1
"redhat"
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379>
redis 的主从复制
[root@node1 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.217.16,port=6379,state=online,offset=71,lag=1
master_repl_offset:71
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:70
127.0.0.1:6379> 观察从服务器的状态。[root@node2 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.217.17
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_repl_offset:99
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>
127.0.0.1:6379> MONITOR
OK
1491651464.592468 [0 192.168.217.17:6379] "PING"
1491651474.721863 [0 192.168.217.17:6379] "PING"
1491651485.674035 [0 192.168.217.17:6379] "PING"
redis的HA管理工具。
[root@node1 redis-3.2.5]# pwd
/usr/local/redis-3.2.5
[root@node1 redis]# redis-cli -p 26379
127.0.0.1:26379> INFO sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.217.17:6379,slaves=2,sentinels=1
127.0.0.1:26379>
确认master的信息。sentinel masters
127.0.0.1:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "192.168.217.17"
5) "port"
6) "6379"
7) "runid"
...
127.0.0.1:26379> sentinel slaves mymaster
1) 1) "name"
2) "192.168.217.16:6379"
3) "ip"
4) "192.168.217.16"
...
2) 1) "name"
2) "192.168.217.15:6379"
3) "ip"
4) "192.168.217.15"
...
[root@node3 ~]# ps aux |grep redis
root 1324 0.1 0.4 133524 2352 ? Ssl 01:31 0:02 /usr/local/bin/redis-server *:6379
root 1354 0.3 0.4 133528 2236 ? Ssl 01:57 0:00 redis-server *:26379 [sentinel]
停止master。
127.0.0.1:26379> SHUTDOWN
not connected>
主从切换
401




被折叠的 条评论
为什么被折叠?



