Redis安装与基本操作

下载安装包:

1
wget http://download.redis.io/releases/redis-5.0.5.tar.gz

解压:

1
tar -zcvf redis-5.0.5.tar.gz

编译:

1
2
cd redis-5.0.5.tar.gz
make

安装到指定目录:

1
make install PREFIX=/usr/local/redis

redis.conf文件复制到/usr/local/中:

1
cp redis.conf /user/local

配置redis.conf:

1
vim redis.conf

基本配置主要修改三处,其保持默认即可使用:

  • daemonize no改为daemonize yes,以守护进程方式运行
  • bind:127.0.0.1这一句注释掉
  • 找到requirepass,将这一句取消注释,并修改为自己的密码,即requirepass 你的密码

运行服务端:

1
2
cd /usr/local/redis
./bin/redis-server ./redis.conf

查看服务端运行状态:

1
ps aux|grep redis

登录客户端:

1
2
./bin/redis-cli
auth passward

基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
redis-cli --raw
set key value #设置key
get key #获得key,不存在返回nil
del key #删除key
exists key #查询某个key是否存在,1为存在,0为不存在
ttl key #查询有效时间,-1表示永久有效,-2表示过期(会自动删除).因此ttl key返回值为-2表示该值不存在(单位:秒)
pttl key #查询有效时间,-1表示永久有效,-2表示过期(会自动删除).因此ttl key返回值为-2表示该值不存在(单位:毫秒)
expire key seconds #为key设置过期时间(单位:秒)
pexpire key milliseconds #为key设置过期时间(单位:毫秒)
persist key #移除key的过期时间,key将永久有效
keys patten #查询
select 1 #切换到第二个数据库,默认在第零个数据库
rename key newkey #重命名key
move key db #将当前数据库key移动到指定数据库中
type key #查询key的数据类型

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×