1 解压
[iyunv@h2master local]# tar -zxvf redis-2.8.19.tar.gz
2 重命名
[iyunv@h2master local]# mv redis-2.8.19 redis
[iyunv@h2master local]# cd redis
3 编译
[iyunv@h2master redis]# make 将redis常用命令拷贝到/ usr/local/bin/下
4 安装
[iyunv@h2master redis]# make install
5 拷贝配置文件到/etc下
[iyunv@h2master redis]# cp redis.conf /etc/
6 修改配置文件
[iyunv@h2master redis]# vi /etc/redis.conf
daemonize yes 37行 设置后台启动
logfile /usr/local/redis/log 103行 设置redis日志位置
7 启动redis:
[iyunv@h2master local]# /usr/local/bin/redis-server /etc/redis.conf
8 查看启动:
[iyunv@h2master local]# ps -ef | grep redis
root 20372 1 0 22:09 ? 00:00:00 /usr/local/bin/redis-server *:6379
root 20396 11209 0 22:10 pts/3 00:00:00 grep redis
9 使用客户端连接redis服务:
[iyunv@h2master bin]# pwd
/usr/local/bin
[iyunv@h2master bin]# redis-cli 或者使用 redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set name zm
OK
127.0.0.1:6379> get name
"zm"
127.0.0.1:6379> exit;
(error) ERR unknown command 'exit;' 离开客户端
127.0.0.1:6379> quit
10 关闭redis
[iyunv@h2master bin]# redis-cli shutdown 这样redis会把内存中的数据拷贝到硬盘中 然后在关闭redis服务
[iyunv@h2master bin]# ps -ef | grep redis
root 20477 11209 0 22:14 pts/3 00:00:00 grep redis
[iyunv@h2master bin]# redis-cli
Could not connect to Redis at 127.0.0.1:6379: Connection refused 此时在连接连接不上服务端了 2 redis 多数据库特性:
a) 默认16个数据库,名称从0-15
b) flushall 将16个数据库的所有数据都清空, 多数据库之间并不是完全隔离的
c) 数据库切换写法:
127.0.0.1:6379> select 1 表示使用1号数据库
OK
127.0.0.1:6379[1]> set name zm 设置值
OK
127.0.0.1:6379[1]> exists zm 判断key是否存在 1表示存在 0表示不存在
(integer) 0
127.0.0.1:6379[1]> exists name
(integer) 1
3 redis 基本命令:
0 redis命令不区分大小写,
0 存储 set key XX
1 获得符合规则的键名称
keys 表达式(?,* ,[],\?)
? 匹配一个任意字符
* 匹配0-多个
[]匹配[]内的字符 比如[a-c]表示匹配a b c
/?表示匹配? \是转义字符
2 判断一个键是否存在
exists key 存在返回1 不存在返回0
3 删除键
del key
del key1 key2
4 批量删除
redis-cli keys "key*" | xargs redis-cli del 删除匹配key*的所有键值对
redis-cli del `redis-cli keys "key*"`
5 获得值的数据类型type
type key 得到对应值的数据类型
返回值可能是这五种类型(string,hash,list,set,zset)
127.0.0.1:6379> set name0 zm
OK
127.0.0.1:6379> set namea dabing
OK
127.0.0.1:6379> set name2 liang
OK
127.0.0.1:6379> set name3 xinxin
OK
127.0.0.1:6379> keys *
1) "name0"
2) "namea"
3) "name3"
4) "name2"
127.0.0.1:6379> exits key name0
(error) ERR unknown command 'exits'
127.0.0.1:6379> exists name0
(integer) 1
127.0.0.1:6379> del name0
(integer) 1
127.0.0.1:6379> type name2
string 4 redis 之help介绍:
127.0.0.1:6379> help
redis-cli 2.8.19
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
127.0.0.1:6379> help @string 按住tab键 help后面的命令将不停变换来提示
127.0.0.1:6379> 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 5 什么情况下使用redis?