[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++