Redis启动
启动错误
The Windows version of Redis allocates a large memory mapped file for sharing
the heap with the forked process used in persistence operations. This file
will be created in the current working directory or the directory specified by
the 'heapdir' directive in the .conf file. Windows is reporting that there is
insufficient disk space available for this file (Windows error 0x70).
You may fix this problem by either reducing the size of the Redis heap with
the --maxheap flag, or by moving the heap file to a local drive with sufficient
space.
Please see the documentation included with the binary distributions for more
details on the --maxheap and --heapdir flags.
Redis can not continue. Exiting.
redis.conf配置文件中设置
maxheap 1014000000
启动配置
配置文件:redis.conf
设置模式
vi redis.conf
daemonize yes #默认为no
端口配置:
默认6379
配置数据库数量:
配置内存大小:
会生成一个和内存大小一样的文件。
maxmemory 200mb #在真实环境必须部署,否则物理内存会被耗尽。一般配置200mb/500mb/1gb/2gb。可以分散到多台服务器,和其它业务共享服务器,以充分利用资源。同时因为分散,防止单点故障,造成大量缓存失效。
maxmemory 200mb
启动
redis-server #默认找redis.conf配置文件
redis-server & #上面ctrl+c中断reis会退出,这个不会
redis-server redis6380.conf #指定配置文件,这样可以启动多个实例
注意两种启动方式的差异
ps -ef|grep redis
root 3511 1 0 16:29 ? 00:00:01 redis-server *:6379
root 3515 1 0 16:29 ? 00:00:01 redis-server 127.0.0.1:6380
注释掉redis.conf配置文件中的48行;# bind 127.0.0.1 ::1即可
版本
redis-server –v
Redis server v=3.2.5 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=e6c4c3113548f6b0
redis-cli –v
设置访问密码
默认没有密码,可以随意访问。redis速度相当快,在一个较好的服务器下,外部用户每秒可以进行15w此的密码尝试,这意味着必须指定非常强大的密码来防止暴力破解。
如果要加,打开redis.conf的
requirepass 123456 #480行,设置请求密码,这样访问时都需要先登录
127.0.0.1:6379> auth 123456 #客户端访问方式
jedis.auth(“123456”); #jedis访问方式
详细信息
redis-cli
127.0.0.1:6379> info #查看当前redis节点的详细配置信息
Redis命令
redis-cli的使用之发送命令
默认连接:IP 127.0.0.1 端口 6379
redis-cli
指定IP端口:
redis-cli –h 127.0.0.1 –p 6379
Redis提供了PING-PONG机制,测试与客户端和服务器链接是否正常
redis-cli ping
或
redis-cli
redis 127.0.0.1:6379>ping
PONG
redis-cli的使用之命令返回值
状态回复(最简单的回复-redis提供的测试命令)
redis>PING
PONG
127.0.0.1:6379>SET test 123
OK
错误回复(以error开头,后面跟着错误信息)
127.0.0.1:6379>TEST
(error) ERR unknown command 'TEST'
整数回复
127.0.0.1:6379>INCR test_incr
(integer) 1
字符串回复(最长久的一种回复,双引号包裹)
127.0.0.1:6379>get test
“123”
多行字符串回复
127.0.0.1:6379>KEYS *
1) "test_incr"
2) "test"
exist(退出)
退出
127.0.0.1:6379> exit
shutdown(关闭)
关闭
127.0.0.1:6379> shutdown
keys
字符串类型是redis中最基本的数据类型,它能存储任何形式的字符串,包括二进制数据。可以存储JSON化的对象、字节数组等。一个字符串类型键允许存储的数据最大容量是512MB。
赋值与取值:
SET key value
GET key
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set test 123
OK
127.0.0.1:6379> set test1 ab
OK
127.0.0.1:6379> keys *
1) "test1"
2) "test"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test1
"abc"
127.0.0.1:6379> get test2
(nil)
127.0.0.1:6379>
select
redis默认支持16个数据库,对外都是以一个从0开始的递增数字命名,可以通过参数database来修改默认数据库个数。客户端连接redis服务后会自动选择0号数据库,可以通过select命令更换数据库,例如选择1号数据库:
127.0.0.1:6379>SELECT 1
OK
127.0.0.1:6379>GET test
(nil)
说明:
Redis不支持自定义数据库名称。
Redis不支持为每个数据库设置访问密码。
Redis的多个数据库之间不是安全隔离的,FLUSHALL命令会清空所有数据库的数据。
keys
获取符合规则的建名列表。
KEYS *
keys test[_]*
keys t[a-d]
说明:
? 匹配一个字符
* 匹配任意个(包括0个)字符
[] 匹配括号间的任一字符,可以使用“-“表示范围。如a[a-d]匹配ab/ac/ad
\x 匹配字符x,用于转义符合,如果要匹配“?“就需要使用\?
clear
清除屏幕内容
exists
判断一个键是否存在。
如果键存在则返回整数类型1,否则返回0。
127.0.0.1:6379> keys *
1) "test_incr"
2) "test"
127.0.0.1:6379> exists test
(integer) 1
127.0.0.1:6379> exists test1
(integer) 0
127.0.0.1:6379>
del
删除键,可以删除一个或者多个键,多个键用空格隔开,返回值是删除的键的个数。
127.0.0.1:6379> del test
(integer) 1
127.0.0.1:6379> del test
(integer) 0
127.0.0.1:6379> del test test_incr
(integer) 1
127.0.0.1:6379>
type
获得键值的数据类型,返回值可能是string(字符串)、hash(散列类型)、list(列表类型)、set(集合类型)、zset(有序集合类型)。
127.0.0.1:6379> keys *
1) "test1"
2) "test"
127.0.0.1:6379> type test
string
127.0.0.1:6379> type test1
string
help
127.0.0.1:6379> help
redis-cli 2.8.19
Type: "help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
127.0.0.1:6379> help type
TYPE key
summary: Determine the type stored at key
since: 1.0.0
group: generic
官网:http://www.redis.io帮助
flushall
清空所有数据库。
127.0.0.1:6379> FLUSHALL
OK
flushdb
清空当前数据库。
127.0.0.1:6379> FLUSHDB
OK
Redis数据类型之字符串
存放的字符串为二进制是安全的。字符串长度支持到512M。
incr/incrby
递增数字INCR key
当存储的字符串是整数时,redis提供了一个实用的命令INCR,其作用是让当前键值递增,并返回递增后的值。
127.0.0.1:6379> keys *
1) "test1"
2) "test"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> get test1
"abc"
127.0.0.1:6379> get test2
(nil)
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> keys *
1) "num"
2) "test1"
3) "test"
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3
127.0.0.1:6379>
从上面例子可以看出,如果num不存在,则自动会创建,如果存在自动+1。
指定增长系数
语法:INCRBY key increment
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> incr num
(integer) 3
127.0.0.1:6379> incrby num 2
(integer) 5
127.0.0.1:6379> incrby num 2
(integer) 7
127.0.0.1:6379> incrby num 2
(integer) 9
127.0.0.1:6379> incr num
(integer) 10
127.0.0.1:6379>
decr/decrby
减少指定的整数
DECR key
DECRBY key decrement
127.0.0.1:6379> incr num
(integer) 10
127.0.0.1:6379> decr num
(integer) 9
127.0.0.1:6379> decrby num 3
incrbyfloat
整数时,第一次加可以得到正确结果,浮点数后再加浮点就会出现精度问题。
原来下面的例子2.8.7注意在新版本中已经修正了这个浮点精度问题。3.0.7
INCRBYFLOAT key decrement
127.0.0.1:6379> set num
(integer) 131
127.0.0.1:6379> incrbyfloat num 0.7
“131.7”
127.0.0.1:6379> incrbyfloat num 0.7
“132.3999999999999999”
append
向尾部追加值。如果键不存在则创建该键,其值为写的value,即相当于SET key value。返回值是追加后字符串的总长度。
语法:APPEND key value
127.0.0.1:6379> keys *
1) "num"
2) "test1"
3) "test"
127.0.0.1:6379> get test
"123"
127.0.0.1:6379> append test "abc"
(integer) 6
127.0.0.1:6379> get test
"123abc"
127.0.0.1:6379>
strlen
字符串长度,返回数据的长度,如果键不存在则返回0。注意,如果键值为空串,返回也是0。
语法:STRLEN key
127.0.0.1:6379> get test
"123abc"
127.0.0.1:6379> strlen test
(integer) 6
127.0.0.1:6379> strlen tnt
(integer) 0
127.0.0.1:6379> set tnt ""
OK
127.0.0.1:6379> strlen tnt
(integer) 0
127.0.0.1:6379> exists tnt
(integer) 1
127.0.0.1:6379>
mset/mget
同时设置/获取多个键值
语法:MSET key value [key value …]
MGET key [key …]
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> mset a 1 b 2 c 3
OK
127.0.0.1:6379> mget a b c
1) "1"
2) "2"
3) "3"
127.0.0.1:6379>