Redis Remote Dictionary Server(远程字典服务器)-www.redis.cn中文网站
高性能的分布式内存数据库,支持数据持久化(可以把内存保存到硬盘),list,hash,set,zset等数据类型和master-slave模式数据备份
Redis服务器搭建
安装源码包redis-4.0.8.tar.gz(基于c语言编译,需要先安装gcc)
[root@mysql50 ~]# yum -y install gcc
[root@mysql50 ~]# tar -zxf redis-4.0.8.tar.gz
[root@mysql50 ~]# cd redis-4.0.8/
[root@mysql50 redis-4.0.8]# make
cd src && make all
make[1]: 进入目录“/root/redis-4.0.8/src”
CC Makefile.dep
....
INSTALL redis-check-rdb
INSTALL redis-check-aof
Hint: It's a good idea to run 'make test' ;)
make[1]: 离开目录“/root/redis-4.0.8/src”
[root@mysql50 redis-4.0.8]# make install
cd src && make install
...
INSTALL install
INSTALL install
INSTALL install
make[1]: 离开目录“/root/redis-4.0.8/src”
初始化配置 (端口,主配置文件,日志文件,数据库目录,启动服务程序,命令行链接工具)
- Port : 6379
- Config file : /etc/redis/6379.conf
- Log file : /var/log/redis_6379.log
- Data dir : /var/lib/redis/6379
- Executable : /usr/local/bin/redis-server
- Cli Executable : /usr/local/bin/redis-cli
[root@mysql50 redis-4.0.8]# ./utils/install_server.sh //初始化
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379 //服务启动脚本
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server... //服务已经开启
Installation successful!
查看服务状态信息及启动,停止
[root@mysql50 redis-4.0.8]# netstat -pantul | grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 5025/redis-server 1
[root@mysql50 redis-4.0.8]# ps -C redis-server
PID TTY TIME CMD
5025 ? 00:00:00 redis-server
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (5025)
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 stop
Stopping ...
Redis stopped
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 status
cat: /var/run/redis_6379.pid: 没有那个文件或目录
Redis is running ()
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@mysql50 redis-4.0.8]# /etc/init.d/redis_6379 status
Redis is running (5421)
链接redis
[root@mysql50 redis-4.0.8]# redis-cli
127.0.0.1:6379> ping
PONG //返回值为PONG正常
127.0.0.1:6379> exit
数据管理命令
[root@mysql50 redis-4.0.8]# redis-cli
127.0.0.1:6379> set key value [EX seconds] [PX milliseconds] [NX|XX] //存储
127.0.0.1:6379[1]> set test 123
OK
127.0.0.1:6379[1]> get key //获取键值
127.0.0.1:6379[1]> select index //数据库编号0-15 切换数据库
127.0.0.1:6379[1]> keys pattern //* 打印所有 te?? 打印指定变量,?通配符
127.0.0.1:6379[1]> EXISTS key [key ...] //检查是否存在
127.0.0.1:6379[1]> ttl key //查看生存时间
127.0.0.1:6379[1]> type key //查看类型
127.0.0.1:6379[1]> move key db //移动记录到某个库
127.0.0.1:6379> expire key seconds //设置生存时间
127.0.0.1:6379> del key [key ...] //删除键值
127.0.0.1:6379> fullshall //删除所有
127.0.0.1:6379> flushdb //删除当前库的内容
127.0.0.1:6379> save //保存
127.0.0.1:6379> SHUTDOWN [NOSAVE|SAVE] //关闭服务,默认save
配置选项
[root@mysql50 ~]# vim /etc/redis/6379.conf //进入打开行号
...
12 # 1k => 1000 bytes //数据单位
13 # 1kb => 1024 bytes
14 # 1m => 1000000 bytes
15 # 1mb => 1024*1024 bytes
16 # 1g => 1000000000 bytes
17 # 1gb => 1024*1024*1024 bytes
...
################### NETWORK ###############
70 bind 127.0.0.1 //IP地址
93 port 6379 //端口
102 tcp-backlog 511 //tcp链接总数
114 timeout 0 //链接超时时间
131 tcp-keepalive 300 //长链接时间
#################### GENERAL #################
137 daemonize yes //守护进程方式运行
172 logfile /var/log/redis_6379.log //pid文件
187 databases 16 //数据库个数
#################### SNAPSHOTTING ###########
264 dir /var/lib/redis/6379 //数据库目录
################### CLIENTS ################
533 # maxclients 10000 //并发链接数量
################## MEMORY MANAGEMENT ###########
560 # maxmemory <bytes> //最大内存
565 # volatile-lru -> Evict using approximated LRU among the keys with an expir e set. //最近最少使用(针对设置了ttl的key)
566 # allkeys-lru -> Evict any key using approximated LRU. //删除最少使用的key
569 # volatile-random -> Remove a random key among the ones with an expire set. //在设置了ttl的key里随机删除
570 # allkeys-random -> Remove a random key, any key. //随机删除key
571 # volatile-ttl -> Remove the key with the nearest expire time (minor TTL) //移除最近过期的key
572 # noeviction -> Don't evict anything, just return an error on write operati ons. //不删除,写满时报错
591 # maxmemory-policy noeviction //定义使用策略
602 # maxmemory-samples 5 //选取模板数据的个数(针对lru和ttl策略
修改服务使用的IP地址,端口,及密码
常见问题:
[root@mysql50 ~]# /etc/init.d/redis_6379 start //非正常情况下停止此服务,不会自动删除进程
/var/run/redis_6379.pid exists, process is already running or crashed
[root@mysql50 ~]#rm -rf /var/run/redis_6379.pid //删除这个文件,即可重新启动
[root@mysql50 ~]# vim /etc/redis/6379.conf
70 bind 192.168.4.50
...
93 port 6350
...
501 requirepass 123456 //修改密码
[root@mysql50 ~]#/etc/init.d/redis_6379 start
[root@mysql50 ~]# ss -antul | grep 6350
tcp LISTEN 0 128 192
[root@mysql50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350>
[root@mysql50 ~]# redis-cli -h 192.168.4.50 -p 6350
192.168.4.50:6350> auth 123456 //里面输入密码也可以
[root@mysql50 ~]# /etc/init.d/redis_6379 stop //这种方式停不下来
Stopping ...
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Waiting for Redis to shutdown ...
....
[root@mysql50 ~]# vim /etc/init.d/redis_6379 //这个方式需要改以下内容,才可以调用脚本 执行stop命令
REDISPORT="6350 -h 192.168.4.50 -a 123456 " //因为stop会调用这个函数,所以如果不改端口,密码,ip 这里不用改。默认就是6379端口号
....
stop)
...
$CLIEXEC -p $REDISPORT shutdown
...
[root@mysql50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456 shutdown //可以这样停
[root@mysql50 ~]# ss -antul | grep 6350
[root@mysql50 ~]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> shutdown //或者这样
Redis数据类型
string字符串 | list列表 |
hash表 | |
---|---|---|---|
操 作 |
set key value[ex seconds] [px milliseconds] [nx | xx] 设置key和值,过期时间可以使用ex 秒 px 毫秒 不存在则赋值nx 存在赋值xx |
是一个字符队列,先进后出, 一个key多个值 |
一个string类型的field和value的映射表 一个key可对应多个field, 一个field对应一个value 将一个对应存储为hash类型,较于每个字段都存储乘string类型更能节省内存 |
mset key value [key value...] 设置多个key及值,空格分隔,具有原子性 |
lrange key start stop 从开始位置读取key的值到stop结束 lrange list 0 -1 开始到结束(开始为0,最后存的,结束为-1 ,开始存的) |
||
setrange key offset value 从偏移量开始复写key的特定位的值,起始0 |
hset key field value 将hash表中field值设置为value |
||
strlen key 统计字串长度 | llen key 返回列表key的长度 | hget key field 获取hash表中field的值 |
|
append key value 存在则追加,不存在则创建,返回key长度 |
lpop key 移除并返回列表头元素数据,存在则nil |
||
setbit key offset values 对key所存储字串 设置或清楚特定偏移量上的位(bit) value值可以为1或0 offset为0~2^32之间 key不存在,则创建新key |
lset key index value 将可以中index位置的值修改为value |
hmset key field value [field value ...] 同时给hash表中的多个field赋值 |
|
bitcount key 统计字串中被设置为1的比特位数量 |
lindex key index 返回列表中第index个值 |
hkeys key 返回hash表中多个field的值 |
|
decr key 将key的值减1,key不存在则先初始化为0,再减1 |
rpush key value [value ...] 将value插入到key的末尾 |
hmget key field [field ...] 返回hash表中多个field的值 |
|
decrby key decrement 将key中的值,减去decrement |
rpop key 删除并返回key末尾的值 |
hgetall key 返回hash表中所有key名和对应值列表 |
|
incr key 将key的值加1,不存在则初始化0后再加1 |
lpush key value [value...] 将一/多个值value插入到列表key表头 不存在,则创建key |
hvals key 返回hash表中所有key的值 |
|
incrby key increment 将key的值增加increment |
hdel key field [field ...] 删除hash表中多个field的值, 不存在则忽略 |
||
incrbyfloat key increment 为key中所储存的值加上浮点数 increment |
|||
get key 返回key存储的值,不存在则返回null |
|||
getrange key start end 返回字串值中的子字串,范围为start和end 负偏移从末尾开始数,-1表示最后一个,以此类推 |
|||
mget key [key ...] 获取一个多个key值,空格分隔,有原子性 |
string字符串案例
[root@mysql50 6379]# redis-cli -h 192.168.4.50 -p 6350 -a 123456
192.168.4.50:6350> set a 123 ex 20 xx //不存在用xx赋值为错
(nil)
192.168.4.50:6350> set a 123 ex 20 nx //不存在用nx才可以赋值,生存时间为20s
OK
192.168.4.50:6350> ttl a //统计剩余时间
(integer) 9
192.168.4.50:6350> ttl a
(integer) 7
192.168.4.50:6350> set hello ninhao
OK
192.168.4.50:6350> SETRANGE hello 3 , //从偏移量3开始,只给3重新赋值为“,” ,初始默认为0
(integer) 6
192.168.4.50:6350> get hello //查看值
"nin,ao"
192.168.4.50:6350> STRLEN hello //统计字符长度
(integer) 6
192.168.4.50:6350> append hello beijing //追加hello的值beijing
(integer) 13 //返回长度
192.168.4.50:6350> get hello
"nin,aobeijing"
192.168.4.50:6350> append like xihuan //不存在key 创建一个新的key值
(integer) 6
192.168.4.50:6350> get like
"xihuan"
192.168.4.50:6350> mset e 1 f 2 g 3 //设置多个key和值 以空格分开
OK
192.168.4.50:6350> mget e f g //取多个key的值
1) "1"
2) "2"
3) "3"
192.168.4.50:6350> set b b
OK
192.168.4.50:6350> BITCOUNT b //统计b的bit为1的个数 b对应的ascii码为98 二进制为0110 0010
(integer) 3
192.168.4.50:6350> get b
"b"
192.168.4.50:6350> setbit b 7 1 //设置b的第7位为1
(integer) 0 //这个在这里代表之前的位值为0
192.168.4.50:6350> get b //可以看到b的值变成c了 因为c的二进制为01100011 也就是b+1
"c"
192.168.4.50:6350> setbit b 7 0 //重新设置为0
(integer) 1
192.168.4.50:6350> get b //值又恢复了b
"b"
192.168.4.50:6350> setbit nin 2 1 //设置nin的第二位为1
(integer) 0
192.168.4.50:6350> BITCOUNT nin
(integer) 1
192.168.4.50:6350> setbit nin 0 1 //设置nin的第0位为1
(integer) 0
192.168.4.50:6350> BITCOUNT nin //统计nin的bit为1的比特位是两个
(integer) 2
192.168.4.50:6350> decr a //没有a 则初始化为0 在减1
(integer) -1
192.168.4.50:6350> set test 9
OK
192.168.4.50:6350> decr test //将key的值减1
(integer) 8
192.168.4.50:6350> decrby test 2 //将key的值减2
(integer) 6
192.168.4.50:6350> incr test //将key值加1
(integer) 7
192.168.4.50:6350> incrby test 3 //将key的值加3
(integer) 10
192.168.4.50:6350> incr d //d值不存在,初始化为0 加1
(integer) 1
192.168.4.50:6350> incrbyfloat d 0.55 //将d的值添加小数0.55
"1.55"
192.168.4.50:6350> set chenggong success
OK
192.168.4.50:6350> getrange chenggong 0 1 //子串截取从0开始 到1结束
"su"
192.168.4.50:6350> getrange chenggong 0 -1 //子串截取从开始到结束
"success"
192.168.4.50:6350> getrange chenggong -4 -1 //子串截取从倒数第四位到倒数第一位
"cess"
list列表案例
192.168.4.50:6350> lpush ai wo xi huan ni //定以key的值列表
(integer) 4
192.168.4.50:6350> lrange ai 0 -1 //后存的为0 先存的在最下面,开始到结束
1) "ni"
2) "huan"
3) "xi"
4) "wo"
192.168.4.50:6350> lrange ai 0 1 //取0和1位
1) "ni"
2) "huan"
192.168.4.50:6350> lrange ai -3 -1 //按存的顺序取反。
1) "huan"
2) "xi"
3) "wo"
192.168.4.50:6350> lpop ai //移除ni
"ni"
192.168.4.50:6350> lpop ai
"huan"
192.168.4.50:6350> lrange ai 0 -1 //查看列表
1) "xi"
2) "wo"
192.168.4.50:6350> lpop ii //不存在
(nil)
192.168.4.50:6350> rpush ai ya a //在ai的末尾依次插入值ya a
(integer) 6
192.168.4.50:6350> lrange ai 0 -1 //显示列表的值
1) "ni"
2) "huan"
3) "xi"
4) "wo"
5) "ya"
6) "a"
192.168.4.50:6350> rpop ai //移除末尾值
"a"
192.168.4.50:6350> lrange ai 0 -1
1) "ni"
2) "huan"
3) "xi"
4) "wo"
5) "ya"
192.168.4.50:6350> lindex ai 3 //列出列表的第三位
"wo"
192.168.4.50:6350> lset ai -1 hello //将-1位(末尾第一位)改成hello
OK
192.168.4.50:6350> lrange ai 0 -1
1) "ni"
2) "huan"
3) "xi"
4) "wo"
5) "hello"
hash表案例
168.4.50:6350> hset site baidu www.baidu.com //定义键对应的域对应的值(key field value)
(integer) 1
192.168.4.50:6350> hset site tiammao www.tmall.com
(integer) 1
192.168.4.50:6350> hset site taobao www.taobao.com
(integer) 1
192.168.4.50:6350> hget site taobao //获取键值site对应的域淘宝对应的值
"www.taobao.com"
192.168.4.50:6350> hget site baidu
"www.baidu.com"
192.168.4.50:6350> hmset hobby book sanguoyanyi ball football //同时给多个域field赋值
OK
192.168.4.50:6350> hmget hobby book ball //返回多个field的值
1) "sanguoyanyi"
2) "football"
192.168.4.50:6350> hmget site taobao tiammao baidu
1) "www.taobao.com"
2) "www.tmall.com"
3) "www.baidu.com"
192.168.4.50:6350> hvals site //返回所有site的值
1) "www.baidu.com"
2) "www.tmall.com"
3) "www.taobao.com"
192.168.4.50:6350> hgetall site //返回所有的域和值
1) "baidu"
2) "www.baidu.com"
3) "tiammao"
4) "www.tmall.com"
5) "taobao"
6) "www.taobao.com"
192.168.4.50:6350> hkeys hobby 返回域
1) "book"
2) "ball"
192.168.4.50:6350> hkeys site
1) "baidu"
2) "tiammao"
3) "taobao"
192.168.4.50:6350> hdel hobby book ball 删除多个field的值
(integer) 2
192.168.4.50:6350> hgetall hobby
(empty list or set)