>src/redis-server 启动 Redis 服务
或者>src/redis-server redis.conf
src/redis-server redis.conf 1>log.log 2>errlog.log
1为标准输出,2为错误输出
将 Redis 作为 Linux 服务随机启动
>vi /etc/rc.local, 加入代码: /root/install/redis-2.4.9/src/redis-server
客户端连接 src/redis-cli -p 端口号
>src/redis-cli shutdown 停止redis服务
增删改查:
> keys *
取出当前匹配的所有key
> exists larry
(integer) 0
当前的key是否存在
> del lv
删除当前key
> expire 设置过期时间 例如
> expire larry 10
(integer) 1
> move larry ad4 移动larry键值对到ad4数据库
(integer) 1
> persist lv 移除当前key的过期时间
(integer) 1
>randomkey 随机返回一个key
>rename 重命名key
>type 返回值的数据类型
type testlist
list
> ping 测试连接是否还在
PONG
>echo name 打印
"larry"
> select ad4databank 数据库切换
OK
> quit 退出连接
> dbsize
(integer) 12 当前数据库中key的数量
> info 服务器基本信息
> monitor 实时转储收到的请求
> config get 获取服务器的参数配置
> flushdb 清空当前数据库
> flushall 清除所有数据库
**********************************
需要查看当前库的数据量,有三种方法:keys *,dbsize,info(这里是有很多的数据参数,也是最近常使用的)
[root@fsailing1 bin]# ./redis-cli
redis 127.0.0.1:6379> keys *
1) "key2"
2) "age"
3) "ss"
4) "test"
5) "name"
6) "key1"
redis 127.0.0.1:6379> dbsize
(integer) 6
redis 127.0.0.1:6379> info
redis_version:2.4.15
redis_git_sha1:00000000
vm_enabled:0
role:master
db0:keys=6,expires=0
db1:keys=5,expires=0
查看某个key值时,exists删除某个key值时del
redis 127.0.0.1:6379> exists age
(integer) 1
redis 127.0.0.1:6379> exists chen
(integer) 0
redis 127.0.0.1:6379> del age
(integer) 1
redis 127.0.0.1:6379> exists age
(integer) 0
key值的重命名问题rename/renamenx
redis 127.0.0.1:6379> keys *
1) "key2"
2) "ss"
3) "test"
4) "name"
5) "key1"
redis 127.0.0.1:6379> rename ss chen
OK
redis 127.0.0.1:6379> renamenx key1 chen
(integer) 0
redis 127.0.0.1:6379> keys *
1) "key2"
2) "chen"
3) "test"
4) "name"
5) "key1"
设置过期时间和持久化expire/persist还有个命令就是查看当前的情况,ttl意思是time to live
redis 127.0.0.1:6379> keys *
1) "key2"
2) "ss"
3) "test"
4) "name"
5) "key1"
redis 127.0.0.1:6379> rename ss chen
OK
redis 127.0.0.1:6379> renamenx key1 chen
(integer) 0
redis 127.0.0.1:6379> keys *
1) "key2"
2) "chen"
3) "test"
4) "name"
5) "key1"
redis 127.0.0.1:6379> expire chen 10
(integer) 1
redis 127.0.0.1:6379> ttl chen
(integer) 6
redis 127.0.0.1:6379> ttl chen
(integer) 4
redis 127.0.0.1:6379> ttl chen
(integer) 2
redis 127.0.0.1:6379> keys *
1) "key2"
2) "test"
3) "name"
4) "key1"
redis 127.0.0.1:6379> expire test 100
(integer) 1
redis 127.0.0.1:6379> ttl test
(integer) 94
redis 127.0.0.1:6379> persist test
(integer) 1
redis 127.0.0.1:6379> ttl test
(integer) -1
还有redis默认是有16个库存,我们如何进行选择和使用呢:select move
redis 127.0.0.1:6379[19]> select 15
OK
redis 127.0.0.1:6379[15]> select 16
(error) ERR invalid DB index redis 127.0.0.1:6379> keys *
1) "key2"
2) "test"
3) "name"
4) "key1"
redis 127.0.0.1:6379> move key1 1
(integer) 1
redis 127.0.0.1:6379> select 1
OK
redis 127.0.0.1:6379[1]> keys *
1) "test3"
2) "test"
3) "key1"
4) "test2"
5) "myhash"
6) "mylist"
redis 127.0.0.1:6379[1]> select 0
OK
redis 127.0.0.1:6379> keys *
1) "key2"
2) "test"
3) "name"