[root@jin-10 /usr/local/src/redis-4.0.1]# redis-server /etc/redis.conf [root@jin-10 /usr/local/src/redis-4.0.1]# ps aux|grep redis root 9627 0.2 0.2 145300 2200 ? Ssl 20:59 0:03 redis-server 127.0.0.1:6379 root 9828 0.0 0.0 112724 992 pts/1 R+ 21:28 0:00 grep --color=auto redis
[root@jin-10 /usr/local/src/redis-4.0.1]# cat /var/log/redis.log 9626:C 13 Aug 20:59:17.792 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 9626:C 13 Aug 20:59:17.792 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=9626, just started 9626:C 13 Aug 20:59:17.792 # Configuration loaded 9627:M 13 Aug 20:59:17.795 * Increased maximum number of open files to 10032 (it was originally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 4.0.1 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 9627 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 9627:M 13 Aug 20:59:17.796 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 9627:M 13 Aug 20:59:17.796 # Server initialized 9627:M 13 Aug 20:59:17.822 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 9627:M 13 Aug 20:59:17.873 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 9627:M 13 Aug 20:59:17.873 * Ready to accept connections
而后在当前目录下执行如下两条命令:javascript
[root@jin-10 /usr/local/src/redis-4.0.1]# sysctl vm.overcommit_memory=1 vm.overcommit_memory = 1 [root@jin-10 /usr/local/src/redis-4.0.1]# echo never >/sys/kernel/mm/transparent_hugepage/enabled
[root@jin-10 /usr/local/src/redis-4.0.1]# echo "sysctl vm.overcommit_memory=1" >> /etc/rc.local [root@jin-10 /usr/local/src/redis-4.0.1]# echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >> /etc/rc.local
save 900 1 #表示每15分钟且至少有1个key改变,就触发一次持久化 save 300 10 #表示每5分钟且至少有10个key改变,就触发一次持久化 save 60 10000 #表示每60秒至少有10000个key改变,就触发一次持久 save “” #这样能够禁用rdb持久化 appendonly yes #若是是yes,则开启aof持久化 appendfilename “appendonly.aof” # 指定aof文件名字 appendfsync everysec #指定fsync()调用模式,有三种no(不调用fsync),always(每次写都会调用fsync),everysec(每秒钟调用一次fsync)。第一种最快,第二种数据最安全,但性能会差一些,第三种为折中方案,默认为第三种。
string是最简单的类型,与Memcached同样的类型,一个key对应一个value,其支持的操做与Memcached的操做相似,但功能更丰富。设置能够存二进制的对象。php
[root@jin-10 /usr/local/src/redis-4.0.1]# redis-cli 127.0.0.1:6379> set mykey "test_redis" OK 127.0.0.1:6379> get mykey "test_redis" 127.0.0.1:6379> mset key1 a key2 b key3 c OK 127.0.0.1:6379> mget key1 mykey 1) "a" 2) "test_redis"
127.0.0.1:6379> lpush list1 "jin" (integer) 1 127.0.0.1:6379> lpush list1 "a" (integer) 2 127.0.0.1:6379> lpush list1 "b" (integer) 3 127.0.0.1:6379> lpush list1 "c" (integer) 4 127.0.0.1:6379> lrange list1 0 -1 1) "c" 2) "b" 3) "a" 4) "jin" 127.0.0.1:6379> lrange list1 0 2 1) "c" 2) "b" 3) "a" 127.0.0.1:6379> lpop list1 "c" 127.0.0.1:6379> lrange list1 0 -1 1) "b" 2) "a" 3) "jin"
127.0.0.1:6379> SADD set1 a (integer) 1 127.0.0.1:6379> SADD set1 b (integer) 1 127.0.0.1:6379> SADD set1 c (integer) 1 127.0.0.1:6379> SMEMBERS set1 1) "c" 2) "a" 3) "b" 127.0.0.1:6379> SADD set2 a (integer) 1 127.0.0.1:6379> SADD set2 c (integer) 1 127.0.0.1:6379> SADD set2 d (integer) 1 127.0.0.1:6379> SMEMBERS set2 1) "d" 2) "c" 3) "a" 127.0.0.1:6379> SUNION set1 set2 #交集 1) "b" 2) "c" 3) "a" 4) "d" 127.0.0.1:6379> SINTER set1 set2 #并集 1) "c" 2) "a" 127.0.0.1:6379> SDIFF set1 set2 #差集 1) "b" 127.0.0.1:6379> SREM set1 a #删除集合1中的a (integer) 1 127.0.0.1:6379> SMEMBERS set1 1) "c" 2) "b"
sorted set是有序集合,它比set多了一个权重参数score,使得集合中的元素可以按 score 进行有序排列,好比一个存储全班同窗成绩的 Sorted Sets,其集合 value 能够是同窗的学号,而 score 就能够是其考试得分,这样在数据插入集合的时候,就已经进行了自然的排序。java
127.0.0.1:6379> ZADD set3 2 a (integer) 1 127.0.0.1:6379> ZADD set3 10 "b" (integer) 1 127.0.0.1:6379> ZADD set3 5 "c" (integer) 1 127.0.0.1:6379> ZADD set3 8 "d" (integer) 1 127.0.0.1:6379> ZRANGE set3 0 -1 1) "a" 2) "c" 3) "d" 4) "b" 127.0.0.1:6379> ZREVRANGE set3 0 -1 1) "b" 2) "d" 3) "c" 4) "a"
127.0.0.1:6379> HSET hash1 name jin (integer) 1 127.0.0.1:6379> HSET hash1 age 30 (integer) 1 127.0.0.1:6379> HSET hash1 job Linux (integer) 1 127.0.0.1:6379> HGET hash1 name "jin" 127.0.0.1:6379> HGET hash1 age "30" 127.0.0.1:6379> HGET hash1 job "Linux" 127.0.0.1:6379> HGETALL hash1 1) "name" 2) "jin" 3) "age" 4) "30" 5) "job" 6) "Linux"
127.0.0.1:6379> set key1 linux OK 127.0.0.1:6379> set key2 python OK 127.0.0.1:6379> set key1 php #会覆盖前一个值 OK 127.0.0.1:6379> get key1 "php" 127.0.0.1:6379> SETNX key1 a #值若是存在,则返回0 (integer) 0 127.0.0.1:6379> get key1 "php" 127.0.0.1:6379> SETNX key3 java #值不存在,则返回1 (integer) 1 127.0.0.1:6379> get key3 "java" 127.0.0.1:6379> set key4 C++ OK 127.0.0.1:6379> SETEX key4 10 javascripts #给key4设置过时时间为10s,值为javascripts OK 127.0.0.1:6379> get key4 "javascripts"
127.0.0.1:6379> LPUSH list a #LPUSH从左侧加入一个元素a (integer) 1 127.0.0.1:6379> LPUSH list b (integer) 2 127.0.0.1:6379> LRANGE list 0 -1 1) "b" 2) "a" 127.0.0.1:6379> LPOP list #从左侧取出第一个元素 "b" 127.0.0.1:6379> RPUSH list c #从右侧加入一个元素c (integer) 2 127.0.0.1:6379> LRANGE list 0 -1 1) "a" 2) "c" 127.0.0.1:6379> RPOP list #从右侧个元素 "c" 127.0.0.1:6379> LRANGE list 0 -1 1) "a" 127.0.0.1:6379> LPUSH list a b c d #从左侧加入元素a、b、c、d (integer) 5 127.0.0.1:6379> LINSERT list before b 2 #在list中的b前面插入元素2 (integer) 6 127.0.0.1:6379> LRANGE list 0 -1 1) "d" 2) "c" 3) "2" 4) "b" 5) "a" 6) "a" 127.0.0.1:6379> LSET list 0 1 #把第一个元素修改成1 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "1" 2) "c" 3) "2" 4) "b" 5) "a" 6) "a" 127.0.0.1:6379> LINDEX list 4 #查找list的第5个元素 "a" 127.0.0.1:6379> LLEN list #查看list元素的个数 (integer) 6
127.0.0.1:6379> SADD seta linux python java #往集合seta中插入元素 (integer) 3 127.0.0.1:6379> SMEMBERS seta #查看集合seta的元素 1) "java" 2) "python" 3) "linux" 127.0.0.1:6379> SPOP seta #随机取出seta中的一个元素 "java" 127.0.0.1:6379> SMEMBERS seta 1) "python" 2) "linux" 127.0.0.1:6379> SREM seta python #删除集合seta中的python元素 (integer) 1 127.0.0.1:6379> SMEMBERS seta 1) "linux" 127.0.0.1:6379> SADD seta HTML (integer) 1 127.0.0.1:6379> SMEMBERS seta 1) "linux" 2) "HTML" 127.0.0.1:6379> SADD setb linux c++ HTML python #在集合setb中插入元素 (integer) 4 127.0.0.1:6379> SMEMBERS setb 1) "python" 2) "c++" 3) "linux" 4) "HTML" 127.0.0.1:6379> SADD seta java (integer) 1 127.0.0.1:6379> SMEMBERS seta 1) "java" 2) "linux" 3) "HTML" 127.0.0.1:6379> SMEMBERS setb 1) "python" 2) "c++" 3) "linux" 4) "HTML" 127.0.0.1:6379> SDIFF seta setb #求差集,以seta为标准 1) "java" 127.0.0.1:6379> SDIFFSTORE setc seta setb #将差集存储到setc中 (integer) 1 127.0.0.1:6379> SMEMBERS setc 1) "java" 127.0.0.1:6379> SINTER seta setb #求seta和setb的交集 1) "linux" 2) "HTML" 127.0.0.1:6379> SINTERSTORE setd seta setb #将交集存储到setb中 (integer) 2 127.0.0.1:6379> SMEMBERS setd 1) "linux" 2) "HTML" 127.0.0.1:6379> SUNION seta setb #求seta和setb的并集 1) "HTML" 2) "java" 3) "linux" 4) "c++" 5) "python" 127.0.0.1:6379> SUNIONSTORE sete seta setb #将并集存储到sete中 (integer) 5 127.0.0.1:6379> SMEMBERS sete 1) "HTML" 2) "java" 3) "linux" 4) "c++" 5) "python" 127.0.0.1:6379> SMEMBERS seta 1) "java" 2) "linux" 3) "HTML" #判断一个元素是否在集合中,在则返回1,不在则返回0 127.0.0.1:6379> SISMEMBER seta linux (integer) 1 127.0.0.1:6379> SISMEMBER seta java (integer) 1 127.0.0.1:6379> SISMEMBER seta python (integer) 0 127.0.0.1:6379> SRANDMEMBER seta #随机取出一个元素,但不删除 "HTML" 127.0.0.1:6379> SMEMBERS seta 1) "java" 2) "linux" 3) "HTML"
127.0.0.1:6379> ZADD zseta 2 "b" (integer) 1 127.0.0.1:6379> ZADD zseta 3 "c" (integer) 1 127.0.0.1:6379> ZADD zseta 1 "a" (integer) 1 127.0.0.1:6379> ZRANGE zseta 0 -1 #显示全部元素,按顺序显示 1) "a" 2) "b" 3) "c" 127.0.0.1:6379> ZRANGE zseta 0 -1 withscores #带上分值显示 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 127.0.0.1:6379> ZREM zseta a #删除指定元素 (integer) 1 127.0.0.1:6379> ZRANGE zseta 0 -1 1) "b" 2) "c" 127.0.0.1:6379> ZRANK zseta b #返回元素的索引值,索引值从0开始,按score正向排序 (integer) 0 127.0.0.1:6379> ZCARD zseta #回集合中全部元素的个数 (integer) 2 127.0.0.1:6379> ZCOUNT zseta 1 2 # 返回分值范围1-2的元素个数 (integer) 1 127.0.0.1:6379> ZRANGE zseta 0 -1 1) "b" 2) "c"
127.0.0.1:6379> hset user name jin age 30 job linux (integer) 3 127.0.0.1:6379> hmget user name age job #获取user的全部信息 1) "jin" 2) "30" 3) "linux" 127.0.0.1:6379> hdel user age #删除user的指定filed (integer) 1 127.0.0.1:6379> hkeys user #打印全部的key 1) "name" 2) "job" 127.0.0.1:6379> hvals user #打印全部的values 1) "jin" 2) "linux" 127.0.0.1:6379> hlen user #查看hash有几个filed (integer) 2
127.0.0.1:6379> keys * #取出全部的key 1) "set2" 2) "sete" 3) "setb" 4) "set" 5) "mykey" 6) "list" 7) "key1" 8) "setc" 9) "key3" 10) "set3" 11) "hash1" 12) "set1" 13) "list1" 14) "key2" 15) "seta" 16) "setd" 17) "user" 127.0.0.1:6379> keys key* #模糊查找以key开头的key 1) "key1" 2) "key3" 3) "key2" 127.0.0.1:6379> exists mykey #判断mykey是否存在,存在返回1,不存在返回0 (integer) 1 127.0.0.1:6379> del user #删除一个key,成功返回1,不成功返回0 (integer) 1 127.0.0.1:6379> EXPIRE list 100 #设置list 100s后过时 (integer) 1 127.0.0.1:6379> ttl set1 #查看set1还有多长时间过时,单位是s,当 set1不存在时,返回-2 。当set1存在但没有设置剩余生存时间时,返回-1 ,不然,返回set1的剩余生存时间。 (integer) -1 127.0.0.1:6379> persist list #取消list的过时时间 (integer) 1 127.0.0.1:6379> select 0 #表明选择当前数据库,默认进入0数据库 OK 127.0.0.1:6379> move key2 1 #把key2移动到1数据库 (integer) 1 127.0.0.1:6379> randomkey #随机返回一个key "seta" 127.0.0.1:6379> rename key1 key6 #重命名key OK 127.0.0.1:6379> type key6 #返回键的类型 string
dbsize #返回当前数据库中key的数目 info #返回redis数据库状态信息 flushdb #清空当前数据库中全部的键 flushall #清空全部数据库中的全部的key bgsave #保存数据到 rdb文件中,在后台运行 save #做用同上,可是在前台运行 config get * #获取全部配置参数 config get dir #获取配置参数 config set dir #更改配置参数 数据恢复:首先定义或者肯定dir目录和dbfilename,而后把备份的rdb文件放到dir目录下面,重启redis服务便可恢复数据
编辑配置文件/etc/redis.conf
bind能够设置多个IP,用空格格开
设置监听端口port
设置密码:equirepass 123456>com
将config命令更名:ename-command CONFIG jin
禁掉config命令: rename-command CONFIG ""python
针对慢查询日志,能够设置两个参数,一个是执行时长,单位是微秒,另外一个是慢查询日志的长度。当一个新的命令被写入日志时,最老的一条会从命令日志队列中被移除。linux
此两处的数量能够根据本身需求修改。更改后需重启redisc++