启动VMware虚拟机,并在Xshell 7连接上虚拟机后
查看Redis进程是否自启动:ps -ef | grep redis
自启动成功结果图片
查看redis运行状态:service redis status
进入指定目录下:cd /home/redis-6.2.6/bin
运行redis文件进入redis: ./redis-cli
一、String类型
#设置String类型数据(添加)
127.0.0.1:6379> set name xiaolin
OK
#查看String类型值(获取)
127.0.0.1:6379> get name
"xiaolin"
#若key存在就不设置,不存在就设置(添加),当key存在的时候,返回0,表未设置成功
127.0.0.1:6379> setnx name xiaodong
(integer) 0
#为键和值设置生存时间,模板:setex key time value,其中time为生存时间,以秒为单位
127.0.0.1:6379> setex address 5 LP
OK
127.0.0.1:6379> get address
(nil)
#替换从指定索引处起的字符,从键值的value的索引0开始,替换(4+1、包括4+1)索引地址后的字符
127.0.0.1:6379> setrange name 4 dragon
(integer) 10
127.0.0.1:6379> get name
"xiaodragon"
#返回指定索引处的字符
127.0.0.1:6379> getrange name 4 9
"dragon"
#一次性设置多个键值对和一次性获取多个key值(m表多个)
127.0.0.1:6379> mset name xuebao age 2010 hostrole wenzhang
OK
127.0.0.1:6379> mget name age hostrole
1) "xuebao"
2) "2010"
3) "wenzhang"
#同时设置多个键值,存在就不设置,不存在就新增key值。要么都设置成功,要么都设置失败
127.0.0.1:6379> msetnx name xue sex male number 12
(integer) 0
127.0.0.1:6379> mget name age hostrole sex number
1) "xuebao"
2) "2010"
3) "wenzhang"
4) (nil)
5) (nil)
127.0.0.1:6379> msetnx name1 xue sex male number 12
(integer) 1
127.0.0.1:6379> mget name name1 age hostrole sex number
1) "xuebao"
2) "xue"
3) "2010"
4) "wenzhang"
5) "male"
6) "12"
#返回原始的值,设置新值
127.0.0.1:6379> get name
"xuebao"
127.0.0.1:6379> getset name xuhu
"xuebao"
127.0.0.1:6379> get name
"xuhu"
# value自增
127.0.0.1:6379> get number
"12"
127.0.0.1:6379> incr number
(integer) 13
# value增加指定的值(助记:number by 12 number被增加12)
127.0.0.1:6379> incr number
(integer) 13
127.0.0.1:6379> incrby number 12
(integer) 25
# value自减
127.0.0.1:6379> incrby number 12
(integer) 25
127.0.0.1:6379> decr number
(integer) 24
# value减少指定的值
127.0.0.1:6379> decr number
(integer) 24
127.0.0.1:6379> decrby number 14
(integer) 10
# value后面添加字符串
127.0.0.1:6379> append name xuebaotezhandui
(integer) 19
127.0.0.1:6379> get name
"xuhuxuebaotezhandui"
# 获取value字符串长度
127.0.0.1:6379> strlen name
(integer) 19