[toc]linux
[root@xavi ~]# cd /usr/local/src/redis-4.0.9 [root@xavi redis-4.0.9]# redis-cli 127.0.0.1:6379> set key1 xavi OK 127.0.0.1:6379> set key2 xavilinux OK 127.0.0.1:6379> set key1 linux OK 127.0.0.1:6379> get key1 "linux"
127.0.0.1:6379> SETNX key1 aaa (integer) 0 127.0.0.1:6379> get key1 "linux" 127.0.0.1:6379> SETNX key3 aaa (integer) 1 127.0.0.1:6379> get key3 "aaa"
127.0.0.1:6379> set key3 aaa ex 10 OK 127.0.0.1:6379> get key3 (nil) 127.0.0.1:6379> set key3 aaa ex 100 OK 127.0.0.1:6379> get key3 "aaa" set key1 aming//第二次賦值會覆蓋 setnx key2 aaa //返回1 若是key2不存在直接建立key setnx key2 bbb //返回0,若是key2存在,返回0 setex key3 10 1 //給key3設置過時時間爲10s,值爲1,若key已經存在,會覆蓋新的值
127.0.0.1:6379> LPUSH list aaa (integer) 1 127.0.0.1:6379> LPush list bbb (integer) 2 127.0.0.1:6379> LPUSH list ccc (integer) 3 127.0.0.1:6379> LPUSH list ddd (integer) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "ddd" 2) "ccc" 3) "bbb" 4) "aaa" 127.0.0.1:6379> RPOP list "aaa" 127.0.0.1:6379> lpop list "ddd"
[ ] linsert lista before 2 3 //aaa 333//在aaa的前面插入一個元素爲333git
舉例:redis
127.0.0.1:6379> linsert list before aaa bbb (integer) 3 127.0.0.1:6379> linsert list after aaa 111 (integer) 4 127.0.0.1:6379> lrange list 0 -1 1) "bbb" 2) "aaa" 3) "111" 4) "ccc" 127.0.0.1:6379> lindex list 0 //LINDEX 查看元素的值 "bbb" 127.0.0.1:6379> llen list // LINDEX 查看元素的長度 (integer) 4
127.0.0.1:6379> sadd setb 222 (integer) 1 127.0.0.1:6379> sadd setb aaa (integer) 1 127.0.0.1:6379> sadd seta 111 (integer) 1 127.0.0.1:6379> smembers seta 1) "bbb" 2) "111" 127.0.0.1:6379> smembers setb 1) "aaa" 2) "222" 127.0.0.1:6379> sdiff seta setb 1) "bbb" 2) "111"
127.0.0.1:6379> ZADD zseta 11 123 (integer) 1 127.0.0.1:6379> ZADD zseta 0 1ab (integer) 1 127.0.0.1:6379> zrange zseta 0 -1 1) "1ab" 2) "123" 127.0.0.1:6379> ZADD zseta 14 dddd (integer) 1 127.0.0.1:6379> zadd zseta 16 121211 (integer) 1 127.0.0.1:6379> zrange zseta 0 -1 1) "1ab" 2) "123" 3) "dddd" 4) "121211" //顯示全部元素,按順序顯示 127.0.0.1:6379> ZREM zseta 123 //刪除指定元素 (integer) 1 127.0.0.1:6379> ZRANGE zseta 0 -1 1) "1ab" 2) "dddd" 3) "121211"
127.0.0.1:6379> zrange zseta 0 -1 1) "1ab" 2) "123" 3) "dddd" 4) "121211" 127.0.0.1:6379> zrank zseta dddd (integer) 2 //返回元素的索引值,索引值從0開始,按score正向排序 127.0.0.1:6379> ZREVRANK zseta 121211 (integer) 0 //按score反向排序,獲取數據的值
127.0.0.1:6379> ZCARD zseta (integer) 4 ZCOUNT 統計範圍內集合元素的個數 127.0.0.1:6379> ZCOUNT zseta 10 20 (integer) 3 //返回分值範圍10-20的元素個數 (integer) 2
127.0.0.1:6379> ZRANGEBYSCORE zseta 10 20 1) "123" 2) "dddd" 3) "121211"
127.0.0.1:6379> ZREMRANGEBYSCORE zseta 10 15 (integer) 2 127.0.0.1:6379> ZRANGE zseta 0 -1 1) "1ab" 2) "121211"
127.0.0.1:6379> ZRANGE zseta 0 -1 1) "asdaqwe" 2) "123" 3) "234" 4) "345" 5) "asdaa" 6) "asasdz" 127.0.0.1:6379> ZREMRANGEBYRANK zseta 1 10 (integer) 5 127.0.0.1:6379> ZRANGE zseta 0 -1 1) "asdaqwe"
127.0.0.1:6379> HMSET hash2 a 1 b 2 c 3 d 4 OK 127.0.0.1:6379> Hgetall hash2 1) "a" 2) "1" 3) "b" 4) "2" 5) "c" 6) "3" 7) "d" 8) "4"
127.0.0.1:6379> hmget hash2 b c 1) "2" 2) "3"
127.0.0.1:6379> hdel hash2 b (integer) 1 127.0.0.1:6379> Hgetall hash2 1) "a" 2) "1" 3) "c" 4) "3" 5) "d" 6) "4"
127.0.0.1:6379> hkeys hash2 1) "a" 2) "c" 3) "d" 127.0.0.1:6379> hvals hash2 1) "1" 2) "3" 3) "4"
127.0.0.1:6379> hlen hash2 (integer) 3
127.0.0.1:6379> keys * 1) "list1" 2) "k2" 3) "list" 4) "k3" 5) "set2" 6) "hash1" 7) "hash2" 8) "zseta" 9) "mykey" 10) "key1" 11) "set3" 12) "setb" 13) "seta" 14) "key2" 15) "k1" 16) "set1" 127.0.0.1:6379> keys my* 1) "mykey" 127.0.0.1:6379> EXISTS key1 (integer) 1 127.0.0.1:6379> EXISTS key11 (integer) 0 127.0.0.1:6379> del key1 (integer) 1 127.0.0.1:6379> get key1 (nil) 127.0.0.1:6379> EXPIRE k2 10 (integer) 1 127.0.0.1:6379> get k2 "2" 127.0.0.1:6379> get k2 (nil)
127.0.0.1:6379> ttl key2 (integer) 6 127.0.0.1:6379> ttl key2 (integer) 1 127.0.0.1:6379> ttl key2 (integer) -2
127.0.0.1:6379> move set2 1 (integer) 1 127.0.0.1:6379> select 1 OK 127.0.0.1:6379[1]> keys * 1) "set2" 127.0.0.1:6379[1]> SELECT 0 OK 127.0.0.1:6379> EXPIRE mykey 1000 (integer) 1 127.0.0.1:6379> ttl mykey (integer) 996 127.0.0.1:6379> rename setb set3 OK 127.0.0.1:6379> keys set* 1) "set3" 2) "seta" 3) "set1"
127.0.0.1:6379> TYPE seta set 127.0.0.1:6379> TYPE zseta zset 127.0.0.1:6379> type key1 none 127.0.0.1:6379> type hash2 hash 127.0.0.1:6379> type list list
dbsize //返回當前數據庫中key的數目
info//返回redis數據庫狀態信息數據庫
127.0.0.1:6379> DBSIZE (integer) 11 127.0.0.1:6379> info # Server redis_version:4.0.9 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:5c0f8253777cfd43 redis_mode:standalone os:Linux 3.10.0-514.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll atomicvar_api:atomic-builtin gcc_version:4.8.5 process_id:7661 run_id:8e0a79aba2f0507ea46724b5e274a026a4b03b14 tcp_port:6379 uptime_in_seconds:20486 uptime_in_days:0 hz:10 lru_clock:2406733 executable:/usr/local/src/redis-4.0.9/redis-server config_file:/etc/redis.conf # Clients connected_clients:1 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # Memory used_memory:850408 used_memory_human:830.48K used_memory_rss:7700480 used_memory_rss_human:7.34M used_memory_peak:872080 used_memory_peak_human:851.64K
[ ] config set dir //更改配置參數vim
127.0.0.1:6379> CONFIG GET dir 1) "dir" 2) "/data/redis" 127.0.0.1:6379> CONFIG GET dbfileema (empty list or set) 127.0.0.1:6379> CONFIG GET dbfilename 1) "dbfilename" 2) "dump.rdb"
前兩年Redis比較火的時候,也就是×××最嚴重的時候。見到了不少次由Redis被黑致使系統被黑的案例!api
Redis 服務啓動了,可是監聽了一個全網IP,若是6379默認端口沒有更改,而且iptables也處於關閉,公網IP 也開放着,Redis也沒有設置任何密碼,×××就會掃描端口,而後就能夠輕輕鬆鬆登陸你的Redis服務器,而且是超級管理員的權限。而後×××就能夠設置dir dbname , dir 定義到/root/.ssh 且dbname定義爲:.ssh/authorized_keys 再把這個值寫上本身的公鑰。 安全
這些應該都不陌生吧,這不就活生生的把×××的公鑰上傳到了我們的服務器中,而後利用超級管理員作任何想作的事服務器
打開配置文件 vim /etc/redis.confdom
設置監聽ip bind 127.0.0.1 2.2.2.2//能夠是多個ip,用空格分隔 設置監聽端口 port 16000 設置密碼 requirepass xavi redis-cli -a 'asd9577' 將config命令更名 rename-command CONFIG xavi 禁掉config命令 rename-command CONFIG 「」
[root@xavi redis-4.0.9]# vim /etc/redis.conf [root@xavi redis-4.0.9]# killall redis-server [root@xavi redis-4.0.9]# redis-server /etc/redis.conf //修改密碼後重啓redis [root@xavi redis-4.0.9]# redis-cli 127.0.0.1:6379> keys * (error) NOAUTH Authentication required. 127.0.0.1:6379> redis-cli -a 'xavi>com' (error) ERR unknown command 'redis-cli' 127.0.0.1:6379> keys * (error) NOAUTH Authentication required. 127.0.0.1:6379> quit [root@xavi redis-4.0.9]# redis-cli -a 'xavi>com' 127.0.0.1:6379> keys * (empty list or set)