redis安装及简单使用

时间:2022-09-16 05:20:10
ridis安装
[root@ansible
-client downloads]# tar -xf redis-3.2.5.tar.gz -C /usr/local/src
[root@ansible
-client downloads]# cd /usr/local/src
[root@ansible
-client src]# ln -s redis-3.2.5/ redis
[root@ansible
-client src]# make MALLOC=libc
[root@ansible
-client redis]# make PREFIX=/usr/local/redis install

[root@ansible
-client src]# cp /usr/local/src/redis-3.2.5/utils/redis_init_script /etc/rc.d/init.d/redis

[root@ansible
-client src]#vim /etc/rc.d/init.d/redis
=========================================
# chkconfig:
2345 90 10
# description: Redis
is a persistent key-value database

EXEC
=/usr/local/redis/bin/redis-server
CLIEXEC
=/usr/local/redis/bin/redis-cli
==========================================



[root@ansible
-client src]#mkdir /etc/redis
[root@ansible
-client src]#cp /usr/local/src/redis-3.2.5/redis.conf /etc/redis/6379.conf

[root@ansible
-client mysql]# echo 'export PATH=/usr/local/redis/bin:$PATH' >>/etc/profile
[root@ansible
-client mysql]# source /etc/profile
[root@ansible
-client mysql]# chkconfig --add redis

启动
[root@ansible
-client ~]# redis-server
停止
[root@ansible
-client ~]# redis-cli -h 127.0.0.1 -p 6379 shutdown



登录
[root@ansible
-client ~]# redis-cli
127.0.0.1:6379> ping
PONG

关闭防火墙:
[root@ansible
-client ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

 

[键(key)]
set yang redis
get yang
del yang #删除
exists yang #查看是否存在

 

[字符串(string)]
set yang redis 设置指定 key 的值
get yang #获取指定 key 的值。

 


[哈希(hash)]
127.0.0.1:6379> hmset yanglei name "redis" age 18 verson 2.3
OK
127.0.0.1:6379> hgetall yanglei #查看全部
1) "name"
2) "redis"
3) "age"
4) "18"
5) "verson"
6) "2.3"


127.0.0.1:6379> hkeys yanglei #获取哈希表中的字段
1) "name"
2) "age"
3) "verson"


127.0.0.1:6379> hvals yanglei #获取哈希表中的值
1) "redis"
2) "18"
3) "2.3"


127.0.0.1:6379> hset yanglei age 19 #将哈希表key中的值 age值改为19
(integer) 0
127.0.0.1:6379> hvals yanglei
1) "redis"
2) "19"
3) "2.3"
127.0.0.1:6379>

 

 

[列表 list]
将2个值插入名为 yyy 的列表中
127.0.0.1:6379> lpush yyy redis
(integer)
1
127.0.0.1:6379> lpush yyy 222
(integer)
2
127.0.0.1:6379> lrange yyy 0 100
1) "222"
2) "redis"
127.0.0.1:6379>

 


Redis 集合(Set)
127.0.0.1:6379> sadd yangyang redis
(integer) 1
127.0.0.1:6379> sadd yangyang age
(integer) 1
127.0.0.1:6379> sadd yangyang mysql
(integer) 1
127.0.0.1:6379> smembers yangyang
1) "mysql"
2) "age"
3) "redis"


127.0.0.1:6379> spop yangyang
"redis"
127.0.0.1:6379> smembers yangyang
1) "mysql"
2) "age"


127.0.0.1:6379> sunion yangyang #返回所有给定集合的并集
1) "mysql"
2) "age"



 

Redis 有序集合(sorted set)
redis
127.0.0.1:6379> ZADD runoobkey 1 redis
(integer)
1
redis
127.0.0.1:6379> ZADD runoobkey 2 mongodb
(integer)
1
redis
127.0.0.1:6379> ZADD runoobkey 3 mysql
(integer)
1
redis
127.0.0.1:6379> ZADD runoobkey 3 mysql
(integer)
0
redis
127.0.0.1:6379> ZADD runoobkey 4 mysql
(integer)
0
redis
127.0.0.1:6379> ZRANGE runoobkey 0 10 WITHSCORES

1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"